安装super-gradients报错:无法从treelib导入__version__
安装super-gradients(YOLO-NAS依赖)时treelib相关报错的解决
环境信息
- 系统:Windows
- Spyder版本:5.4.3
- Python版本:3.8.10
问题现象
执行pip install super-gradients时触发如下错误:
ImportError: cannot import name '__version__' from 'treelib'
完整报错回溯:
pip install super-gradients Defaulting to user installation because normal site-packages is not writeable Collecting super-gradients Using cached super_gradients-3.1.2-py3-none-any.whl (983 kB) Requirement already satisfied: torch>=1.9.0 in c:\users\xxx\appdata\roaming\python\python38\site-packages (from super-gradients) (1.9.0) Requirement already satisfied: tqdm>=4.57.0 in c:\users\xxx\appdata\roaming\python\python38\site-packages (from super-gradients) (4.65.0) Collecting boto3>=1.17.15 (from super-gradients) Using cached boto3-1.28.1-py3-none-any.whl (135 kB) Requirement already satisfied: jsonschema>=3.2.0 in c:\program files\spyder\pkgs (from super-gradients) (4.17.3) Collecting Deprecated>=1.2.11 (from super-gradients) Using cached Deprecated-1.2.14-py2.py3-none-any.whl (9.6 kB) Requirement already satisfied: opencv-python>=4.5.1 in c:\users\xxx\appdata\roaming\python\python38\site-packages (from super-gradients) (4.5.5.64) Requirement already satisfied: scipy>=1.6.1 in c:\program files\spyder\pkgs (from super-gradients) (1.10.0) Requirement already satisfied: matplotlib>=3.3.4 in c:\program files\spyder\pkgs (from super-gradients) (3.6.3) Requirement already satisfied: psutil>=5.8.0 in c:\users\xxx\appdata\roaming\python\python38\site-packages (from super-gradients) (5.9.5) Collecting tensorboard>=2.4.1 (from super-gradients) Using cached tensorboard-2.13.0-py3-none-any.whl (5.6 MB) Requirement already satisfied: setuptools>=21.0.0 in c:\program files\spyder\pkgs (from super-gradients) (66.0.0) Collecting coverage~=5.3.1 (from super-gradients) Using cached coverage-5.3.1-cp38-cp38-win_amd64.whl (212 kB) Requirement already satisfied: torchvision>=0.10.0 in c:\users\xxx\appdata\roaming\python\python38\site-packages (from super-gradients) (0.14.1) Collecting sphinx~=4.0.2 (from super-gradients) Using cached Sphinx-4.0.3-py3-none-any.whl (2.9 MB) Collecting sphinx-rtd-theme (from super-gradients) Using cached sphinx_rtd_theme-1.2.2-py2.py3-none-any.whl (2.8 MB) Collecting torchmetrics==0.8 (from super-gradients) Using cached torchmetrics-0.8.0-py3-none-any.whl (408 kB) Collecting hydra-core>=1.2.0 (from super-gradients) Using cached hydra_core-1.3.2-py3-none-any.whl (154 kB) Collecting omegaconf (from super-gradients) Using cached omegaconf-2.3.0-py3-none-any.whl (79 kB) Collecting onnxruntime==1.13.1 (from super-gradients) Using cached onnxruntime-1.13.1-cp38-cp38-win_amd64.whl (5.9 MB) Collecting onnx==1.13.0 (from super-gradients) Using cached onnx-1.13.0-cp38-cp38-win_amd64.whl (12.2 MB) Requirement already satisfied: pillow!=8.3,>=5.3.0 in c:\program files\spyder\pkgs (from super-gradients) (9.4.0) Collecting pip-tools>=6.12.1 (from super-gradients) Using cached pip_tools-6.14.0-py3-none-any.whl (55 kB) Collecting pyparsing==2.4.5 (from super-gradients) Using cached pyparsing-2.4.5-py2.py3-none-any.whl (67 kB) Collecting einops==0.3.2 (from super-gradients) Using cached einops-0.3.2-py3-none-any.whl (25 kB) Collecting pycocotools==2.0.6 (from super-gradients) Using cached pycocotools-2.0.6.tar.gz (24 kB) Installing build dependencies: started Installing build dependencies: finished with status 'done' Getting requirements to build wheel: started Getting requirements to build wheel: finished with status 'done' Preparing metadata (pyproject.toml): started Preparing metadata (pyproject.toml): finished with status 'done' Requirement already satisfied: protobuf==3.20.3 in c:\users\xxx\appdata\roaming\python\python38\site-packages (from super-gradients) (3.20.3) Collecting treelib==1.6.1 (from super-gradients) Using cached treelib-1.6.1.tar.gz (24 kB) Preparing metadata (setup.py): started Preparing metadata (setup.py): finished with status 'error' Note: you may need to restart the kernel to use updated packages. 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 "C:\Users\xxx\AppData\Local\Temp\pip-install-_eldz5ej\treelib_f0d5ee2532f0448da840362e00ef7cc2\setup.py", line 3, in <module> from treelib import __version__ ImportError: cannot import name '__version__' from 'treelib' (C:\Users\xxx\AppData\Roaming\Python\Python38\site-packages\treelib\__init__.py) [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.
执行pip show treelib确认本地已安装treelib 1.6.4:
pip show treelib Version: 1.6.4
但测试代码无法访问版本属性:
import treelib print(treelib.__version__)
报错:
File c:\data\objectrecognition\test2.py:11 print(treelib.__version__) AttributeError: module 'treelib' has no attribute '__version__'
问题原因
- super-gradients 3.1.2明确依赖treelib==1.6.1,但本地已安装更高版本的treelib 1.6.4
- treelib 1.6.4的代码结构调整,移除了
__version__直接导出的属性;而treelib 1.6.1的setup.py依赖从包内导入该属性,pip安装时会优先读取本地已安装的高版本treelib,导致导入失败
解决方法
方法1:卸载现有版本,安装指定依赖版本
依次执行以下命令:
pip uninstall -y treelib pip install treelib==1.6.1 pip install super-gradients
方法2:使用虚拟环境隔离依赖(推荐)
- 创建并激活虚拟环境:
python -m venv sg_env sg_env\Scripts\activate
- 在虚拟环境内直接安装super-gradients:
pip install super-gradients
方法3:强制重新安装(不推荐,可能引发其他依赖冲突)
pip install super-gradients --force-reinstall
内容的提问来源于stack exchange,提问作者bitterjam
相关产品推荐
相关产品推荐

