AzureML Scoring Script fails with ImportError: no known parent package

Sunil Singhal 11 Reputation points Microsoft Employee
2020-10-29T11:13:16.16+00:00

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 Learning
Azure Machine Learning
An Azure machine learning service for building and deploying models.
2,580 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Ramr-msft 17,616 Reputation points
    2020-10-29T16:20:54.573+00:00

    @Sunil Singhal 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://learn.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


  2. Ramr-msft 17,616 Reputation points
    2020-11-02T10:44:15.697+00:00

    @Sunil Singhal 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.

    0 comments No comments

  3. Rahman, Choudhury 1 Reputation point
    2022-10-12T17:44:52.173+00:00

    Hi, I am having the same issue. Could you please direct me to the right resource from where I can get some insight to solve my issue. I have a trained model file which also includes some supported py module files. The tree is shown below:

    model --> conf --> hmcn.json
    --> data --> (some other required json files)
    --> dataset --> init.py
    --> classification_dataset.py
    --> collator.py
    --> data_preprocessor.py
    --> dataset.py
    --> model --> (some other .py module files)
    --> config.py
    --> HMCN
    --> util.py

    In the above tree HMCN is the model file and as shown there are some python module files which are imported in the score.py (not included in this tree) script for inferencing. I was using the AZURE Web UI and registered the model by selecting the "model" directory so that in the artifacts tab the root item is shown as "model" and inside the model there are all the files shown in the tree are uploaded.

    Now I am trying to create an endpoint and deploy the model by using a "score.py" script for real-time inferencing. I did it with the WebUI by going to Endpoints --> Create deployment and in the Environment I selected score.py and choose PyTorch 1.9 curated environment but the during deployment the process fails and gives error that the module not found in the "score.py". The module that it is referring is in the "config.py" as shown in the model tree. I believe I need to set the path of the model tree so that the "score.py" can find the module. But I do now know how.

    Your help would be greatly appreciated. Thanks

    0 comments No comments