TensorFlow/PyTorch安装失败求助:Python3.12环境适配问题
我正尝试安装TensorFlow或PyTorch,以便使用Hugging Face的Transformers库。在常规项目和虚拟环境中都进行了尝试,使用多种pip命令并测试不同版本,也查阅了相关文档,执行的命令包括:
pip install torch pip3 install torch --no-cache-dir pip install "tensorflow<2.11" pip install tensorflow==2.0 pip install tensorflow --user pip3 install tensorflow python3 -m pip install --upgrade https://storage.googleapis.com/tensorflow/windows/cpu/tensorflow_cpu-2.10.0-cp310-cp310-win_amd64.whl py -m pip install torch py -m pip install tensorflow --upgrade
还进行了其他多种尝试。
当前环境版本
Python 3.12.0 pip 23.3.1
遇到的报错
ERROR: Could not find a version that satisfies the requirement torch (from versions: none)
ERROR: No matching distribution found for torch
ERROR: Could not find a version that satisfies the requirement tensorflow2.10.0 (from versions: none)
ERROR: No matching distribution found for tensorflow2.10.0
ERROR: tensorflow_cpu-2.10.0-cp310-cp310-win_amd64.whl is not a supported wheel on this platform.
ERROR: Could not find a version that satisfies the requirement tensorflow (from versions: none)
ERROR: Can not perform a '--user' install. User site-packages are not visible in this virtualenv.
ERROR: Could not find a version that satisfies the requirement torch2.0.1 (from versions: none)
ERROR: No matching distribution found for torch2.0.1
ERROR: No matching distribution found for tensorflow
ERROR: Could not find a version that satisfies the requirement tensorflow[and-cuda] (from versions: none)
ERROR: No matching distribution found for tensorflow[and-cuda]
ERROR: Could not find a version that satisfies the requirement tensorflow (from versions: none)
ERROR: No matching distribution found for tensorflow
此外还触发了RuntimeError,提示需安装TensorFlow 2.0或PyTorch之一。在虚拟环境内外均进行过尝试,现寻求解决办法。
解决办法
核心原因
Python 3.12是较新版本,早期的TensorFlow、PyTorch未适配该版本的安装包(wheel),导致pip找不到匹配的版本。
具体方案
1. 降级Python版本(最稳妥)
- 安装Python 3.10或3.11(这两个版本是TensorFlow和PyTorch官方长期支持的稳定版本)
- 创建并激活对应版本的虚拟环境:
# 以Python3.10为例 py -3.10 -m venv transformers-env # Windows激活 transformers-env\Scripts\activate # Linux/macOS激活 source transformers-env/bin/activate - 激活后直接安装:
- PyTorch:
pip install torch torchvision torchaudio - TensorFlow:
pip install tensorflow
- PyTorch:
2. 安装支持Python3.12的最新版本
- PyTorch 2.1.0及以上支持Python3.12,执行对应命令安装:
# CPU版 pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu # GPU版(需提前安装CUDA 12.1) pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121 - TensorFlow 2.16.0及以上支持Python3.12,安装命令:
pip install tensorflow>=2.16.0
3. 修复虚拟环境--user参数错误
在虚拟环境中安装包时,不要加--user参数,虚拟环境本身是隔离的,直接用pip install <包名>即可,该参数仅用于全局Python环境的用户目录安装。
内容的提问来源于stack exchange,提问作者Bigbear

