3

I have created an event grid triggered azure function in python. I have deployed my solution to azure successfully and the execution is working fine. But, I have an issue with calling another python script in the same folder location. My code is given below: -

import os, json, subprocess
import logging   
import azure.functions as func

def main(event: func.EventGridEvent):
    try:
        correctionsMessages = event.get_json()
        for correctionMessage in correctionsMessages:
            strMessage = json.dumps(correctionMessage)
            full_path_to_script = os.path.join(os.path.dirname(os.path.realpath(__file__)) + '/' + correctionMessage['ScriptName'] + '.py')
            logging.info('Script Path: %s', full_path_to_script)
            logging.info('Parameter: %s', json.dumps(detectionMessage))
            subprocess.check_call('python '+ full_path_to_script + ' ' +  json.dumps(strMessage))

        result = json.dumps({
            'id': event.id,
            'data': event.get_json(),
            'topic': event.topic,
            'subject': event.subject,
            'event_type': event.event_type,
        })
        logging.info('Python EventGrid trigger processed an event: %s', result)

    except Exception as e:
        logging.info('Error: %s', e)

The above code is giving error for subprocess.check_call. Error is "Error: [Errno 2] No such file or directory: 'python /home/site/wwwroot/Detections/Script1.py". Script1.py is in same folder with init.py. When i am running this function locally, it is working absolutely fine.

1 Answer 1

3

Per my experience, the error was caused by the subprocess.check_call function not know the call path of python, not due to the Script1.py path.

On your local for Azure Functions development environment, the python path has been configured in the local environment variable, so the subprocess.check_call function could invoke python via search the python execute file from the paths of environment variable. But on cloud, there is not a python path value pre-configured in the same environment variable, only the Azure Function Host know the real absoluted path for Python.

So the solution is to find out the real absoluted path of Python and use it instead of python in your code.

However, in Azure Function for Python stack runtime, I think it's not a good idea for using subprocess.check_call to spawn a child process to do some processing for a given message. The safe and correct way is to define a function in Script1.py or directly in __init__.py to pass the given message as parameters to realize the same feature.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.