How to fix ModuleNotFoundError in azureml-sdk when installed inside conda environment

I'm setting up a conda environment on Windows 10 Pro x64 using Miniconda 4.5.12 and have done a pip install of azureml-sdk inside the environment but get a ModuleNotFoundError when attempting to execute the following code:

import azureml.core
azureml.core.VERSION

This is the output:

Traceback (most recent call last):
  File "azureml.py", line 1, in <module>
    import azureml.core
  File "D:\Projects\style-transfer\azureml.py", line 1, in <module>
    import azureml.core
ModuleNotFoundError: No module named 'azureml.core'; 'azureml' is not a package

The code above has been run from the conda prompt, with the test environment active as well as in vscode with the same environment selected.

I setup the conda environment as per the following:

  1. Created the conda environment conda create -n test.
  2. Activated the environment activate test.
  3. Installed pip conda install pip.
  4. Installed azureml-sdk pip install azureml-sdk.

This results in the following packages being installed in the environment as per conda list:

adal                      1.2.0                     <pip>
antlr4-python3-runtime    4.7.2                     <pip>
applicationinsights       0.11.7                    <pip>
argcomplete               1.9.4                     <pip>
asn1crypto                0.24.0                    <pip>
azure-cli-command-modules-nspkg 2.0.2                     <pip>
azure-cli-core            2.0.54                    <pip>
azure-cli-nspkg           3.0.3                     <pip>
azure-cli-profile         2.1.2                     <pip>
azure-cli-telemetry       1.0.0                     <pip>
azure-common              1.1.16                    <pip>
azure-graphrbac           0.53.0                    <pip>
azure-mgmt-authorization  0.51.1                    <pip>
azure-mgmt-containerregistry 2.5.0                     <pip>
azure-mgmt-keyvault       1.1.0                     <pip>
azure-mgmt-nspkg          3.0.2                     <pip>
azure-mgmt-resource       2.0.0                     <pip>
azure-mgmt-storage        3.1.0                     <pip>
azure-nspkg               3.0.2                     <pip>
azure-storage-blob        1.4.0                     <pip>
azure-storage-common      1.4.0                     <pip>
azure-storage-nspkg       3.1.0                     <pip>
azureml-core              1.0.6                     <pip>
azureml-pipeline          1.0.6                     <pip>
azureml-pipeline-core     1.0.6                     <pip>
azureml-pipeline-steps    1.0.6                     <pip>
azureml-sdk               1.0.6                     <pip>
azureml-telemetry         1.0.6                     <pip>
azureml-train             1.0.6                     <pip>
azureml-train-core        1.0.6                     <pip>
azureml-train-restclients-hyperdrive 1.0.6                     <pip>
backports.tempfile        1.0                       <pip>
backports.weakref         1.0.post1                 <pip>
bcrypt                    3.1.5                     <pip>
ca-certificates           2018.03.07                    0
certifi                   2018.11.29               py37_0
cffi                      1.11.5                    <pip>
chardet                   3.0.4                     <pip>
colorama                  0.4.1                     <pip>
contextlib2               0.5.5                     <pip>
cryptography              2.4.2                     <pip>
docker                    3.6.0                     <pip>
docker-pycreds            0.4.0                     <pip>
futures                   3.1.1                     <pip>
humanfriendly             4.17                      <pip>
idna                      2.8                       <pip>
isodate                   0.6.0                     <pip>
jmespath                  0.9.3                     <pip>
jsonpickle                1.0                       <pip>
knack                     0.5.1                     <pip>
msrest                    0.6.2                     <pip>
msrestazure               0.6.0                     <pip>
ndg-httpsclient           0.5.1                     <pip>
oauthlib                  2.1.0                     <pip>
openssl                   1.1.1a               he774522_0
paramiko                  2.4.2                     <pip>
pathspec                  0.5.9                     <pip>
pip                       18.1                     py37_0
portalocker               1.2.1                     <pip>
pyasn1                    0.4.4                     <pip>
pycparser                 2.19                      <pip>
Pygments                  2.3.1                     <pip>
PyJWT                     1.7.1                     <pip>
PyNaCl                    1.3.0                     <pip>
pyOpenSSL                 18.0.0                    <pip>
pypiwin32                 223                       <pip>
pyreadline                2.1                       <pip>
python                    3.7.1                h8c8aaf0_6
python-dateutil           2.7.5                     <pip>
pytz                      2018.7                    <pip>
pywin32                   224                       <pip>
PyYAML                    3.13                      <pip>
requests                  2.21.0                    <pip>
requests-oauthlib         1.0.0                     <pip>
ruamel.yaml               0.15.51                   <pip>
SecretStorage             2.3.1                     <pip>
setuptools                40.6.3                   py37_0
six                       1.12.0                    <pip>
sqlite                    3.26.0               he774522_0
tabulate                  0.8.2                     <pip>
urllib3                   1.23                      <pip>
vc                        14.1                 h0510ff6_4
vs2015_runtime            14.15.26706          h3a45250_0
websocket-client          0.54.0                    <pip>
wheel                     0.32.3                   py37_0
wheel                     0.30.0                    <pip>
wincertstore              0.2                      py37_0

If I run which pip, I get the following output, which confirms that I used the pip inside the environment to install azureml-sdk, I think:

/c/Users/allan/Miniconda3/envs/test/Scripts/pip

I can also see that the azureml packages do in fact exist within the environment folder structure.

Answers

It's probably because the name if your python file is the same as a module name you are trying import. In this case, rename the file to something other than azureml.py.

Posted on by najeem