【问题标题】:ImportError: cannot import name 'BlobService' when using Azure BackendImportError:使用 Azure 后端时无法导入名称“BlobService”
【发布时间】:2018-03-23 20:00:09
【问题描述】:

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

还为每个文档添加了额外的包: https://docs.microsoft.com/en-us/azure/storage/blobs/storage-python-how-to-use-blob-storage

收到此错误: Traceback(最近一次调用最后一次):

  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

我的 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

【问题讨论】:

  • 你解决了这个问题吗?我现在正面临这个问题。

标签: python django azure azure-blob-storage


【解决方案1】:

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

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

from azure.storage.blob import BlobServiceClient

可以在文档here 中看到一个示例。

【讨论】:

  • 你能告诉它更新的是哪个版本吗?
  • 从 Laurent 的回答看来,这个变化是在 0.37 版中引入的
【解决方案2】:

当您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 azure-storage-blob >= 0.37 不兼容。你默默地用 0.37.0 版本替换了一些 0.34.3 的代码文件。

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

pip3 install azure-storage-blob
pip3 install azure

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

TLDR,有人需要更新 django-storages 以支持 azure-storage-blob >= 0.37.0。同时,坚持azure-storage azure-storage-blob。

【讨论】:

  • 你是对的,当我将 Docker 容器向上推并且不再手动进行安装时,事情就开始崩溃了。我会试试你的方法。
  • 所以只安装 Django-storages 和 azure-storage=0.36 会产生错误:文件“/usr/local/lib/python3.6/site-packages/storages/backends/azure_storage.py”,第 26 行,在 from azure.storage import BlobService ImportError: cannot import name 'BlobService'。我不确定是否要安装更多的库以及什么版本不会意外得到库混淆。
【解决方案3】:

我不得不降级到azure-storage-0.20.0 解决了这个问题,新版本找不到BlobService 类。

【讨论】:

    【解决方案4】:

    在合并之前,当前的 azure 存储库不起作用。所以为了让它工作,我必须安装一个私有的 Django 存储分支:

    我分叉了:https://github.com/guydou/django-storages/tree/upgrade_azure 并指出我自己的回购:

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

    然后安装 blob 库和 azure SDK:

    pip3 install azure
    pip3 install azure-storages==0.34.3
    

    之后配置似乎起作用了。 azure 的最终需求文档是这样的: (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
    

    【讨论】:

    【解决方案5】:

    奇怪...在Azure官方文档中,它说您需要from azure.storage.blob import BlockBlobService,但您所做的是from azure.storage.blob.blobservice import BlobServicefrom azure.storage import BlobService

    那你为什么不按照官方文档来呢?

    【讨论】:

    • django 提供的 Azure Storage 似乎已经过时了。
    • 我实际上并没有写那些导入。我有一个 Django 项目,我在其中使用 Azure 作为后端服务。导入在官方库中。我认为人们通过版本控制或排序导入以某种方式解决了问题。正在寻找解决方法。
    • 问题 - 是使用 Azure 博客存储手工制作自己的实现的唯一方法吗?
    • @mikebz 查看此拉取请求:github.com/jschneier/django-storages/pull/394,至少现在答案是“是”。
    • 谢谢!它是否与某些版本中断?我很好奇我是否可以引入与 Django 1.11 和 Python 3.6 兼容的不同版本,这样我就不必手工制作这些东西了。使用该框架的原因之一是避免手工制作基本服务......
    猜你喜欢
    • 2017-02-23
    • 2023-04-05
    • 2020-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-07
    • 2018-11-08
    • 2018-07-03
    相关资源
    最近更新 更多