Ubuntu16.04安装Tensorflow问题及步骤总结

本总结,主要针对胡春旭老师的《ROS机器人开发实践》中第11章-ROS与机器学习中安装TensorFlow遇到的问题。

1.Ubuntu安装pip工具
系统环境:Ubuntu16.04LTS
Python版本:Python2.7

$ sudo apt-get install python-pip python-dev
正在读取软件包列表... 完成
正在分析软件包的依赖关系树       
正在读取状态信息... 完成       
python-dev 已经是最新版 (2.7.12-1~16.04)。
python-dev 已设置为手动安装。
将会同时安装下列软件:
  libpython-all-dev python-all python-all-dev python-pip-whl python-wheel
下列【新】软件包将被安装:
  libpython-all-dev python-all python-all-dev python-pip python-pip-whl python-wheel
升级了 0 个软件包,新安装了 6 个软件包,要卸载 0 个软件包,有 3 个软件包未被升级。
需要下载 1,307 kB 的归档。
解压缩后会消耗 2,086 kB 的额外空间。

2.设置TensorFlow的下载地址并进行安装

$ sudo pip install --upgrade https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-1.4.0-cp27-none-linux_x86_64.whl

注:https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-1.4.0-cp27-none-linux_x86_64.whl根据需要的版本进行设置。

$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-1.4.0-cp27-none-linux_x86_64.whl
$ sudo pip install --upgrade $TF_BINARY_URL

可能出现的问题1:

You are using pip version 8.1.1, however version 9.0.1 is available

解决办法

$ sudo -H pip install --upgrade pip
Collecting pip
  Downloading https://files.pythonhosted.org/packages/b7/2d/ad02de84a4c9fd3b1958dc9fb72764de1aa2605a9d7e943837be6ad82337/pip-21.0.1.tar.gz (1.5MB)
    100% |████████████████████████████████| 1.6MB 93kB/s 
Building wheels for collected packages: pip
  Running setup.py bdist_wheel for pip ... done
  Stored in directory: /root/.cache/pip/wheels/d9/3c/49/0725759c13b5dc65b58490b1a271b9b664912f1f43f0cfd769
Successfully built pip
Installing collected packages: pip
  Found existing installation: pip 8.1.1

可能出现的问题2:

Traceback (most recent call last):
  File "/usr/local/bin/pip", line 7, in 
    from pip._internal.cli.main import main
  File "/usr/local/lib/python2.7/dist-packages/pip/_internal/cli/main.py", line 60
    sys.stderr.write(f"ERROR: {exc}")
                                   ^
SyntaxError: invalid syntax

解决办法

$ wget https://bootstrap.pypa.io/get-pip.py --no-check-certificate
--2021-02-27 12:22:42--  https://bootstrap.pypa.io/get-pip.py
正在解析主机 bootstrap.pypa.io (bootstrap.pypa.io)... 151.101.108.175
正在连接 bootstrap.pypa.io (bootstrap.pypa.io)|151.101.108.175|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度: 1927626 (1.8M) [text/x-python]
正在保存至: “get-pip.py”

get-pip.py               100%[==================================>]   1.84M   458KB/s    in 3.9s    

2021-02-27 12:22:56 (480 KB/s) - 已保存 “get-pip.py” [1927626/1927626])

使用该命令下载更快,若不加–no-check-certificate会因网络问题,反复报错。

继续执行命令

$ sudo python get-pip.py
ERROR: This script does not work on Python 2.7 The minimum supported Python version is 3.6. Please use https://bootstrap.pypa.io/2.7/get-pip.py instead.

因为使用的是Python2.7,因此无法执行get-pip.py。

因此应使用

$ wget https://bootstrap.pypa.io/2.7/get-pip.py --no-check-certificate
--2021-02-27 12:24:42--  https://bootstrap.pypa.io/2.7/get-pip.py
正在解析主机 bootstrap.pypa.io (bootstrap.pypa.io)... 151.101.228.175, 2a04:4e42:12::175
正在连接 bootstrap.pypa.io (bootstrap.pypa.io)|151.101.228.175|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度: 1908226 (1.8M) [text/x-python]
正在保存至: “get-pip.py.1”

get-pip.py.1             100%[==================================>]   1.82M   747KB/s    in 2.5s    

2021-02-27 12:24:47 (747 KB/s) - 已保存 “get-pip.py.1” [1908226/1908226])
$ sudo python get-pip.py
Collecting pip<21.0
  Downloading pip-20.3.4-py2.py3-none-any.whl (1.5 MB)
     |████████████████████████████████| 1.5 MB 32 kB/s 
Installing collected packages: pip
  Attempting uninstall: pip
    Found existing installation: pip 21.0.1
    Uninstalling pip-21.0.1:
      Successfully uninstalled pip-21.0.1
Successfully installed pip-20.3.4

再次执行

$ sudo pip install --upgrade https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-1.4.0-cp27-none-linux_x86_64.whl
...
ERROR: Cannot uninstall 'enum34'. It is a distutils installed project and thus we cannot accurately determine which files belong to it which would lead to only a partial uninstall.
//无法升级enum34,导致TensorFlow无法正常下载安装
$ pip install enum34 --upgrade  --ignore-installed
Defaulting to user installation because normal site-packages is not writeable
Collecting enum34
  Downloading enum34-1.1.10-py2-none-any.whl (11 kB)
Installing collected packages: enum34
Successfully installed enum34-1.1.10
$ sudo pip install --upgrade https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-1.4.0-cp27-none-linux_x86_64.whl
...
Installing collected packages: tensorflow
Successfully installed tensorflow-1.4.0

安装成功,再次在终端执行Python,并使用import tensorflow as tf,成功。
至此安装结束。

你可能感兴趣的:(python,tensorflow)