question

SunilSinghal-3380 avatar image
0 Votes"
SunilSinghal-3380 asked ramr-msft answered

AzureML Scoring Script fails with ImportError: no known parent package

On trying to deploy a Model as a Container, endpoint gets created, however, scoring script fails with an error:

ImportError: attempted relative import with no known parent package



This is because i'm referencing another module (packaged in the docker image using source_directory) with a relative path from scoring file.

Can you help me in resolving this error?

Files\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

azure-machine-learningazure-machine-learning-inference
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

ramr-msft avatar image
0 Votes"
ramr-msft answered ramr-msft edited

@SunilSinghal-3380 Thanks for the question. When deploying your inference script, beyond the entry script (score.py), inferenceConfig also let you specify source directory that include the entry script as well as all other python code (packages as a subfolder in the source directory that has its own init.py, or plain python script files modules). The score.py script can directly import from them because the whole folder including score.py and all other folders will be available at the inference running environment. There is no need to save them as a "model".
https://docs.microsoft.com/en-us/python/api/azureml-core/azureml.core.model.inferenceconfig?view=azure-ml-py

Full sample available at https://github.com/Azure/MachineLearningNotebooks/blob/master/how-to-use-azureml/deployment/deploy-to-cloud/model-register-and-deploy.ipynb

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@ramr-msft
Yes and that's exactly I'm doing. Specified the source_directory in an Inference config.
That's not the problem.
The problem is in importing the other modules in scoring and other modules through relative imports.

In my scoring script, I have an import statement:

from .module1.py import SomeClass

this is failing.

And, all through my project files, I'm using relative imports in other files as well.



2 Votes 2 ·
ramr-msft avatar image
0 Votes"
ramr-msft answered

@SunilSinghal-3380 Thanks for the details. When you specify a source directory and a path(relative to the source_directory) to entry script, and if your deployment is failing, most likely the issue is with how you entry_script references other files in the source_directory. register-model-deploy-local-advanced.ipynb has an example of how to specify source directory and perform a local deployment for faster troubleshooting.

How to deploy using environments can be found here model-register-and-deploy.ipynb . InferenceConfig class accepts source_directory and entry_script parameters, where source_directory is a path to the folder that contains all files(score.py and any other additional files) to create the image.
This multi-model-register-and-deploy.ipynb has code snippets on how to create InferenceConfig with source_directory and entry_script.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.