tensorflow windows安装 Error import tensorflow错误解决方法

  • 学习资料:
    • 官方关于 Tensorflow 安装的 说明

安装 Tensorflow 时需要注意的几点:

  1. MacOS, Linux, Windows 系统均已支持 Tensorflow
  2. 确定你的 python 版本
  3. 你的 GPU 是 NVIDIA, 就可以安装 GPU 版本的 Tensorflow; 你的 GPU 不是 NVIDIA 也没有关系, 安装 CPU 版本的就好了.
安装 Tensorflow

Tensorflow 的安装方式很多. 比如官网提供的:

这节内容使用 pip 在每个系统的安装方式:

Linux 和 MacOS

本文将提到第一种最简单的安装方式, pip 安装. 使用 pip 安装的时候要确保你的 pip 已经存在于你的电脑中. 如果还没有安装 pip. 你可以在 Terminal 窗口中运行这个:

# Ubuntu/Linux 64-位 系统的执行代码:
$ sudo apt-get install python-pip python-dev

# Mac OS X 系统的执行代码:
$ sudo easy_install pip
$ sudo easy_install --upgrade six
CPU 版

激动人心的时刻到了, Tensorflow (0.12) 刚刚做了更新, 绕过了复杂的安装步骤, 如果你只需要安装 CPU 版本的 Tensorflow, 运行下面这个就好了:

# python 2+ 的用户:
$ pip install tensorflow

# python 3+ 的用户:
$ pip3 install tensorflow

注意: 你需要8.1或更高版的 pip 才能顺利安装.

GPU 版

但是 如果你想安装 GPU 版的 Tensorflow, 你要在下面找一个适合你版本的安装文件, 并在 terminal 中执行:

# Ubuntu/Linux 64-bit, CPU only, Python 2.7
$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.12.0rc0-cp27-none-linux_x86_64.whl

# Ubuntu/Linux 64-bit, GPU enabled, Python 2.7
# Requires CUDA toolkit 8.0 and CuDNN v5. For other versions, see "Installing from sources" below.
$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow_gpu-0.12.0rc0-cp27-none-linux_x86_64.whl

# Mac OS X, CPU only, Python 2.7:
$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-0.12.0rc0-py2-none-any.whl

# Mac OS X, GPU enabled, Python 2.7:
$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/mac/gpu/tensorflow_gpu-0.12.0rc0-py2-none-any.whl

# Ubuntu/Linux 64-bit, CPU only, Python 3.4
$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.12.0rc0-cp34-cp34m-linux_x86_64.whl

# Ubuntu/Linux 64-bit, GPU enabled, Python 3.4
# Requires CUDA toolkit 8.0 and CuDNN v5. For other versions, see "Installing from sources" below.
$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow_gpu-0.12.0rc0-cp34-cp34m-linux_x86_64.whl

# Ubuntu/Linux 64-bit, CPU only, Python 3.5
$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.12.0rc0-cp35-cp35m-linux_x86_64.whl

# Ubuntu/Linux 64-bit, GPU enabled, Python 3.5
# Requires CUDA toolkit 8.0 and CuDNN v5. For other versions, see "Installing from sources" below.
$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow_gpu-0.12.0rc0-cp35-cp35m-linux_x86_64.whl

# Mac OS X, CPU only, Python 3.4 or 3.5:
$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-0.12.0rc0-py3-none-any.whl

# Mac OS X, GPU enabled, Python 3.4 or 3.5:
$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/mac/gpu/tensorflow_gpu-0.12.0rc0-py3-none-any.whl

最后, 根据自己的 python 版本, 在 terminal 中执行以下语句:

# 如果你是 Python 2, 请复制下面
$ sudo pip install --upgrade $TF_BINARY_URL

# 如果你是 Python 3, 请复制下面
$ sudo pip3 install --upgrade $TF_BINARY_URL
Windows

tf 0.12 版的英文安装说明

安装前的检查:

  • 目前只支持 Python 3.5 (64bit) 版本
  • 你有安装 numpy (没有的话,请看这里numpy 安装教程)

接下来惊心动魄啦! 在 command 窗口中执行

# CPU 版的
C:\> pip install tensorflow

# GPU 版的
C:\> pip install tensorflow-gpu

注意 Windows 运行 Tensorflow 如果遇到这个报错:

Error importing tensorflow.  Unless you are using bazel,
you should not try to import tensorflow from its source directory;
please exit the tensorflow source tree, and relaunch your python interpreter
from there.

不要惊慌, 尝试下载安装 Windows 的 Microsoft Visual C++ 2015 redistributable update 3 64 bit. 就能解决这个问题.

测试

然后打开你的 python 编辑器, 输入

import tensorflow

运行脚本来检查一下是否有正确安装.

更新 Tensorflow

最后, 如果你需要升级 Tensorflow 的版本, 推荐的方式是:

根据你的 python 版本, 在 terminal 中删除原有的版本

# 如果你是 Python 2, 请复制下面
pip uninstall tensorflow

# 如果你是 Python 3, 请复制下面
pip3 uninstall tensorflow

然后重复这个安装教程的步骤, 从头安装新版本.

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值