python虚拟环境 virtualenv

同一台实例,部署多个不同依赖的python项目:

pip install virtualenv

cd my_project_dir
virtualenv venv  #venv为虚拟环境目录名,目录名自定义
# virtualenv venv 将会在当前的目录中创建一个文件夹,包含了Python可执行文件,以及 pip 库的一份拷贝,这样就能安装其他包了。虚拟环境的名字(此例中是 venv )可以是任意的;若省略名字将会把文件均放在当前目录

virtualenv -p /usr/bin/python2.7 venv #  指定创建虚拟环境的解释器

source venv/bin/activate  # 激活
. venv/bin/deactivate            # 停用


# virtualenvwrapper
# 鉴于virtualenv不便于对虚拟环境集中管理,所以推荐直接使用virtualenvwrapper。 virtualenvwrapper提供了一系列命令使得和虚拟环境工作变得便利。它把你所有的虚拟环境都放在一个地方。
virtualenv -p /usr/bin/python3 zhenai-devops
报错:
Collecting setuptools
  Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError('<pip._vendor.urllib3.connection.VerifiedHTTPSConnection object at 0x7fa2a05b8ef0>: Failed to establish a new connection: [Errno 101] Network is unreachable',)': /packages/09/27/98ef5f13ca9c63d9d8601e739817839dde55baeb105a6c32fc8f24daddc2/setuptools-46.3.0-py3-none-any.whl
  Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError('<pip._vendor.urllib3.connection.VerifiedHTTPSConnection object at 0x7fa2a05b8a20>: Failed to establish a new connection: [Errno 101] Network is unreachable',)': /packages/09/27/98ef5f13ca9c63d9d8601e739817839dde55baeb105a6c32fc8f24daddc2/setuptools-46.3.0-py3-none-any.whl
  Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError('<pip._vendor.urllib3.connection.VerifiedHTTPSConnection object at 0x7fa2a05b82e8>: Failed to establish a new connection: [Errno 101] Network is unreachable',)': /packages/09/27/98ef5f13ca9c63d9d8601e739817839dde55baeb105a6c32fc8f24daddc2/setuptools-46.3.0-py3-none-any.whl
  Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError('<pip._vendor.urllib3.connection.VerifiedHTTPSConnection object at 0x7fa2a05b8278>: Failed to establish a new connection: [Errno 101] Network is unreachable',)': /packages/09/27/98ef5f13ca9c63d9d8601e739817839dde55baeb105a6c32fc8f24daddc2/setuptools-46.3.0-py3-none-any.whl
  Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError('<pip._vendor.urllib3.connection.VerifiedHTTPSConnection object at 0x7fa2a05b8ba8>: Failed to establish a new connection: [Errno 101] Network is unreachable',)': /packages/09/27/98ef5f13ca9c63d9d8601e739817839dde55baeb105a6c32fc8f24daddc2/setuptools-46.3.0-py3-none-any.whl
Could not install packages due to an EnvironmentError: HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Max retries exceeded with url: /packages/09/27/98ef5f13ca9c63d9d8601e739817839dde55baeb105a6c32fc8f24daddc2/setuptools-46.3.0-py3-none-any.whl (Caused by NewConnectionError('<pip._vendor.urllib3.connection.VerifiedHTTPSConnection object at 0x7fa2a05b8a58>: Failed to establish a new connection: [Errno 101] Network is unreachable',))

----------------------------------------
...Installing setuptools, pip, wheel...done.
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/virtualenv.py", line 2567, in <module>
    main()
  File "/usr/local/lib/python3.6/site-packages/virtualenv.py", line 793, in main
    symlink=options.symlink,
  File "/usr/local/lib/python3.6/site-packages/virtualenv.py", line 1088, in create_environment
    install_wheel(to_install, py_executable, search_dirs, download=download)
  File "/usr/local/lib/python3.6/site-packages/virtualenv.py", line 935, in install_wheel
    _install_wheel_with_search_dir(download, project_names, py_executable, search_dirs)
  File "/usr/local/lib/python3.6/site-packages/virtualenv.py", line 1025, in _install_wheel_with_search_dir
    call_subprocess(cmd, show_stdout=False, extra_env=env, stdin=script)
  File "/usr/local/lib/python3.6/site-packages/virtualenv.py", line 886, in call_subprocess
    raise OSError("Command {} failed with error code {}".format(cmd_desc, proc.returncode))
OSError: Command /data/zhenai-devops/bin/python3 - setuptools pip wheel failed with error code 1



解决:
重装virtualenv:
pip3 install virtualenv -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
之后再执行:
virtualenv -p /usr/bin/python3 zhenai-devops
(service_tree_env) [root@demon bsc-service-tree]# pip3 install --index https://pypi.douban.com/simple/ requirements.txt 
Looking in indexes: https://pypi.douban.com/simple/
ERROR: Could not find a version that satisfies the requirement requirements.txt (from versions: none)
ERROR: No matching distribution found for requirements.txt


命令执行错误
正确方法: pip3 install --index https://pypi.douban.com/simple/ 
 -r requirements.txt 
原文地址:https://www.cnblogs.com/yum777/p/10470008.html