Streamlit Cloud部署应用时安装torch_sparse遇torch模块缺失错误求助
Streamlit Cloud部署中torch_sparse安装失败的解决方法
问题描述
部署应用时,torch_sparse==0.6.16安装报错,错误信息显示ModuleNotFoundError: No module named 'torch'。尽管requirements.txt中已包含torch==1.8.1,且日志显示pip先下载了torch,但在生成torch_sparse的metadata时,系统仍找不到torch模块。
错误日志
Collecting torch==1.8.1 Downloading torch-1.8.1-cp39-cp39-manylinux1_x86_64.whl (804.1 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 804.1/804.1 MB 147.0 MB/s eta 0:00:00[2023-02-23 12:32:59.564581] Collecting matplotlib==3.5.2 Downloading matplotlib-3.5.2-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.whl (11.2 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 11.2/11.2 MB 90.2 MB/s eta 0:00:00 Collecting translate==3.6.1 Collecting torch_sparse==0.6.16 Downloading torch_sparse-0.6.16.tar.gz (208 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 208.2/208.2 KB 285.3 MB/s eta 0:00:00[2023-02-23 03:52:24.740968] Preparing metadata (setup.py): started Preparing metadata (setup.py): finished with status 'error' error: subprocess-exited-with-error × python setup.py egg_info did not run successfully. │ exit code: 1 ╰─> [6 lines of output] Traceback (most recent call last): File "<string>", line 2, in <module> File "<pip-setuptools-caller>", line 34, in <module> File "/tmp/pip-install-6ook3r5m/torch-sparse_a4c09b3da42046a5a246b2ccd6433d71/setup.py", line 8, in <module> import torch ModuleNotFoundError: No module named 'torch' [end of output] note: This error originates from a subprocess, and is likely not a problem with pip. error: metadata-generation-failed × Encountered error while generating package metadata. ╰─> See above for output. note: This is an issue with the package mentioned above, not pip. hint: See above for details.
当前requirements.txt内容
torch==1.8.1 matplotlib==3.5.2 translate==3.6.1 numpy==1.21.6 pandas==1.3.5 streamlit==1.15.1 plotly==5.11.0 scipy==1.7.3 networkx==2.6.3 rdkit==2022.9.2 requests==2.28.1 Pillow==9.4.0 scikit_learn==1.2.1 torch_sparse==0.6.16 torch_geometric==2.1.0.post1
解决方案
方案1:使用预编译wheel包(推荐)
PyTorch Geometric提供了对应不同torch版本的预编译wheel,避免从源码编译时依赖torch。修改requirements.txt,在开头添加对应torch版本的索引源:
--extra-index-url https://data.pyg.org/whl/torch-1.8.0+cu111.html torch==1.8.1 matplotlib==3.5.2 translate==3.6.1 numpy==1.21.6 pandas==1.3.5 streamlit==1.15.1 plotly==5.11.0 scipy==1.7.3 networkx==2.6.3 rdkit==2022.9.2 requests==2.28.1 Pillow==9.4.0 scikit_learn==1.2.1 torch_sparse==0.6.16 torch_geometric==2.1.0.post1
注意:索引地址需匹配你的torch版本,比如torch-1.8.0+cu111对应torch 1.8.x版本+CUDA 11.1,若使用CPU版本则替换为torch-1.8.0+cpu。
方案2:使用安装脚本强制安装顺序
创建setup.sh文件,手动指定安装顺序,确保torch完全安装后再安装依赖它的包:
pip install torch==1.8.1 pip install torch_sparse==0.6.16 torch_geometric==2.1.0.post1 pip install -r requirements.txt
然后创建.streamlit/config.toml文件,禁用自动依赖安装,让Streamlit执行自定义脚本:
[server] install_dependencies = false
将这两个文件上传到GitHub仓库,Streamlit Cloud会优先执行setup.sh完成安装。
方案3:禁用构建隔离
在requirements.txt中给torch_sparse添加--no-build-isolation参数,避免pip在隔离环境中编译该包:
torch==1.8.1 torch_sparse==0.6.16 --no-build-isolation torch_geometric==2.1.0.post1 # 其他依赖...
内容的提问来源于stack exchange,提问作者cuger
相关产品推荐
相关产品推荐

