ubuntu18.04 配置python tensorflow虚拟环境+pycharm

在ubuntu18.04环境下装tensorflow的时候,总是会被装到~/.local下,寻找时候很麻烦,于是决定使用虚拟环境。

pycharm可以直接创建新的虚拟环境,并且可以直接在pycharm里添加安装环境需要的库(文末会提到),不过在pycharm里操作可控性不好,而且安装tensorflow往往会出现错误,所以这里还是先不使用pycharm来创建虚拟环境。

ubuntu18.04 默认的python版本是3.6.5

先安装python3 的自带的虚拟环境创建工具:

sudo apt-get install python3-venv

然后创建一个名为venv4ML0的虚拟环境(最好到需要创建环境的路径操作):

python3 -m venv venv4ML0

进入创建的该环境:

source ./venv4ML0/bin/activate

进入之后命令行头部会有标识:

(venv4ML0) yangguang@yangguang-Precision-Tower-7910:~/MachineLearning$

接下来直接安装tensorflow,有一些依赖库在安装tensorflow的时候会自动安装,例如numpy1.14.3,基本上自动安装的都是最新版,所以比自己装方便多了:

pip3 install tensorflow==1.8.0

不过装的时候容易出以下的错误:

(venv4ML0) yangguang@yangguang-Precision-Tower-7910:~/MachineLearning$ pip3 install tensorflow==1.8.0
Collecting tensorflow==1.8.0
  Using cached https://files.pythonhosted.org/packages/22/c6/d08f7c549330c2acc1b18b5c1f0f8d9d2af92f54d56861f331f372731671/tensorflow-1.8.0-cp36-cp36m-manylinux1_x86_64.whl
Collecting tensorboard<1.9.0,>=1.8.0 (from tensorflow==1.8.0)
  Using cached https://files.pythonhosted.org/packages/59/a6/0ae6092b7542cfedba6b2a1c9b8dceaf278238c39484f3ba03b03f07803c/tensorboard-1.8.0-py3-none-any.whl
Collecting astor>=0.6.0 (from tensorflow==1.8.0)
  Using cached https://files.pythonhosted.org/packages/b2/91/cc9805f1ff7b49f620136b3a7ca26f6a1be2ed424606804b0fbcf499f712/astor-0.6.2-py2.py3-none-any.whl
Collecting wheel>=0.26 (from tensorflow==1.8.0)
  Using cached https://files.pythonhosted.org/packages/81/30/e935244ca6165187ae8be876b6316ae201b71485538ffac1d718843025a9/wheel-0.31.1-py2.py3-none-any.whl
Collecting termcolor>=1.1.0 (from tensorflow==1.8.0)
Collecting numpy>=1.13.3 (from tensorflow==1.8.0)
  Using cached https://files.pythonhosted.org/packages/71/90/ca61e203e0080a8cef7ac21eca199829fa8d997f7c4da3e985b49d0a107d/numpy-1.14.3-cp36-cp36m-manylinux1_x86_64.whl
Collecting absl-py>=0.1.6 (from tensorflow==1.8.0)
Requirement already satisfied: six>=1.10.0 in ./venv4ML0/lib/python3.6/site-packages (from tensorflow==1.8.0)
Requirement already satisfied: grpcio>=1.8.6 in ./venv4ML0/lib/python3.6/site-packages (from tensorflow==1.8.0)
Collecting gast>=0.2.0 (from tensorflow==1.8.0)
Requirement already satisfied: protobuf>=3.4.0 in ./venv4ML0/lib/python3.6/site-packages (from tensorflow==1.8.0)
Collecting werkzeug>=0.11.10 (from tensorboard<1.9.0,>=1.8.0->tensorflow==1.8.0)
Exception:
Traceback (most recent call last):
  File "/home/yangguang/MachineLearning/venv4ML0/share/python-wheels/urllib3-1.19.1-py2.py3-none-any.whl/urllib3/connectionpool.py", line 350, in _make_request
    self._validate_conn(conn)
  File "/home/yangguang/MachineLearning/venv4ML0/share/python-wheels/urllib3-1.19.1-py2.py3-none-any.whl/urllib3/connectionpool.py", line 837, in _validate_conn
    conn.connect()
  File "/home/yangguang/MachineLearning/venv4ML0/share/python-wheels/urllib3-1.19.1-py2.py3-none-any.whl/urllib3/connection.py", line 323, in connect
    ssl_context=context)
  File "/home/yangguang/MachineLearning/venv4ML0/share/python-wheels/urllib3-1.19.1-py2.py3-none-any.whl/urllib3/util/ssl_.py", line 324, in ssl_wrap_socket
    return context.wrap_socket(sock, server_hostname=server_hostname)
  File "/usr/lib/python3.6/ssl.py", line 407, in wrap_socket
    _context=self, _session=session)
  File "/usr/lib/python3.6/ssl.py", line 814, in __init__
    self.do_handshake()
  File "/usr/lib/python3.6/ssl.py", line 1068, in do_handshake
    self._sslobj.do_handshake()
  File "/usr/lib/python3.6/ssl.py", line 689, in do_handshake
    self._sslobj.do_handshake()
socket.timeout: _ssl.c:817: The handshake operation timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/yangguang/MachineLearning/venv4ML0/share/python-wheels/urllib3-1.19.1-py2.py3-none-any.whl/urllib3/connectionpool.py", line 594, in urlopen
    chunked=chunked)
  File "/home/yangguang/MachineLearning/venv4ML0/share/python-wheels/urllib3-1.19.1-py2.py3-none-any.whl/urllib3/connectionpool.py", line 353, in _make_request
    self._raise_timeout(err=e, url=url, timeout_value=conn.timeout)
  File "/home/yangguang/MachineLearning/venv4ML0/share/python-wheels/urllib3-1.19.1-py2.py3-none-any.whl/urllib3/connectionpool.py", line 313, in _raise_timeout
    raise ReadTimeoutError(self, url, "Read timed out. (read timeout=%s)" % timeout_value)
requests.packages.urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='pypi.org', port=443): Read timed out. (read timeout=15)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/yangguang/MachineLearning/venv4ML0/lib/python3.6/site-packages/pip/basecommand.py", line 215, in main
    status = self.run(options, args)
  File "/home/yangguang/MachineLearning/venv4ML0/lib/python3.6/site-packages/pip/commands/install.py", line 353, in run
    wb.build(autobuilding=True)
  File "/home/yangguang/MachineLearning/venv4ML0/lib/python3.6/site-packages/pip/wheel.py", line 749, in build
    self.requirement_set.prepare_files(self.finder)
  File "/home/yangguang/MachineLearning/venv4ML0/lib/python3.6/site-packages/pip/req/req_set.py", line 380, in prepare_files
    ignore_dependencies=self.ignore_dependencies))
  File "/home/yangguang/MachineLearning/venv4ML0/lib/python3.6/site-packages/pip/req/req_set.py", line 554, in _prepare_file
    require_hashes
  File "/home/yangguang/MachineLearning/venv4ML0/lib/python3.6/site-packages/pip/req/req_install.py", line 278, in populate_link
    self.link = finder.find_requirement(self, upgrade)
  File "/home/yangguang/MachineLearning/venv4ML0/lib/python3.6/site-packages/pip/index.py", line 465, in find_requirement
    all_candidates = self.find_all_candidates(req.name)
  File "/home/yangguang/MachineLearning/venv4ML0/lib/python3.6/site-packages/pip/index.py", line 423, in find_all_candidates
    for page in self._get_pages(url_locations, project_name):
  File "/home/yangguang/MachineLearning/venv4ML0/lib/python3.6/site-packages/pip/index.py", line 568, in _get_pages
    page = self._get_page(location)
  File "/home/yangguang/MachineLearning/venv4ML0/lib/python3.6/site-packages/pip/index.py", line 683, in _get_page
    return HTMLPage.get_page(link, session=self.session)
  File "/home/yangguang/MachineLearning/venv4ML0/lib/python3.6/site-packages/pip/index.py", line 792, in get_page
    "Cache-Control": "max-age=600",
  File "/home/yangguang/MachineLearning/venv4ML0/share/python-wheels/requests-2.12.4-py2.py3-none-any.whl/requests/sessions.py", line 501, in get
    return self.request('GET', url, **kwargs)
  File "/home/yangguang/MachineLearning/venv4ML0/lib/python3.6/site-packages/pip/download.py", line 386, in request
    return super(PipSession, self).request(method, url, *args, **kwargs)
  File "/home/yangguang/MachineLearning/venv4ML0/share/python-wheels/requests-2.12.4-py2.py3-none-any.whl/requests/sessions.py", line 488, in request
    resp = self.send(prep, **send_kwargs)
  File "/home/yangguang/MachineLearning/venv4ML0/share/python-wheels/requests-2.12.4-py2.py3-none-any.whl/requests/sessions.py", line 630, in send
    history = [resp for resp in gen] if allow_redirects else []
  File "/home/yangguang/MachineLearning/venv4ML0/share/python-wheels/requests-2.12.4-py2.py3-none-any.whl/requests/sessions.py", line 630, in <listcomp>
    history = [resp for resp in gen] if allow_redirects else []
  File "/home/yangguang/MachineLearning/venv4ML0/share/python-wheels/requests-2.12.4-py2.py3-none-any.whl/requests/sessions.py", line 190, in resolve_redirects
    **adapter_kwargs
  File "/home/yangguang/MachineLearning/venv4ML0/share/python-wheels/requests-2.12.4-py2.py3-none-any.whl/requests/sessions.py", line 609, in send
    r = adapter.send(request, **kwargs)
  File "/home/yangguang/MachineLearning/venv4ML0/share/python-wheels/CacheControl-0.11.7-py2.py3-none-any.whl/cachecontrol/adapter.py", line 47, in send
    resp = super(CacheControlAdapter, self).send(request, **kw)
  File "/home/yangguang/MachineLearning/venv4ML0/share/python-wheels/requests-2.12.4-py2.py3-none-any.whl/requests/adapters.py", line 423, in send
    timeout=timeout
  File "/home/yangguang/MachineLearning/venv4ML0/share/python-wheels/urllib3-1.19.1-py2.py3-none-any.whl/urllib3/connectionpool.py", line 643, in urlopen
    _stacktrace=sys.exc_info()[2])
  File "/home/yangguang/MachineLearning/venv4ML0/share/python-wheels/urllib3-1.19.1-py2.py3-none-any.whl/urllib3/util/retry.py", line 315, in increment
    total -= 1
TypeError: unsupported operand type(s) for -=: 'Retry' and 'int'

个人觉得可能是网络的问题,遇到这种情况就单独装出错处的lib,笔者出现过多次出错,但是重复安装就成功了:

pip3 install Werkzeug==0.14.1

然后再一遍:

pip3 install tensorflow==1.8.0

可能还会有其他同样的问题,同样处理,直到pip3 install tensorflow==1.8.0运行成功!

这样tensorflow就装好了,可以使用hello world测试一下,此处不做说明。

现在打开pycharm,File->Setting->Project Interpreter->设置->Existing environment->选择该虚拟环境路径 bin下的python3即可,导入虚拟环境后pycharm显示:

可以直接点击右边的+进行添加新的库,也可以一开始就在这里安装虚拟环境里的tensorflow,有兴趣可以尝试,不过我这样操作的体验并不好。

测试:

安装一些其他依赖的模块:

pip3 install matplotlib --upgrade

pip3 install scikit-learn --upgrade

pip3 install scikit-image --upgrade

使用完环境退出虚拟环境:

deactivate

使用这种方法创建虚拟环境不会导入系统python环境里pip安装的包(ubuntu18.04会将pip安装的包存在~/.local中),默认使用最干净的环境。

可以使用pip freeze > requirements.txt 将当前环境中的包的名称及版本导出到文本中 
然后在另一个环境中 pip install -r requirements.txt 安装,虚拟环境会自动创建和系统环境里包的链接,不需要重新下载一遍。

如果从系统环境中导出,会有很多没有用的lib,可以在requirement.txt里删除,或者自己手动编辑一个requirement.txt文件。

如果同时需要python2的开发环境,可以再创建一个python2的虚拟环境,这个虚拟环境和python3的虚拟环境完全隔离:

sudo apt-get install python-virtualenv

virtualenv -p /usr/bin/python2 py2env4ML

之后的操作和python3的虚拟环境一样。

猜你喜欢

转载自blog.csdn.net/ygfrancois/article/details/80328679