主要介绍在 Ubuntu16 中使用 pip 安装 Tensorflow 的过程。 pip 的安装:我通过安装 Anaconda3 已经安装好了 python 和 pip,有关 Anaconda3 安装过程参见:《Anaconda3 安装——Linux 下》
说明:截止 2019 年 1 月, tensorflow 还不支持 python3.7 版本。
1、安装:
说明:python 版本 3.6,pip 版本:
使用 pip 安装:
pip install tensorflow
或者使用国内源 (推荐):
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ --upgrade tensorflow
安装指定版本:
pip install tensorflow==1.12.0
2、验证
完成后验证是否安装成功,进入 python 命令行(python shell):
$ python
在 Python shell 中输入以下几行简短的程序代码(并赋输出):
>>> import tensorflow as tf >>> tf.__version__ '1.10.0' >>> hello = tf.constant('hello,tensorflow') >>> sess = tf.Session() >>> print(sess.run(hello)) b'hello,tensorflow'
则,说明 tensorflow 安装成功了。
3、可能遇到的问题
(1)若在 import 时出如下错误:
>>> import tensorflow
/usr/local/anaconda3/lib/python3.6/site-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from float
to np.floating
is deprecated. In future, it will be treated as np.float64 == np.dtype(float).type
.
from ._conv import register_converters as _register_converters
解决:
包内出错,是 h5py 包,则更新 h5py 即可,执行:
pip install --upgrade h5py
(2)若在使用 matplotlib.pyplot 时,出现如下错误:
>>> import matplotlib.pyplot
Traceback (most recent call last):
File “<stdin>”, line 1, in <module>
File “/home/bigdata/anaconda3/lib/python3.6/site-packages/matplotlib/pyplot.py”, line 113, in <module>
_backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
File “/home/bigdata/anaconda3/lib/python3.6/site-packages/matplotlib/backends/__init__.py”, line 60, in pylab_setup
[backend_name], 0)
File “/home/bigdata/anaconda3/lib/python3.6/site-packages/matplotlib/backends/backend_qt5agg.py”, line 16, in <module>
from .backend_qt5 import (
File “/home/bigdata/anaconda3/lib/python3.6/site-packages/matplotlib/backends/backend_qt5.py”, line 18, in <module>
import matplotlib.backends.qt_editor.figureoptions as figureoptions
File “/home/bigdata/anaconda3/lib/python3.6/site-packages/matplotlib/backends/qt_editor/figureoptions.py”, line 20, in <module>
import matplotlib.backends.qt_editor.formlayout as formlayout
File “/home/bigdata/anaconda3/lib/python3.6/site-packages/matplotlib/backends/qt_editor/formlayout.py”, line 56, in <module>
from matplotlib.backends.qt_compat import QtGui, QtWidgets, QtCore
File “/home/bigdata/anaconda3/lib/python3.6/site-packages/matplotlib/backends/qt_compat.py”, line 137, in <module>
from PyQt5 import QtCore, QtGui, QtWidgets
ImportError: libGL.so.1: cannot open shared object file: No such file or directory
解决:
apt-get install –y libgl1-mesa-glx
(3)若出现如下错误:
解决:
pip install -U --ignore-installed wrapt enum34 simplejson netaddr
(4)若在 import 时出现如下错误:
>>> import tensorflow as tf
tf.estimator package not installed.
解决:
相关包版本冲突,查看 pandas、matplotlib、numpy 等版本。通过调整版本解决,如:
pip install -U pandas==0.23.4
大功告成!!!