简体   繁体   中英

error importing 'BlobServiceClient' from 'azure.storage.blob'

I have the following line of code:

from azure.storage.blob import BlobServiceClient

I get the following error:

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)

I have this error BOTH on PythonAnywhere and on my local machine. I am following the tutorial here

I have read through this post on github but to no avail.

What is missing? The objective i am trying to accomplish is to access a blob in a container when I have a URI and a shared key.

Appreciate any help.

Can you check pip list and make sure you have the latest version of azure-storage-blob ie >= 12.0.0?

BlobServiceClient is introduced in 12.0.0.

If you have an older version, do pip install azure-storage-blob --upgrade

Also, azure-storage-blob v 12.0.0 and above is moved to a different repository https://github.com/Azure/azure-sdk-for-python/tree/master/sdk/storage/azure-storage-blob

In my case, I installed azure first and then I got this error. By doing the following steps, and it works fine now:

  1. Uninstall azure-storage-blob using: pip uninstall azure-storage-blob.
  2. Reinstall azure-storage-blob using: pip install azure-storage-blob.

Can you try the following instead:

    from azure.storage.blob import BlockBlobService
block_blob_service = BlockBlobService(account_name='yraccountname', account_key='accountkey')

You can find more details of examples i wrote in python: https://github.com/adamsmith0016/Azure-storage/blob/master/del-blob.py

Also ensure to run based on the pip version you have, either pip or pip3:

pip install azure-storage-blob

It appears the issue was either with the version of azure-storage-blob that I was using or the order in which I was installing the other azure libraries (common, core, etc). I uninstalled everything and just installed azure-storage-blob and it worked.

I also got stuck with this error especially when I was deploying Azure Web App(it worked fine at local pc).

I think first of all you should check your installed packages list again.

For my case, I had azure-storage and azure-storage-blob which was strange as I remember only installing azure-storage-blob . And I was getting exact same message as you.

like the answer from @Ivory, I also uninstalled both of them and tried installing all over again.

Then I got this error for 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

You can see that there are error message telling that this azure-storage is deprecated.

So what you should do is(If you have both azure-storage and azure-storage-blob like I did):

  1. pip uninstall azure-storage
  2. pip uninstall azure-storage-blob
  3. pip install azure-storage-blob again.
  4. Try from azure.storage.blob import BlobServiceClient again, and it would work perfectly.

I hope this can help who stumbles upon this problem.

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