如何通过Conda在Python 3.10.*环境中安装TensorFlow
默认defaults频道推送的TensorFlow构建版本适配进度滞后,最高仅支持到Python 3.9,才会触发你看到的版本范围不兼容报错,并非Python 3.10无法通过Conda安装TensorFlow,按以下步骤操作即可正常安装:
- 创建独立的Python 3.10环境,避免全局包依赖冲突
conda create -n tf-py310 python=3.10 -y - 激活新建的虚拟环境
conda activate tf-py310 - 指定从conda-forge频道安装TensorFlow,该频道维护的TensorFlow构建包早已完成Python 3.10适配,自带全量运行依赖
conda install -c conda-forge tensorflow -y
安装提示:
如果你的Conda配置了defaults频道优先级高于conda-forge,执行安装命令时追加--strict-channel-priority参数,强制优先读取conda-forge的包版本,避免再次触发依赖冲突,完整命令为conda install -c conda-forge --strict-channel-priority tensorflow -y
如果需要安装GPU版本TensorFlow,不需要额外手动安装系统级CUDA、cuDNN,conda-forge的TensorFlow包会自动匹配安装对应版本的GPU依赖,开箱即用。如果需要指定TensorFlow版本,直接在包名后追加版本号即可,例如安装2.10版本的命令为conda install -c conda-forge tensorflow=2.10 -y
如果觉得原生Conda依赖解析速度过慢,可以先在base环境安装mamba加速解析:conda install -n base mamba -c conda-forge -y,后续安装命令把conda替换为mamba即可,解析速度能提升数倍。
安装完成后可以在环境内执行python -c "import tensorflow as tf; print(tf.__version__)"验证安装结果,正常输出版本号即代表安装成功。
内容的提问来源于stack exchange,提问作者g.pickardou

