python虚拟环境 virtualenv 镜像源

 

 

pip 镜像源

清华大学 https://pypi.tuna.tsinghua.edu.cn/simple/

豆瓣 (douban) http://pypi.douban.com/simple/ (推荐)

阿里云 http://mirrors.aliyun.com/pypi/simple/
中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/
中国科学技术大学 http://pypi.mirrors.ustc.edu.cn/simple/

 

 

 

 

 

同一台实例,部署多个不同依赖的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 

 

 

Centos7安装Python3的方法

 

由于centos7原本就安装了Python2,而且这个Python2不能被删除,因为有很多系统命令,比如yum都要用到。

[root@VM_105_217_centos Python-3.6.2]# python
Python 2.7.5 (default, Aug  4 2017, 00:39:18)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux2
Type "help", "copyright", "credits" or "license" for more information.

输入Python命令,查看可以得知是Python2.7.5版本

输入

which python

可以查看位置,一般是位于/usr/bin/python目录下。

下面介绍安装Python3的方法

首先安装依赖包

yum -y groupinstall "Development tools"
yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel

然后根据自己需求下载不同版本的Python3,我下载的是Python3.6.2

wget https://www.python.org/ftp/python/3.6.2/Python-3.6.2.tar.xz

如果速度不够快,可以直接去官网下载,利用WinSCP等软件传到服务器上指定位置,我的存放目录是/usr/local/python3,使用命令:

mkdir /usr/local/python3 

建立一个空文件夹

然后解压压缩包,进入该目录,安装Python3

tar -xvJf  Python-3.6.2.tar.xz
cd Python-3.6.2
./configure --prefix=/usr/local/python3
make && make install

最后创建软链接

ln -s /usr/local/python3/bin/python3 /usr/bin/python3
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3

在命令行中输入python3测试

 
分类: Python
posted @ 2019-03-04 12:40  whitesky-root  阅读(386)  评论(0编辑  收藏  举报