mikebz
mikebz

Reputation: 3335

ImportError: cannot import name 'BlobService' when using Azure Backend

I followed these instructions to set up Azure as my backend service: http://django-storages.readthedocs.io/en/latest/backends/azure.html

Also added additional packages per this document: https://learn.microsoft.com/en-us/azure/storage/blobs/storage-python-how-to-use-blob-storage

Getting this error: Traceback (most recent call last):

  File "/usr/local/lib/python3.6/site-packages/storages/backends/azure_storage.py", line 23, in <module>
    from azure.storage.blob.blobservice import BlobService
ModuleNotFoundError: No module named 'azure.storage.blob.blobservice'

....

  File "/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 978, in _gcd_import
  File "<frozen importlib._bootstrap>", line 961, in _find_and_load
  File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
  File "/usr/local/lib/python3.6/site-packages/storages/backends/azure_storage.py", line 26, in <module>
    from azure.storage import BlobService
ImportError: cannot import name 'BlobService'
[12/Oct/2017 01:38:00] "POST /upload HTTP/1.1" 500 18034

My pip3 freeze looks like so:

(venv) Mikes-MacBook:drhazelapp mikebz$ pip3 freeze | grep azure
azure==2.0.0
azure-batch==3.0.0
azure-common==1.1.8
azure-datalake-store==0.0.17
azure-graphrbac==0.30.0
azure-keyvault==0.3.7
azure-mgmt==1.0.0
azure-mgmt-authorization==0.30.0
azure-mgmt-batch==4.0.0
azure-mgmt-cdn==0.30.3
azure-mgmt-cognitiveservices==1.0.0
azure-mgmt-compute==1.0.0
azure-mgmt-containerregistry==0.2.1
azure-mgmt-datalake-analytics==0.1.6
azure-mgmt-datalake-nspkg==2.0.0
azure-mgmt-datalake-store==0.1.6
azure-mgmt-devtestlabs==2.0.0
azure-mgmt-dns==1.0.1
azure-mgmt-documentdb==0.1.3
azure-mgmt-iothub==0.2.2
azure-mgmt-keyvault==0.31.0
azure-mgmt-logic==2.1.0
azure-mgmt-monitor==0.2.1
azure-mgmt-network==1.0.0
azure-mgmt-nspkg==2.0.0
azure-mgmt-rdbms==0.1.0
azure-mgmt-redis==4.1.0
azure-mgmt-resource==1.1.0
azure-mgmt-scheduler==1.1.3
azure-mgmt-sql==0.5.3
azure-mgmt-storage==1.0.0
azure-mgmt-trafficmanager==0.30.0
azure-mgmt-web==0.32.0
azure-nspkg==2.0.0
azure-servicebus==0.21.1
azure-servicefabric==5.6.130
azure-servicemanagement-legacy==0.20.6
azure-storage==0.34.3
azure-storage-blob==0.37.0
azure-storage-common==0.37.0
azure-storage-file==0.37.0
azure-storage-nspkg==2.0.0
msrestazure==0.4.14

Upvotes: 10

Views: 28095

Answers (5)

Adam T
Adam T

Reputation: 786

In newer versions of azure-storage-blob the import BlockBlobService has been renamed to BlobServiceClient.

Updating your import statement to below should resolve your issue:

from azure.storage.blob import BlobServiceClient

An example of this can seen in the documentation here.

Upvotes: 16

StackEdd
StackEdd

Reputation: 712

I had to downgrade to azure-storage-0.20.0 which fixed the issue, the newer versions couldn't find the BlobService class.

Upvotes: 4

mikebz
mikebz

Reputation: 3335

Before the merge the current azure storage library doesn't work. So to get this to work I had to install a private fork of Django storages:

I forked: https://github.com/guydou/django-storages/tree/upgrade_azure and pointed to my own repo:

pip3 install -e 'git+https://github.com/mikebz/django-storages.git#egg=upgrade_azure'

then install blob library and azure SDK:

pip3 install azure
pip3 install azure-storages==0.34.3

After that the configuration seemed to work. The final requirements document from azure were this: (venv) Mikes-MacBook:drhazelapp mikebz$ pip3 freeze

adal==0.4.7
appnope==0.1.0
asn1crypto==0.23.0
astroid==1.5.3
azure==2.0.0
azure-batch==3.0.0
azure-common==1.1.8
azure-datalake-store==0.0.17
azure-graphrbac==0.30.0
azure-keyvault==0.3.7
azure-mgmt==1.0.0
azure-mgmt-authorization==0.30.0
azure-mgmt-batch==4.0.0
azure-mgmt-cdn==0.30.3
azure-mgmt-cognitiveservices==1.0.0
azure-mgmt-compute==1.0.0
azure-mgmt-containerregistry==0.2.1
azure-mgmt-datalake-analytics==0.1.6
azure-mgmt-datalake-nspkg==2.0.0
azure-mgmt-datalake-store==0.1.6
azure-mgmt-devtestlabs==2.0.0
azure-mgmt-dns==1.0.1
azure-mgmt-documentdb==0.1.3
azure-mgmt-iothub==0.2.2
azure-mgmt-keyvault==0.31.0
azure-mgmt-logic==2.1.0
azure-mgmt-monitor==0.2.1
azure-mgmt-network==1.0.0
azure-mgmt-nspkg==2.0.0
azure-mgmt-rdbms==0.1.0
azure-mgmt-redis==4.1.0
azure-mgmt-resource==1.1.0
azure-mgmt-scheduler==1.1.3
azure-mgmt-sql==0.5.3
azure-mgmt-storage==1.0.0
azure-mgmt-trafficmanager==0.30.0
azure-mgmt-web==0.32.0
azure-nspkg==2.0.0
azure-servicebus==0.21.1
azure-servicefabric==5.6.130
azure-servicemanagement-legacy==0.20.6
azure-storage==0.34.3
certifi==2017.7.27.1
cffi==1.11.2
chardet==3.0.4
cryptography==2.1.1
decorator==4.1.2
Django==1.11.5
django-extensions==1.9.1
-e git+https://github.com/mikebz/django-storages.git@5d073159adc0138f5acef121c2d578f4eba96d7c#egg=django_storages
django-webpack-loader==0.5.0
gunicorn==19.7.1
idna==2.6
ipython==6.2.1
ipython-genutils==0.2.0
isodate==0.6.0
isort==4.2.15
jedi==0.11.0
keyring==10.4.0
lazy-object-proxy==1.3.1
mccabe==0.6.1
msrest==0.4.17
msrestazure==0.4.15
oauthlib==2.0.4
olefile==0.44
parso==0.1.0
pep8==1.7.0
pexpect==4.2.1
pickleshare==0.7.4
Pillow==4.3.0
prompt-toolkit==1.0.15
ptyprocess==0.5.2
pycparser==2.18
Pygments==2.2.0
PyJWT==1.5.3
pylint==1.7.2
python-dateutil==2.6.1
pytz==2017.2
requests==2.18.4
requests-oauthlib==0.8.0
simplegeneric==0.8.1
six==1.10.0
traitlets==4.3.2
urllib3==1.22
wcwidth==0.1.7
whitenoise==3.3.1
wrapt==1.10.11

Upvotes: 1

Laurent Mazuel
Laurent Mazuel

Reputation: 3546

When you pip install azure, you installed azure-storage 0.34.3 (tutorial 1). When you followed the second tutorial, you installed azure-storage-blob 0.37.0. This is where you got issues, there is massive breaking changes in 0.37.0 in the namespaces:

https://github.com/Azure/azure-storage-python/blob/master/BreakingChanges.md#version-0370

See in the ChangeLog that azure-storage <= 0.36 is incompatible with azure-storage-blob >= 0.37. You silently replaced some code file of 0.34.3 by 0.37.0 version.

In you second test, you said you did:

pip3 install azure-storage-blob
pip3 install azure

Package are still incompatible, but you did it in a reverse order, where you crushed your 0.37.0 version with the 0.34.3 one this time. It's why it works.

TLDR, someone needs to update django-storages to support azure-storage-blob >= 0.37.0. In the mean time, stick to azure-storage <= 0.36 and DON'T install azure-storage-blob at all.

Upvotes: 9

Sraw
Sraw

Reputation: 20214

Strange... In Azure official documentation, it says you need to from azure.storage.blob import BlockBlobService, but what you did is from azure.storage.blob.blobservice import BlobService and from azure.storage import BlobService.

So why don't you just follow the official documentation?

Upvotes: 0

Related Questions