ImportError:使用Azure后端时无法导入名称“ BlobService”

mik*_*ebz 5 python django azure azure-storage-blobs

我按照以下说明将Azure设置为我的后端服务:http : //django-storages.readthedocs.io/en/latest/backends/azure.html

每个文档还添加了其他软件包:https : //docs.microsoft.com/zh-cn/azure/storage/blobs/storage-python-how-to-use-blob-storage

收到此错误:追溯(最近一次呼叫最近):

  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'
Run Code Online (Sandbox Code Playgroud)

....

  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
Run Code Online (Sandbox Code Playgroud)

我的pip3冻结看起来像这样:

(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
Run Code Online (Sandbox Code Playgroud)

Ada*_*m T 10

在较新版本中azure-storage-blob,导入BlockBlobService已重命名为BlobServiceClient.

将您的导入语句更新为以下应该可以解决您的问题:

from azure.storage.blob import BlobServiceClient
Run Code Online (Sandbox Code Playgroud)

可以在此处的文档中看到一个示例。


Lau*_*uel 7

当您pip install azure安装时azure-storage 0.34.3(教程 1)。当您按照第二个教程进行操作时,您安装了azure-storage-blob 0.37.0. 这就是您遇到问题的地方,名称空间中的 0.37.0 中有大量重大更改:

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

在 ChangeLog 中看到azure-storage<= 0.36 与azure-storage-blob>= 0.37不兼容。你默默地用 0.37.0 版本替换了一些 0.34.3 的代码文件。

在你的第二次测试中,你说你做到了:

pip3 install azure-storage-blob
pip3 install azure
Run Code Online (Sandbox Code Playgroud)

软件包仍然不兼容,但是您以相反的顺序进行了操作,这次您将 0.37.0 版本与 0.34.3 版本粉碎了。这就是它起作用的原因。

TLDR,有人需要更新 django-storages 以支持 azure-storage-blob >= 0.37.0。同时,坚持azure-storage<= 0.36 并且azure-storage-blob根本不要安装。