使用 pip 安装 TensorFlow
2025-12-09 10:21:19
TensorFlow 2 软件包现已推出
tensorflow:支持 CPU 和 GPU 的最新稳定版(适用于 Ubuntu 和 Windows)
tf-nightly:预览 build(不稳定)。Ubuntu 和 Windows 均包含 GPU 支持。
旧版 TensorFlow
对于 TensorFlow 1.x,CPU 和 GPU 软件包是分开的:
tensorflow==1.15:仅支持 CPU 的版本
tensorflow-gpu==1.15:支持 GPU 的版本(适用于 Ubuntu 和 Windows)
系统要求
Python 3.6–3.9
若要支持 Python 3.9,需要使用 TensorFlow 2.5 或更高版本。
若要支持 Python 3.8,需要使用 TensorFlow 2.2 或更高版本。
pip 19.0 或更高版本(需要 manylinux2010 支持)
Ubuntu 16.04 或更高版本(64 位)
macOS 10.12.6 (Sierra) 或更高版本(64 位)(不支持 GPU)
macOS 要求使用 pip 20.3 或更高版本
Windows 7 或更高版本(64 位)
适用于 Visual Studio 2015、2017 和 2019 的 Microsoft Visual C++ 可再发行软件包
GPU 支持需要使用支持 CUDA® 的卡(适用于 Ubuntu 和 Windows)
注意:必须使用最新版本的 pip,才能安装 TensorFlow 2。
硬件要求
从 TensorFlow 1.6 开始,二进制文件使用 AVX 指令,这些指令可能无法在旧版 CPU 上运行。
阅读 GPU 支持指南,以在 Ubuntu 或 Windows 上设置支持 CUDA® 的 GPU 卡。
1. 在系统上安装 Python 开发环境
检查是否已配置 Python 环境:
需要使用 Python 3.6-3.9 和 pip 19.0 及更高版本
python3 --version
pip3 --version
如果已安装这些软件包,请跳至下一步。
否则,请安装 Python、pip 软件包管理器和 venv:
Ubuntu
sudo apt update
sudo apt install python3-dev python3-pip python3-venv
macOS
使用 Homebrew 软件包管理器安装:
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
export PATH="/usr/local/opt/python/libexec/bin:$PATH"
# if you are on macOS 10.12 (Sierra) use `export PATH="/usr/local/bin:/usr/local/sbin:$PATH"`
brew update
brew install python # Python 3
Windows
安装适用于 Visual Studio 2015、2017 和 2019 的 Microsoft Visual C++ 可再发行软件包。从 TensorFlow 2.1.0 版开始,此软件包需要 msvcp140_1.dll 文件(旧版可再发行软件包可能不提供此文件)。该可再发行软件包随附在 Visual Studio 2019 中,但可以单独安装:
转到 Microsoft Visual C++ 下载页面。
在页面中向下滚动到“Visual Studio 2015、2017 和 2019”部分。
为您的平台下载并安装适用于 Visual Studio 2015、2017 和 2019 的 Microsoft Visual C++ 可再发行软件包。
确保在 Windows 上启用了长路径。
安装 64 位适用于 Windows 的 Python 3 版本(选择 pip 作为可选功能)。
其他
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python get-pip.py
注意:升级系统 pip 可能会导致问题。
如果不是在虚拟环境中,请针对下面的命令使用 python3 -m pip。这样可以确保您升级并使用 Python pip,而不是系统 pip。
2.创建虚拟环境(推荐)
Python 虚拟环境用于将软件包安装与系统隔离开来。
Ubuntu/macOS
创建一个新的虚拟环境,方法是选择 Python 解释器并创建一个 ./venv 目录来存放它:
python3 -m venv --system-site-packages ./venv
使用特定于 shell 的命令激活该虚拟环境:
source ./venv/bin/activate # sh, bash, or zsh
. ./venv/bin/activate.fish # fish
source ./venv/bin/activate.csh # csh or tcsh
当虚拟环境处于有效状态时,shell 提示符带有 (venv) 前缀。
在不影响主机系统设置的情况下,在虚拟环境中安装软件包。首先升级 pip:
pip install --upgrade pip
pip list # show packages installed within the virtual environment
之后退出虚拟环境:
deactivate # don't exit until you're done using TensorFlow
Windows
创建一个新的虚拟环境,方法是选择 Python 解释器并创建一个 .\venv 目录来存放它:
python -m venv --system-site-packages .\venv
激活虚拟环境:
.\venv\Scripts\activate
在不影响主机系统设置的情况下,在虚拟环境中安装软件包。首先升级 pip:
pip install --upgrade pip
pip list # show packages installed within the virtual environment
之后退出虚拟环境:
deactivate # don't exit until you're done using TensorFlow
Conda
虽然我们建议使用 TensorFlow 提供的 pip 软件包,但也可以使用由社区提供支持的 Anaconda 软件包。如需安装软件包,请阅读 Anaconda TensorFlow 指南。
3.安装 TensorFlow pip 软件包
请从 PyPI 中选择以下某个 TensorFlow 软件包进行安装:
tensorflow:支持 CPU 和 GPU 的最新稳定版(适用于 Ubuntu 和 Windows)。
tf-nightly:预览 build(不稳定)。Ubuntu 和 Windows 均包含 GPU 支持。
tensorflow==1.15:TensorFlow 1.x 的最终版本。
系统会自动安装软件包依赖项。这些依赖项就列在 setup.py 文件的 REQUIRED_PACKAGES 下。
虚拟环境安装
pip install --upgrade tensorflow
验证安装效果:
python -c "import tensorflow as tf;print(tf.reduce_sum(tf.random.normal([1000, 1000])))"
系统安装
pip3 install --user --upgrade tensorflow # install in $HOME
验证安装效果:
python3 -c "import tensorflow as tf; print(tf.reduce_sum(tf.random.normal([1000, 1000])))"
成功:如果系统返回了张量,则意味着您已成功安装 TensorFlow。请查看教程开始使用。
软件包位置
部分安装方式需要您提供 TensorFlow Python 软件包的网址。您需要根据 Python 版本指定网址。
版本网址
Linux
Python 3.6(支持 GPU)
https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow_gpu-2.6.0-cp36-cp36m-manylinux2010_x86_64.whl
Python 3.6(仅支持 CPU)
https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow_cpu-2.6.0-cp36-cp36m-manylinux2010_x86_64.whl
Python 3.7(支持 GPU)
https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow_gpu-2.6.0-cp37-cp37m-manylinux2010_x86_64.whl
Python 3.7(仅支持 CPU)
https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow_cpu-2.6.0-cp37-cp37m-manylinux2010_x86_64.whl
Python 3.8(支持 GPU)
https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow_gpu-2.6.0-cp38-cp38-manylinux2010_x86_64.whl
Python 3.8(仅支持 CPU)
https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow_cpu-2.6.0-cp38-cp38-manylinux2010_x86_64.whl
Python 3.9(支持 GPU)
https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow_gpu-2.6.0-cp39-cp39-manylinux2010_x86_64.whl
Python 3.9(仅支持 CPU)
https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow_cpu-2.6.0-cp39-cp39-manylinux2010_x86_64.whl
macOS(仅支持 CPU)
Python 3.6
https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-2.6.0-cp36-cp36m-macosx_10_11_x86_64.whl
Python 3.7
https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-2.6.0-cp37-cp37m-macosx_10_11_x86_64.whl
Python 3.8
https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-2.6.0-cp38-cp38-macosx_10_11_x86_64.whl
Python 3.9
https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-2.6.0-cp39-cp39-macosx_10_11_x86_64.whl
Windows
Python 3.6(支持 GPU)
https://storage.googleapis.com/tensorflow/windows/gpu/tensorflow_gpu-2.6.0-cp36-cp36m-win_amd64.whl
Python 3.6(仅支持 CPU)
https://storage.googleapis.com/tensorflow/windows/cpu/tensorflow_cpu-2.6.0-cp36-cp36m-win_amd64.whl
Python 3.7(支持 GPU)
https://storage.googleapis.com/tensorflow/windows/gpu/tensorflow_gpu-2.6.0-cp37-cp37m-win_amd64.whl
Python 3.7(仅支持 CPU)
https://storage.googleapis.com/tensorflow/windows/cpu/tensorflow_cpu-2.6.0-cp37-cp37m-win_amd64.whl
Python 3.8(支持 GPU)
https://storage.googleapis.com/tensorflow/windows/gpu/tensorflow_gpu-2.6.0-cp38-cp38-win_amd64.whl
Python 3.8(仅支持 CPU)
https://storage.googleapis.com/tensorflow/windows/cpu/tensorflow_cpu-2.6.0-cp38-cp38-win_amd64.whl
Python 3.9(支持 GPU)
https://storage.googleapis.com/tensorflow/windows/gpu/tensorflow_gpu-2.6.0-cp39-cp39-win_amd64.whl
Python 3.9(仅支持 CPU)
https://storage.googleapis.com/tensorflow/windows/cpu/tensorflow_cpu-2.6.0-cp39-cp39-win_amd64.whl