os.makedir() in score.py, init() function, throws permission error while deploying

tekmen0 0 Reputation points
2023-04-13T14:36:55.9533333+00:00

I have to create files and directories in order to perform prediction of my custom model. here is my init function and deployment logs :


import os

inputs_root = "inputs"
outputs_root = "outputs"

def init():
    if not os.path.exists(inputs_root):
        os.mkdir(inputs_root)
    if not os.path.exists(outputs_root):
        os.mkdir(outputs_root)

It throws following error which can be seen from the deployment logs as follows :

2023-04-13 14:10:52,736 E [68] azmlinfsrv - Encountered Exception Traceback (most recent call last):
  File "/opt/miniconda/envs/amlenv/lib/python3.8/site-packages/azureml_inference_server_http/server/user_script.py", line 117, in invoke_init
    self._user_init()
  File "/var/azureml-app/avaj1870/src/score.py", line 71, in init
    os.mkdir(inputs_root)
PermissionError: [Errno 13] Permission denied: 'inputs'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/opt/miniconda/envs/amlenv/lib/python3.8/site-packages/azureml_inference_server_http/server/aml_blueprint.py", line 111, in setup
    self.user_script.invoke_init()
  File "/opt/miniconda/envs/amlenv/lib/python3.8/site-packages/azureml_inference_server_http/server/user_script.py", line 119, in invoke_init
    raise UserScriptException(ex) from ex
azureml_inference_server_http.server.user_script.UserScriptException: Caught an unhandled exception from the user script
Azure Machine Learning
Azure Machine Learning
An Azure machine learning service for building and deploying models.
2,608 questions
{count} votes

1 answer

Sort by: Most helpful
  1. YutongTie-MSFT 46,996 Reputation points
    2023-04-14T00:51:33.87+00:00

    Hello @tekmen0

    Thanks for reaching out to us. Based on the error message you provided, it seems like the user running the code does not have the necessary permissions to create the 'inputs' directory. To fix this issue, you can try specifying the absolute path of the directories you want to create, rather than using relative paths. This will ensure that the directories are created in a location where the user running the code has the necessary permissions.

    For example, you can try replacing the inputs_root and outputs_root variables with absolute paths like this:

    luaCopy code
    inputs_root = os.path.join(os.environ['AZUREML_MODEL_DIR'], 'inputs')
    outputs_root = os.path.join(os.environ['AZUREML_MODEL_DIR'], 'outputs')
    

    This will create the inputs and outputs directories inside the AZUREML_MODEL_DIR directory, which is a location where the user running the code should have the necessary permissions to create files and directories. Alternatively, you can try running the container with elevated privileges (i.e., as an administrator or root user) to ensure that the user running the code has the necessary permissions to create files and directories.

    Note that if you are using Azure Machine Learning to deploy your model, you can also use the os.makedirs() function instead of os.mkdir() to create the directories recursively, along with any necessary parent directories. This can be done by replacing the if not os.path.exists() statements with the following code:

    graphqlCopy code
    os.makedirs(inputs_root, exist_ok=True)
    os.makedirs(outputs_root, exist_ok=True)
    

    The exist_ok=True argument ensures that the function does not raise an error if the directory already exists.

    Please have a try and let me know how it works, I am happy to help further, a similar Python issue here - https://stackoverflow.com/questions/67851446/why-is-os-mkdir-returning-error-when-it-shouldnt-beginner-question

    I hope this helps.Thanks!

    Regards, Yutong -Please kindly accept the answer if you feel helpful to support the community, thanks a lot.