使用Poetry安装torch/tensorflow/transformers遇WinError126问题求助
解决Poetry安装PyTorch/TensorFlow/Transformers后出现WinError 126的问题
1. 确认环境兼容性
- 检查Poetry虚拟环境的Python版本和系统架构:
poetry run python --version poetry run python -c "import platform; print(platform.architecture())" - 确保Python版本符合目标包要求(比如PyTorch 2.x要求Python 3.8-3.11,且必须是64位环境)
2. 为PyTorch指定官方安装源
WinError 126常因Poetry拉取的轮子不兼容Windows导致,修改pyproject.toml添加专属源:
[tool.poetry.dependencies] python = "^3.10" # 替换为你的兼容版本 torch = { version = "2.0.1", source = "pytorch" } [[tool.poetry.source]] name = "pytorch" url = "https://download.pytorch.org/whl/cu118" # 无CUDA则用https://download.pytorch.org/whl/cpu secondary = true
执行以下命令更新依赖:
poetry lock --no-update poetry install
3. 重建Poetry虚拟环境
虚拟环境损坏可能导致模块链接失效,执行:
poetry env remove python poetry install
注意安装时关闭杀毒软件,避免动态链接库(DLL)被误拦截。
4. 手动通过pip安装到Poetry环境
进入Poetry shell后,用官方pip命令安装PyTorch:
poetry shell # CPU版 pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu # CUDA 11.8版 pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118
安装完成后执行poetry add torch,将依赖写入pyproject.toml。
5. 验证安装并修复DLL依赖
- 进入Poetry shell后运行验证命令:
python -c "import torch; print(torch.__version__); print(torch.cuda.is_available())" - 若仍报错,安装Microsoft Visual C++ Redistributable for Visual Studio 2022(PyTorch等包依赖该运行库的DLL文件)
内容的提问来源于stack exchange,提问作者Godefroy Lambert
相关产品推荐
相关产品推荐

