简体   繁体   中英

Unable to use azure SDK in Python

I did this:

sudo pip install azure azure-storage azure-servicebus azure-mgmt azure-servicemanagement-legacy

from azure import * 

Traceback (most recent call last): File "", line 1, in ImportError: No module named azure

from azure.storage import BlobService

Traceback (most recent call last): File "", line 1, in ImportError: No module named azure.storage

Python package installed thru cmd sudo pip install exists at the paths /usr/lib/python2.7 , /usr/local/python2.7 , etc and their sub-folder dist-packages .

You can code import sys and print sys.path in the Python Interpreter to show the completed path list for current python environment.

Iif you installed successfully some packages like azure & azure-storage , you can find these files relate to the package in the python library paths.

However, you got the error in Import Error: No module named <package-name> when you run the code import <package-name> or from <package-name> import <class or object name> . There are two scenes that would be cause the issue normally.

  1. Package not installed successfully.
  2. The library path included package that not exists in Python system environment path sys.path in python or PYTHONHOME in environment variables.

So I think you can try to solve the issue thru three ways below.

  1. Dynamically add the package path into sys.path thru the method sys.path.append('<package path>') in python code.
  2. Check the environment variable PYTHONHOME whether or not set up. If set up PYTHONHOME , python will add package path based on PYTHONHOME into sys.path .
  3. If your python environment encounter some unknown fault that is unrecoverable, you can try to reinstall Python thru commands sudo apt-get remove python python-pip , sudo apt-get update , sudo apt-get install python python-pip on Ubuntu. It's a simple way.

BlobService belongs to azure.storage.blob rather than the azure.storage

it should rather be

from azure.storage.blob import BlobService

Link - https://azure.microsoft.com/en-us/documentation/articles/storage-python-how-to-use-blob-storage/

If it still doesn't work for you, you might would like to use virtualEnv and do the pip install again while in virtualenv

http://docs.python-guide.org/en/latest/dev/virtualenvs/

I had very similar issue. There was a lot of confusion between python2 and python3 package versions as there was no virtual env used and I also had to ungrade pip to 18.

But anyway, this is is how I resolved the part in question.

Locate where the package was installed:

pip show azure    

The output will show the location of the package in the Location section:

Name: azure
Version: 4.0.0
Summary: Microsoft Azure Client Libraries for Python
Home-page: https://github.com/Azure/azure-sdk-for-python
Author: Microsoft Corporation
Author-email: azpysdkhelp@microsoft.com
License: MIT License
Location: /usr/local/lib/python3.6/dist-packages
Requires: azure-servicefabric, azure-cosmosdb-table, azure-datalake-store, azure-loganalytics, azure-eventgrid, azure-servicemanagement-legacy, azure-servicebus, azure-graphrbac, azure-storage-blob, azure-mgmt, azure-storage-file, azure-batch, azure-applicationinsights, azure-keyvault, azure-storage-queue
Required-by: 

If you do:

python -c "import sys;print(sys.path)"

You will see a list of pip package locations:

['/app', '/usr/lib/python36.zip', '/usr/lib/python3.6', '/usr/lib/python3.6/lib-dynload', '/usr/local/lib/python3.6/dist-packages', '/usr/lib/python3/dist-packages']

At the begining of my python file I added:

sys.path.insert( 0, '/usr/local/lib/python3.6/dist-packages' )

This will make sure this package location will be checked in the first place.

UPDATE

Thinking about it in the morning, things opened from a new perspective for me. I saw, that I had #!/usr/bin/python at the beginning of my python file, which says to use the wrong interpreter and look for pip packages in the wrong place.

azure metapackage is deprecated and azure-storage is not being maintained anymore

Please use azure-storage-blob >= 12.0

pip install azure-storage-blob
from azure.storage.blob import BlobServiceClient

PS: I write SDKs for azure

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM