虚拟环境管理工具 - Virtualenvwrapper
virtualenv使用起来不是很方便,因为virtual的启动、停止脚本都在特定目录,可能一段时间后,你可能会有很多个虚拟环境散落在系统各处,你可能忘记它们的名字或者位置。鉴于virtualenv不便于对虚拟环境集中管理,所以推荐直接使用virtualenvwrapper。 virtualenvwrapper提供了一系列命令使得和虚拟环境工作变得便利。它把你所有的虚拟环境都放在一个地方。
Virtaulenvwrapper是virtualenv的扩展包,用于更方便管理虚拟环境,它可以做:
- 将所有虚拟环境整合在一个目录下;
- 管理(新增,删除,复制)虚拟环境;
- 切换虚拟环境等等.
安装virtualenvwrapper (确保virtualenv已提前安装)
[root@kevin-test ~]# pip install virtualenvwrapper
Could not find an activated virtualenv (required).
如果出现上面的报错:"Could not find an activated virtualenv (required)."
解决办法:
打开~/.bashrc文件, 添加下面的内容
[root@kevin-test ~]# vim ~/.bashrc
export PIP_REQUIRE_VIRTUALENV=true
[root@kevin-test ~]# source ~/.bashrc
然后接着安装virtualenvwrapper
[root@kevin-test ~]# pip install virtualenvwrapper
如果还是出现上面的那个报错:"Could not find an activated virtualenv (required)."
接着执行下面命令(~/.bashrc文件里设置该参数为true, 确保pip安装的包在虚拟环境下; 虚拟环境之外的系统里执行pip时, 每次需要手动source 这个参数的false值)
[root@kevin-test ~]# export PIP_REQUIRE_VIRTUALENV=false
最后再次安装virtualenvwrapper即可
[root@kevin-test ~]# pip install virtualenvwrapper
Collecting virtualenvwrapper
Using cached https://files.pythonhosted.org/packages/2b/8c/3192e10913ad945c0f0fcb17e9b2679434a28ad58ee31ce0104cba3b1154/virtualenvwrapper-4.8.2-py2.py3-none-any.whl
Requirement already satisfied: virtualenv-clone in /usr/lib/python2.7/site-packages/virtualenv_clone-0.4.0-py2.7.egg (from virtualenvwrapper) (0.4.0)
Requirement already satisfied: stevedore in /usr/lib/python2.7/site-packages/stevedore-1.30.0-py2.7.egg (from virtualenvwrapper) (1.30.0)
Requirement already satisfied: virtualenv in /usr/lib/python2.7/site-packages (from virtualenvwrapper) (16.1.0)
Requirement already satisfied: pbr!=2.1.0,>=2.0.0 in /usr/lib/python2.7/site-packages/pbr-5.1.1-py2.7.egg (from stevedore->virtualenvwrapper) (5.1.1)
Requirement already satisfied: six>=1.10.0 in /usr/lib/python2.7/site-packages/six-1.12.0-py2.7.egg (from stevedore->virtualenvwrapper) (1.12.0)
Installing collected packages: virtualenvwrapper
Successfully installed virtualenvwrapper-4.8.2
默认virtualenvwrapper安装在下面python解释器中的site-packages,实际上需要运行virtualenvwrapper.sh文件才行;
所以要查找出安装的virtualenvwrapper.sh执行文件
[root@kevin-test ~]# find / -name virtualenvwrapper*
/usr/bin/virtualenvwrapper.sh
/usr/bin/virtualenvwrapper_lazy.sh
/usr/lib/python2.7/site-packages/virtualenvwrapper-4.8.2-py2.7-nspkg.pth
/usr/lib/python2.7/site-packages/virtualenvwrapper
/usr/lib/python2.7/site-packages/virtualenvwrapper-4.8.2.dist-info
此时还不能使用virtualenvwrapper,每次要想使用virtualenvwrapper 工具时,都必须先激活virtualenvwrapper.sh文件才行,
另外,如果创建前要将即将的环境保存到虚拟环境中,就要先设置一下环境变量
设置环境变量
[root@kevin-test ~]# export WORKON_HOME=~/Envs
创建虚拟环境管理目录. 即创建的虚拟环境都会放到上面设置的~/Envs目录下面
[root@kevin-test ~]# mkdir -p $WORKON_HOME
在~/.bashrc文件里配置环境变量, 添加下面两行
第一行表示: 存放虚拟环境目录
第二行表示: virtrualenvwrapper.sh执行文件的路径
[root@kevin-test ~]# vim ~/.bashrc
export WORKON_HOME=~/Envs
source /usr/bin/virtualenvwrapper.sh
使得以上环境变量的配置生效
[root@kevin-test ~]# source ~/.bashrc
现在开始使用Virtualenvwrapper 管理虚拟机 (可以删除上面之前创建的虚拟环境/opt/project_test/pj_test 了, 后续都将使用Virtualenvwrapper创建和管理虚拟环境)
a) 使用mkvirtualenv 命令创建虚拟环境
创建一个虚拟环境, 虚拟机名称为project_kevin:
[root@kevin-test ~]# mkvirtualenv project_kevin
New python executable in /root/Envs/project_kevin/bin/python2
Also creating executable in /root/Envs/project_kevin/bin/python
Installing setuptools, pip, wheel...
done.
virtualenvwrapper.user_scripts creating /root/Envs/project_kevin/bin/predeactivate
virtualenvwrapper.user_scripts creating /root/Envs/project_kevin/bin/postdeactivate
virtualenvwrapper.user_scripts creating /root/Envs/project_kevin/bin/preactivate
virtualenvwrapper.user_scripts creating /root/Envs/project_kevin/bin/postactivate
virtualenvwrapper.user_scripts creating /root/Envs/project_kevin/bin/get_env_details
(project_kevin) [root@kevin-test ~]#
再创建另一个虚拟机, 虚拟机名称为project_bobo
(project_kevin) [root@kevin-test ~]# deactivate //先退出上面的那个虚拟环境
[root@kevin-test ~]# mkvirtualenv --no-site-packages project_bo //这里加上了参数--no-site-packages
New python executable in /root/Envs/project_bo/bin/python2
Also creating executable in /root/Envs/project_bo/bin/python
Installing setuptools, pip, wheel...
done.
virtualenvwrapper.user_scripts creating /root/Envs/project_bo/bin/predeactivate
virtualenvwrapper.user_scripts creating /root/Envs/project_bo/bin/postdeactivate
virtualenvwrapper.user_scripts creating /root/Envs/project_bo/bin/preactivate
virtualenvwrapper.user_scripts creating /root/Envs/project_bo/bin/postactivate
virtualenvwrapper.user_scripts creating /root/Envs/project_bo/bin/get_env_details
(project_bo) [root@kevin-test ~]#
====================================================
还可以选择指定的python版本进行虚拟环境的创建, 不过指定的python版本必须要提前安装好, 比如
# mkvirtualenv project_hui --python=python3.6.1
# mkvirtualenv project_hui --python=/usr/local/python3/bin/python3.6.1
====================================================
创建的虚拟环境的项目目录都放在了上面设置的环境变量目录~/Envs下面
[root@kevin-test ~]# ll -d ~/Envs/project_kevin/
drwxr-xr-x 5 root root 56 Dec 17 22:27 /root/Envs/project_kevin/
[root@kevin-test ~]# ll -d ~/Envs/project_bo/
drwxr-xr-x 5 root root 56 Dec 17 22:28 /root/Envs/project_bo/
b) 使用workon 或者 lsvirtualenv 命令列出虚拟环境列表
[root@kevin-test ~]# workon
project_bo
project_kevin
或者
[root@kevin-test ~]# lsvirtualenv
project_bo
==========
project_kevin
=============
c) 使用workon 启动/切换虚拟环境
[root@kevin-test ~]# workon project_bo
(project_bo) [root@kevin-test ~]# python -V
Python 2.7.5
(project_bo) [root@kevin-test ~]#
创建一个项目,它会创建虚拟环境,并在 $WORKON_HOME 中创建一个项目目录。
当您使用 "workon 项目的虚拟环境名称" 时,它会自动cd -ed 到项目目录中。
d) 使用deactivate命令退出/离开虚拟环境,和virutalenv一样的命令
[root@kevin-test ~]# workon project_bo
(project_bo) [root@kevin-test ~]# python -V
Python 2.7.5
(project_bo) [root@kevin-test ~]# deactivate
[root@kevin-test ~]#
e) 使用rmvirtualenv 命令 删除虚拟环境
[root@kevin-test ~]# lsvirtualenv
project_bo
==========
project_kevin
=============
[root@kevin-test ~]# rmvirtualenv project_bo
Removing project_bo...
[root@kevin-test ~]# lsvirtualenv
project_kevin
=============
f) 使用lssitepackages命令显示 site-packages 目录中的内容。
[root@kevin-test ~]# workon project_kevin
(project_kevin) [root@kevin-test ~]# lssitepackages
easy_install.py pip pkg_resources setuptools-40.6.3.dist-info wheel-0.32.3.dist-info
easy_install.pyc pip-18.1.dist-info setuptools wheel
g) 使用cdvirtualenv命令导航到当前激活的虚拟环境的目录中,比如说这样就能够浏览它的site-packages。
[root@kevin-test ~]# workon project_kevin
(project_kevin) [root@kevin-test site-packages]# cdvirtualenv
(project_kevin) [root@kevin-test project_kevin]# ls
bin include lib lib64
h) 使用cdsitepackages命令直接进入到 site-packages 目录中, 和上面的cdvirtualenv 类似
[root@kevin-test ~]# workon project_kevin
(project_kevin) [root@kevin-test ~]# cdsitepackages
(project_kevin) [root@kevin-test site-packages]# ls
easy_install.py pip pkg_resources setuptools-40.6.3.dist-info wheel-0.32.3.dist-info
easy_install.pyc pip-18.1.dist-info setuptools wheel
=============================================
为了方便运维管理, 可以进行一些别名设置, 如下:
[root@kevin-test ~]# vim ~/.bashrc
#virtualenv
alias vte='virtualenv'
alias mkvte='mkvirtualenv'
alias deact='deactivate'
alias rmvte='rmvirtualenv'
alias lsvte='lsvirtualenv'
alias won='workon'
使得上面配置生效
[root@kevin-test ~]# source ~/.bashrc
使用别名进行操作
[root@kevin-test ~]# lsvte
project_kevin
=============
[root@kevin-test ~]# won project_kevin
(project_kevin) [root@kevin-test ~]# deact
[root@kevin-test ~]# rmvte project_kevin
Removing project_kevin...
[root@kevin-test ~]# lsvte
[root@kevin-test ~]#
virtualenv使用时遇到的一个坑点, 经常排查验证, 得出结论如下: