MachineLearningNotebooks icon indicating copy to clipboard operation
MachineLearningNotebooks copied to clipboard

Relative imports for custom modules

Open maknotavailable opened this issue 6 years ago • 13 comments

I have the following repository structure

root
-code
--main.py
--helper.py

When I deploy a service, I use the following InferenceConfig, from the root level:

inference_config = InferenceConfig(entry_script='code/main.py',
                                   source_directory='.',
                                   environment=environment)

Running everything locally works fine. Even from the command line as python code/main.py. However, as soon as I deploy it as a service I get an ModuleNotFoundError, because the main.py can't find/import the helper (currently done via simple import helper).

I have tried both of these workarounds with no luck:

  • before the import, add sys.path.append('./code')
  • add __init__.py to the /code

What is the right approach to relative module imports in AML, when the deployment is done from the root repository folder and additional modules are in sub-folders?

maknotavailable avatar Mar 03 '20 17:03 maknotavailable

Here a hacky work-around, adding this to the top of your main.py: import os, sys; sys.path.append(os.path.dirname(os.path.realpath(__file__)))

maknotavailable avatar Mar 03 '20 17:03 maknotavailable

Further looking into this, i saw that the current working directory ( os.getcwd() ) points to : /var/azureml-app/

This causes issues when trying to load things from the repository, as it is not the same behavior when running the code locally. For example, when trying to retrieve a file from /var/azureml-app/repo-name/assets.

Why is os.getcwd() != /var/azureml-app/repo-name/ ?

maknotavailable avatar Mar 04 '20 10:03 maknotavailable

@vaidya-s

jpe316 avatar Apr 17 '20 01:04 jpe316

@Bozhong68

vaidya-s avatar Aug 28 '20 04:08 vaidya-s

@maknotavailable you can change inference_config to following to make it work: inference_config = InferenceConfig(entry_script='main.py', source_directory='code', environment=environment)

Bozhong68 avatar Aug 31 '20 08:08 Bozhong68

@maknotavailable thanks. I am experiencing the same problem. I don't understand from where we would execute your code. If you use main.py as the entry_script it means you would be inside the code directory? Then source_directory should be .code and not code?

Konstantina-Lazaridou avatar Sep 02 '20 14:09 Konstantina-Lazaridou

Why is os.getcwd() != /var/azureml-app/repo-name/

In the upcoming SDK release, the current working directory will be changed to the source directory; if one was given in the inference config.

@maknotavailable

I have tried both of these workarounds with no luck: before the import, add sys.path.append('./code') add init.py to the /code

The problem is that the current working directory (in the current AzureML SDK) is set to the directory above the source directory. So add sys.path.append('./code') wouldn't have worked because it would have needed to be add sys.path.append('./<source_directory_name>/code'). And add __init__.py to the /code doesn't work because it just allows for you do import code and treat the directory as a module.

The fix for this currently is to follow https://docs.microsoft.com/en-us/azure/machine-learning/how-to-deploy-and-where?tabs=azcli#define-an-entry-script . Specifically:

    # If your model were stored in the same directory as your score.py, you could also use the following:
    # model_path = os.path.abspath(os.path.join(os.path.dirname(__file__), 'sklearn_mnist_model.pkl')

In @maknotavailable 's situation, this would be (this will work in the current SDK and future releases):

import sys
# add code directory to sys path
sys.path.append(os.path.abspath(os.path.dirname(__file__)))
# should be able to import helper.py in code directory
import helper

after the next SDK release, this should be possible:

from code import helper

jonringer avatar Sep 03 '20 15:09 jonringer

@jonringer for providing explanation and solution! @maknotavailable , @Konstantina-Lazaridou I will close the issue now, please feel free to re-open if you still have issues.

Bozhong68 avatar Sep 03 '20 22:09 Bozhong68

I'm trying to accomplish pretty much a similar thing, with one change that i have relative imports in my files. Due to relative imports, scoring file fails with an error:

ImportError: attempted relative import with no known parent package

iles\modules structure (a simplified version):

project ->src -> scoring.py -> module1.py -> common -> module2.py, etc -> init.py -> init.py -> configs -> conda_env.yml

In scoring.py, from .module1.py import SomeClass .. ..

In module1.py, from .common.module2.py importSC2 ... ..

And below is how an Inference config is initialized: inference_config = InferenceConfig(source_directory="./", runtime= "python", entry_script="src/scoring.py", conda_file="configs/conda_env.yml" )

I could not pass entry_script as "src.scoring" as this fails the Validation and relative path to scoring file is expected

singhalsunil065 avatar Oct 29 '20 11:10 singhalsunil065

@jonringer

Bozhong68 avatar Oct 29 '20 12:10 Bozhong68

I got same issue when deploy the model in azure pipeline

Tried with/without source_directory , still that issue persist

inference_config = InferenceConfig(entry_script='score.py', environment=registered_env, source_directory='./')

Error log:

"message": "entry_script /mnt/batch/tasks/shared/LS_root/jobs/fb-aml-ws/azureml/85fc1381-c868-4dc6-a6ab-ce6073fbb68a/wd/azureml/85fc1381-c868-4dc6-a6ab-ce6073fbb68a/score.py doesn't exist. entry_script should be path relative to current working directory"

Logged to compute instance and check the mount path, files is not there when processing from azure pipeline

Any help appreciated!

dhgokul avatar May 21 '22 05:05 dhgokul

@dhgokul the everything under source folder should be copied into your container. what did you get from folder /mnt/batch/tasks/shared/LS_root/jobs/fb-aml-ws/azureml/85fc1381-c868-4dc6-a6ab-ce6073fbb68a/wd/azureml/85fc1381-c868-4dc6-a6ab-ce6073fbb68a?

seems like you were running script under parent folder of src, if true, you should use something like below inference_config = InferenceConfig(source_directory="./src", runtime= "python", entry_script="scoring.py", conda_file="configs/conda_env.yml" )

ghost avatar May 23 '22 07:05 ghost

@yo4test Thanks for your reply! I have uploaded score.py to blob storage and downloaded to the current pipeline directory and it is working as expected. If I gave the source path in interface config still we got the same issue.

dhgokul avatar May 23 '22 08:05 dhgokul