azure-storage-python: cannot import name 'BlockBlobService'

Hi,

I have azure-storage-blob version 0.37.1 and still i get an error when i import BlockBlobService.

from azure.storage.blob import BlockBlobService # import azure sdk packages

Error message: ImportError: cannot import name ‘BlockBlobService’

Here is a list of existing azure storage packages in my current virtual environment:

azure-storage-blob==0.37.1 azure-storage-common==0.37.1 azure-storage-nspkg==2.0.0

How can i fix this?

Thanks.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 4
  • Comments: 42 (9 by maintainers)

Most upvoted comments

Hi @melzoghbi, I’m unable to reproduce this problem. It does not seem to be a problem with the storage package. Could you please try again with a different virtual environment? Thanks!

I think there is some dependency bug in the source code (version azure-storage-blob==2.1.0 ) of blockblobservice.py and init.py under site-packages/azure/storage/blob/ directory. Because when I type in from azure.storage.blob.blockblobservice import BlockBlobService it worked.

Ok. I’ve been able to figure out how to get BlobServiceClient and BlockBlobService running in Azure Notebooks (thanks in part to @harmonyliu!):

  • !pip uninstall azure -y
  • !pip install azure==4.0.0
  • from azure.storage.blob.blockblobservice import BlockBlobService
  • from azure.storage.blob import BlobServiceClient Hope this helps

@melzoghbi which version of Python are you using?

For me it worked after I installed azure package: pip install azure

I noticed that it uninstalls latest version (12.0) of azure-storage-blob

Now I have these: pip freeze adal==1.2.2 azure==4.0.0 azure-applicationinsights==0.1.0 azure-batch==4.1.3 azure-common==1.1.23 azure-core==1.0.0 azure-cosmosdb-nspkg==2.0.2 azure-cosmosdb-table==1.0.6 azure-datalake-store==0.0.48 azure-eventgrid==1.3.0 azure-graphrbac==0.40.0 azure-keyvault==1.1.0 azure-loganalytics==0.1.0 azure-mgmt==4.0.0 azure-mgmt-advisor==1.0.1 azure-mgmt-applicationinsights==0.1.1 azure-mgmt-authorization==0.50.0 azure-mgmt-batch==5.0.1 azure-mgmt-batchai==2.0.0 azure-mgmt-billing==0.2.0 azure-mgmt-cdn==3.1.0 azure-mgmt-cognitiveservices==3.0.0 azure-mgmt-commerce==1.0.1 azure-mgmt-compute==4.6.2 azure-mgmt-consumption==2.0.0 azure-mgmt-containerinstance==1.5.0 azure-mgmt-containerregistry==2.8.0 azure-mgmt-containerservice==4.4.0 azure-mgmt-cosmosdb==0.4.1 azure-mgmt-datafactory==0.6.0 azure-mgmt-datalake-analytics==0.6.0 azure-mgmt-datalake-nspkg==3.0.1 azure-mgmt-datalake-store==0.5.0 azure-mgmt-datamigration==1.0.0 azure-mgmt-devspaces==0.1.0 azure-mgmt-devtestlabs==2.2.0 azure-mgmt-dns==2.1.0 azure-mgmt-eventgrid==1.0.0 azure-mgmt-eventhub==2.6.0 azure-mgmt-hanaonazure==0.1.1 azure-mgmt-iotcentral==0.1.0 azure-mgmt-iothub==0.5.0 azure-mgmt-iothubprovisioningservices==0.2.0 azure-mgmt-keyvault==1.1.0 azure-mgmt-loganalytics==0.2.0 azure-mgmt-logic==3.0.0 azure-mgmt-machinelearningcompute==0.4.1 azure-mgmt-managementgroups==0.1.0 azure-mgmt-managementpartner==0.1.1 azure-mgmt-maps==0.1.0 azure-mgmt-marketplaceordering==0.1.0 azure-mgmt-media==1.0.0 azure-mgmt-monitor==0.5.2 azure-mgmt-msi==0.2.0 azure-mgmt-network==2.7.0 azure-mgmt-notificationhubs==2.1.0 azure-mgmt-nspkg==3.0.2 azure-mgmt-policyinsights==0.1.0 azure-mgmt-powerbiembedded==2.0.0 azure-mgmt-rdbms==1.9.0 azure-mgmt-recoveryservices==0.3.0 azure-mgmt-recoveryservicesbackup==0.3.0 azure-mgmt-redis==5.0.0 azure-mgmt-relay==0.1.0 azure-mgmt-reservations==0.2.1 azure-mgmt-resource==2.2.0 azure-mgmt-scheduler==2.0.0 azure-mgmt-search==2.1.0 azure-mgmt-servicebus==0.5.3 azure-mgmt-servicefabric==0.2.0 azure-mgmt-signalr==0.1.1 azure-mgmt-sql==0.9.1 azure-mgmt-storage==2.0.0 azure-mgmt-subscription==0.2.0 azure-mgmt-trafficmanager==0.50.0 azure-mgmt-web==0.35.0 azure-nspkg==3.0.2 azure-servicebus==0.21.1 azure-servicefabric==6.3.0.0 azure-servicemanagement-legacy==0.20.6 azure-storage-blob==1.5.0 azure-storage-common==1.4.2 azure-storage-file==1.4.0 azure-storage-queue==1.4.0 certifi==2019.9.11 cffi==1.13.2 chardet==3.0.4 cryptography==2.8 idna==2.8 isodate==0.6.0 msrest==0.6.10 msrestazure==0.6.2 oauthlib==3.1.0 pycparser==2.19 PyJWT==1.7.1 python-dateutil==2.8.1 requests==2.22.0 requests-oauthlib==1.3.0 six==1.13.0 urllib3==1.25.7

Hi @courtenayparserr probably you are using the most recent package? If the package version is 12.0.0 then try to create a new virtual environment and do: pip install azure-storage-blob==2.1.0 You will be able to import BlockBlobService

Works perfectly fine for me:

virtualenv venv
source venv/bin/activate
pip install azure-storage-blob==0.37.1
python
from azure.storage.blob import BlockBlobService

pip freeze output

asn1crypto==0.24.0
azure-common==1.1.16
azure-nspkg==3.0.2
azure-storage-blob==0.37.1
azure-storage-common==0.37.1
azure-storage-nspkg==3.0.0
certifi==2018.10.15
cffi==1.11.5
chardet==3.0.4
cryptography==2.3.1
enum34==1.1.6
futures==3.2.0
idna==2.7
ipaddress==1.0.22
pycparser==2.19
python-dateutil==2.7.3
requests==2.19.1
six==1.11.0
urllib3==1.23

python --version output

Python 2.7.10

I’d suggest updating to a newer version and increasing the logging verbosity for diagnosis. As @zezha-msft said, it sounds like something with your overall system setup isn’t correct…

Hi @harmonyliu to use BlockBlobService in azure-storage-blob==2.1.0 make sure you have a clean virtual environment(It’s highly recommended to create a new one) and just do pip install azure-storage-blob==2.1.0 No extra command is needed.

Let me know if it’s still not working

That didn’t work for me. Still getting this issue.

image

How do we move past this?

For me it was just

pip install azure-storage-blob==2.1.0

Hi @dxkaufman Thanks for reaching out. There’s no BlockBlobService in version>=12.0.0. Please see the wiki https://github.com/Azure/azure-storage-python/wiki#frequently-encountered-problem-no-module-named-azurestorageblob

To get text content of blob in v12.3.2, you should do the following thing

        bsc = BlobServiceClient(
            self.account_url(storage_account, "blob"),
            credential=account_key)

        blob_client = bsc.get_blob_client(your_container_name, your_blob_name)

        blob_client.upload_blob(text_data)

        # Act
        stream = blob_client.download_blob(max_concurrency=2, encoding='UTF-8')
        content = stream.readall()  # this will give you the text content

@SeaDude I think that solution doesn’t work anymore, I’ve tried quite a few times with every possible solution.

I need to know if anyone is able to run BlobServiceClient and BlockBlobService together.

Wah, I'm a ranting baby here. My apologies!

This is very confusing…

  • Per @lmazuel in this thread two days ago, azure-storage is an incompatible old version of azure-storage-blob. You cannot install them at the same time, and that will create terrible conflicts.”

  • This directly conflicts with @yashpatel7025 's “solution” above.

  • As outlined in the thread with @lmazuel, I could NOT install BlobServiceClient.

    • To get it working, I had to uninstall azure-storage then !pip install -U azure-storage-blob to fix the problem.
  • NOW, I can’t install BlockBlobService which exhibits nearly the same error as I had with BlobServiceClient from the other day!

  • I’m trying to use the elegant code found here to directly download large binary files from the web straight to a blob.

  • I’m using Azure Notebooks and Azure Databricks so no, I can’t complicate things with using venv's. These are basically hosted Jupyter notebooks without access to the OS which hosts them.

import os, uuid, json
from azure.identity import ClientSecretCredential
from azure.common.credentials import ServicePrincipalCredentials
from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient, BlockBlobService
from azure.mgmt.resource import ResourceManagementClient
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-63-d1e5e411e620> in <module>
      2 from azure.identity import ClientSecretCredential
      3 from azure.common.credentials import ServicePrincipalCredentials
----> 4 from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient, BlockBlobService
      5 from azure.mgmt.resource import ResourceManagementClient

ImportError: cannot import name 'BlockBlobService'
  • !pip freeze azure packages:
azure==4.0.0
azure-applicationinsights==0.1.0
azure-batch==4.1.3
azure-cli-core==2.0.54
azure-cli-nspkg==3.0.4
azure-cli-telemetry==1.0.4
azure-common==1.1.23
azure-core==1.4.0
azure-cosmosdb-nspkg==2.0.2
azure-cosmosdb-table==1.0.6
azure-datalake-store==0.0.48
azure-eventgrid==1.3.0
azure-graphrbac==0.40.0
azure-identity==1.3.1
azure-keyvault==1.1.0
azure-kusto-data==0.0.38
azure-kusto-ingest==0.0.38
azure-loganalytics==0.1.0
azure-mgmt==4.0.0
azure-mgmt-advisor==1.0.1
azure-mgmt-applicationinsights==0.1.1
azure-mgmt-authorization==0.50.0
azure-mgmt-batch==5.0.1
azure-mgmt-batchai==2.0.0
azure-mgmt-billing==0.2.0
azure-mgmt-cdn==3.1.0
azure-mgmt-cognitiveservices==3.0.0
azure-mgmt-commerce==1.0.1
azure-mgmt-compute==4.6.2
azure-mgmt-consumption==2.0.0
azure-mgmt-containerinstance==1.5.0
azure-mgmt-containerregistry==2.8.0
azure-mgmt-containerservice==4.4.0
azure-mgmt-cosmosdb==0.4.1
azure-mgmt-datafactory==0.6.0
azure-mgmt-datalake-analytics==0.6.0
azure-mgmt-datalake-nspkg==3.0.1
azure-mgmt-datalake-store==0.5.0
azure-mgmt-datamigration==1.0.0
azure-mgmt-devspaces==0.1.0
azure-mgmt-devtestlabs==2.2.0
azure-mgmt-dns==2.1.0
azure-mgmt-eventgrid==1.0.0
azure-mgmt-eventhub==2.6.0
azure-mgmt-hanaonazure==0.1.1
azure-mgmt-iotcentral==0.1.0
azure-mgmt-iothub==0.5.0
azure-mgmt-iothubprovisioningservices==0.2.0
azure-mgmt-keyvault==1.1.0
azure-mgmt-loganalytics==0.2.0
azure-mgmt-logic==3.0.0
azure-mgmt-machinelearningcompute==0.4.1
azure-mgmt-managementgroups==0.1.0
azure-mgmt-managementpartner==0.1.1
azure-mgmt-maps==0.1.0
azure-mgmt-marketplaceordering==0.1.0
azure-mgmt-media==1.0.0
azure-mgmt-monitor==0.5.2
azure-mgmt-msi==0.2.0
azure-mgmt-network==2.7.0
azure-mgmt-notificationhubs==2.1.0
azure-mgmt-nspkg==3.0.2
azure-mgmt-policyinsights==0.1.0
azure-mgmt-powerbiembedded==2.0.0
azure-mgmt-rdbms==1.9.0
azure-mgmt-recoveryservices==0.3.0
azure-mgmt-recoveryservicesbackup==0.3.0
azure-mgmt-redis==5.0.0
azure-mgmt-relay==0.1.0
azure-mgmt-reservations==0.2.1
azure-mgmt-resource==2.0.0
azure-mgmt-scheduler==2.0.0
azure-mgmt-search==2.1.0
azure-mgmt-servicebus==0.5.3
azure-mgmt-servicefabric==0.2.0
azure-mgmt-signalr==0.1.1
azure-mgmt-sql==0.9.1
azure-mgmt-storage==2.0.0
azure-mgmt-subscription==0.2.0
azure-mgmt-trafficmanager==0.50.0
azure-mgmt-web==0.35.0
azure-nspkg==3.0.2
azure-servicebus==0.21.1
azure-servicefabric==6.3.0.0
azure-servicemanagement-legacy==0.20.6
azure-storage-blob==12.3.0
azure-storage-common==2.1.0
azure-storage-file==1.4.0
azure-storage-queue==2.1.0
azureml-automl-core==1.0.72.1
azureml-contrib-notebook==1.0.72
azureml-core==1.0.72
azureml-dataprep==1.1.30
azureml-dataprep-native==13.1.0
azureml-defaults==1.0.72
azureml-explain-model==1.0.72
azureml-interpret==1.0.72.1
azureml-model-management-sdk==1.0.1b6.post1
azureml-opendatasets==1.0.72.1
azureml-pipeline==1.0.72
azureml-pipeline-core==1.0.72
azureml-pipeline-steps==1.0.72
azureml-sdk==1.0.72
azureml-telemetry==1.0.72
azureml-train==1.0.72
azureml-train-automl==1.0.72
azureml-train-core==1.0.72
azureml-train-restclients-hyperdrive==1.0.72
  • There is a new message when trying to download !pip install azure saying something to the effect of “After vX.0 of azure, downloading all packages at once is deprecated. Please download packages individually”, we really need these dependencies fixed.
  • One of the goals of using python within notebooks is the concept of reproducability. Its evident from this string and others around the web that simply importing azure modules at this time is not reproducable.
  • Lets get this fixed once and for all. What can I do to help test?

Hi @harmonyliu to use BlockBlobService in azure-storage-blob==2.1.0 make sure you have a clean virtual environment(It’s highly recommended to create a new one) and just do pip install azure-storage-blob==2.1.0 No extra command is needed.

Let me know if it’s still not working

this worked for me when I was dealing with BlockBlobService import issues

This works for me

pip install azure-storage --upgrade pip install azure-storage-blob --upgrade pip install azure --upgrade

after this pip freeze gives

azure==4.0.0 azure-storage==0.36.0 azure-storage-blob==1.5.0

now I am able to run following from azure.storage.blob import BlockBlobService, PublicAccess

Your code might still be using the legacy Python BlobSDK (https://docs.microsoft.com/en-us/azure/storage/blobs/storage-quickstart-blobs-python-legacy) But you might have update the library to the latest version. Use this SDK now to update your code https://docs.microsoft.com/en-us/azure/storage/blobs/storage-quickstart-blobs-python

This works for me

pip install azure-storage --upgrade pip install azure-storage-blob --upgrade pip install azure --upgrade

after this pip freeze gives

azure==4.0.0 azure-storage==0.36.0 azure-storage-blob==1.5.0

now I am able to run following from azure.storage.blob import BlockBlobService, PublicAccess

you are a legend, worked for me. thanks!

@xiafu-msft,

Thank you for the example of how to use BlobServiceClient to get a BlobClient. I now see this option listed in the SDK docs here.

An alternative is to instantiate a separate BlobClient using a SAS. Not sure if its any easier, but the way I did it was:

blob_client = BlobClient.from_blob_url(blob_url = sas_token)

with open("./sampleFile.txt", "rb") as data:
    blob_client.upload_blob(data)

Hi @xiafu-msft Thanks for the quick response. I was starting to get that idea, although I was looking at SDK documentation that seemed to indicate it was still part of the model. I think the docs aren’t super clear about what version they relate to.

I appreciate the sample. All the samples I was looking at last night seem to create a container before accessing it, even when the description is just for reading a blob. I found that a little confusing, though I surmised that the assumption was that the container didn’t exist yet, and your example seems to make that clearer.

Dylan

@vipulgupta2048 I just left the old version (v2.1) altogether and implemented everything on (v12.0) 😃