Databricks报错ModuleNotFoundError: No module named 'azure.storage'求助
解决Azure Databricks中
azure.storage模块找不到的问题 问题重现
参考Stack Overflow回答编写的SAS令牌生成代码:
# Create a client of Azure File Service as same as yours from azure.storage.file import FileService account_name = '<your account name>' account_key = '<your account key>' share_name = 'test' directory_name = None file_name = 'test.xlsx' file_service = FileService(account_name=account_name, account_key=account_key)
运行时触发报错:
ModuleNotFoundError: No module named 'azure.storage'
已尝试操作
- 执行pip命令安装相关模块:
!pip install azure !pip install azure-storage !pip install azure-storage-file - 在Databricks集群中安装了相同模块
可行解决办法
1. 适配Azure存储SDK版本差异
旧版azure-storage-file(v12之前)的导入路径是azure.storage.file,但新版SDK(v12+)已重构为azure.storage.fileshare,且模块名称改为azure-storage-file-share。
- 若要沿用原有代码逻辑,强制安装旧版兼容包:
!pip uninstall -y azure azure-storage azure-storage-file !pip install azure-storage-file==2.1.0 - 若升级到新版SDK,修改代码并安装对应包:
新版代码示例:!pip install azure-storage-file-sharefrom azure.storage.fileshare import ShareServiceClient account_name = '<your account name>' account_key = '<your account key>' share_name = 'test' file_name = 'test.xlsx' # 初始化服务客户端 share_service_client = ShareServiceClient( account_url=f"https://{account_name}.file.core.windows.net", credential=account_key ) # 生成SAS令牌示例 share_client = share_service_client.get_share_client(share_name) sas_token = share_client.generate_shared_access_signature( permission="r", expiry=datetime.utcnow() + timedelta(hours=1) )
2. 确保集群模块生效
安装模块后重启Databricks集群,新安装的依赖需要重启才能被运行环境加载。
3. 规避版本冲突
部分Databricks Runtime版本自带Azure SDK组件,可能与手动安装的包冲突:
- 检查Runtime版本,若自带高版本SDK,建议直接使用新版SDK的代码逻辑
- 若必须用旧版,可在集群初始化脚本中指定安装旧版包,覆盖默认组件
4. 使用Databricks内置挂载方式(替代SAS生成)
无需手动生成SAS,直接挂载Azure Files到Databricks文件系统:
dbutils.fs.mount( source = f"wasbs://{share_name}@{account_name}.file.core.windows.net", mount_point = "/mnt/azurefiles", extra_configs = {f"fs.azure.account.key.{account_name}.file.core.windows.net": account_key} )
挂载后直接访问文件:
import pandas as pd df = pd.read_excel("/dbfs/mnt/azurefiles/test.xlsx")
内容的提问来源于stack exchange,提问作者mxmlnlrcn
相关产品推荐
相关产品推荐

