[英]error importing 'BlobServiceClient' from 'azure.storage.blob'
我有以下代碼行:
from azure.storage.blob import BlobServiceClient
我收到以下錯誤:
from azure.storage.blob import BlobServiceClient
ImportError: cannot import name 'BlobServiceClient' from 'azure.storage.blob' (/home/user-name/.local/lib/python3.7/site-packages/azure/storage/blob/__init__.py)
我在 PythonAnywhere 和我的本地機器上都有這個錯誤。 我在這里按照教程
我已經閱讀了 github 上的這篇文章,但無濟於事。
什么不見了? 我想要完成的目標是當我有一個 URI 和一個共享密鑰時訪問容器中的 blob。
感謝任何幫助。
您能否檢查pip list
並確保您擁有最新版本的 azure-storage-blob 即 >= 12.0.0?
BlobServiceClient 在 12.0.0 中引入。
如果您有舊版本,請執行pip install azure-storage-blob --upgrade
此外,azure-storage-blob v 12.0.0 及更高版本被移動到不同的存儲庫https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/storage/azure-storage-blob
就我而言,我先安裝了 azure,然后出現此錯誤。 通過執行以下步驟,它現在可以正常工作:
您可以嘗試以下方法:
from azure.storage.blob import BlockBlobService
block_blob_service = BlockBlobService(account_name='yraccountname', account_key='accountkey')
您可以找到我在 python 中編寫的示例的更多詳細信息: https://github.com/adamsmith0016/Azure-storage/blob/master/del-blob.py
還要確保基於您擁有的 pip 版本(pip 或 pip3)運行:
pip install azure-storage-blob
看來問題出在我使用的 azure-storage-blob 版本或我安裝其他 azure 庫(通用、核心等)的順序上。 我卸載了所有東西,只安裝了 azure-storage-blob 並且它工作正常。
我也遇到了這個錯誤,特別是在我部署 Azure Web 應用程序時(它在本地電腦上運行良好)。
我認為首先您應該再次檢查已安裝的軟件包列表。
就我而言,我有azure-storage
和azure-storage-blob
,這很奇怪,因為我記得只安裝了azure-storage-blob
。 我收到的信息和你一模一樣。
就像@Ivory 的回答一樣,我也卸載了它們並嘗試重新安裝。
然后我收到了pip install azure-storage
這個錯誤,
Collecting azure-storage
Using cached azure-storage-0.37.0.zip (4.3 kB)
ERROR: Command errored out with exit status 1:
command: /tmp/8da918957b4378c/antenv/bin/python3.8 -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-s_q2c_3c/azure-storage_855ccb98b522499ca9f42557f5036870/setup.py'"'"'; __file__='"'"'/tmp/pip-install-s_q2c_3c/azure-storage_855ccb98b522499ca9f42557f5036870/setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /tmp/pip-pip-egg-info-n2ov1yg9
cwd: /tmp/pip-install-s_q2c_3c/azure-storage_855ccb98b522499ca9f42557f5036870/
Complete output (19 lines):
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/tmp/pip-install-s_q2c_3c/azure-storage_855ccb98b522499ca9f42557f5036870/setup.py", line 55, in <module>
raise RuntimeError(message)
RuntimeError:
Starting with v0.37.0, the 'azure-storage' meta-package is deprecated and cannot be installed anymore.
Please install the service specific packages prefixed by `azure` needed for your application.
The complete list of available packages can be found at:
https://aka.ms/azsdk/python/all
Here's a non-exhaustive list of common packages:
- [azure-storage-blob](https://pypi.org/project/azure-storage-blob) : Blob storage client
- [azure-storage-file-share](https://pypi.org/project/azure-storage-file-share) : Storage file share client
- [azure-storage-file-datalake](https://pypi.org/project/azure-storage-file-datalake) : ADLS Gen2 client
- [azure-storage-queue](https://pypi.org/project/azure-storage-queue): Queue storage client
您可以看到有錯誤消息告知此azure-storage
已棄用。
所以你應該做的是(如果你像我一樣同時擁有azure-storage
和azure-storage-blob
):
pip uninstall azure-storage
pip uninstall azure-storage-blob
pip install azure-storage-blob
。from azure.storage.blob import BlobServiceClient
,它將完美運行。我希望這可以幫助那些偶然發現這個問題的人。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.