1.pip安装virtualenvwrapper
[root@localhost ~]# pip install virtualenvwrapper # 安装virtualenvwrapper
[root@localhost ~]# pip list | grep virtualenvwrapper
virtualenvwrapper (4.8.2)
2.配置环境变量
[root@localhost ~]# find / -name virtualenvwrapper.sh # 查找virtualenvwrapper.sh所有的目录
/usr/bin/virtualenvwrapper.sh
[root@localhost ~]# mkdir .virtualenvs # 创建虚拟环境的存放目录
[root@localhost ~]# vi .bashrc # 编辑家目录下的.bashrc文件,在文件末尾添加下面的两行内容
export WORKON_HOME=$HOME/.virtualenvs
source /usr/bin/virtualenvwrapper.sh
[root@localhost ~]# source .bashrc # 重新加载.bashrc文件,使配置立即生效
virtualenvwrapper.user_scripts creating /root/.virtualenvs/premkproject
virtualenvwrapper.user_scripts creating /root/.virtualenvs/postmkproject
virtualenvwrapper.user_scripts creating /root/.virtualenvs/initialize
virtualenvwrapper.user_scripts creating /root/.virtualenvs/premkvirtualenv
virtualenvwrapper.user_scripts creating /root/.virtualenvs/postmkvirtualenv
virtualenvwrapper.user_scripts creating /root/.virtualenvs/prermvirtualenv
virtualenvwrapper.user_scripts creating /root/.virtualenvs/postrmvirtualenv
virtualenvwrapper.user_scripts creating /root/.virtualenvs/predeactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/postdeactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/preactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/postactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/get_env_details
3.使用 mkvirtualenv 命令创建虚拟环境
[root@localhost bin]# mkvirtualenv env27 # 创建env27虚拟环境,创建完成后切换到env27虚拟环境
New python executable in /root/.virtualenvs/env27/bin/python2
Also creating executable in /root/.virtualenvs/env27/bin/python
Installing setuptools, pip, wheel...done.
virtualenvwrapper.user_scripts creating /root/.virtualenvs/env27/bin/predeactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/env27/bin/postdeactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/env27/bin/preactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/env27/bin/postactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/env27/bin/get_env_details
(env27) [root@localhost bin]# pip list # 列出env27虚拟环境下所有模块
Package Version
---------- -------
pip 10.0.1
setuptools 40.0.0
wheel 0.31.1
(env27) [root@localhost bin]# python # 进入env27虚拟环境的python解释器
Python 2.7.5 (default, Apr 11 2018, 07:36:10)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
(env27) [root@localhost bin]# pip install -i https://pypi.douban.com/simple django==1.11.6 # 在env27虚拟环境中安装1.11.6版本的Django
Looking in indexes: https://pypi.douban.com/simple
Collecting django==1.11.6
Downloading https://pypi.doubanio.com/packages/82/33/f9d2871f3aed5062661711bf91b3ebb03daa52cc0e1c37925f3e0c4508c5/Django-1.11.6-py2.py3-none-any.whl (6.9MB)
100% |████████████████████████████████| 7.0MB 468kB/s
Collecting pytz (from django==1.11.6)
Downloading https://pypi.doubanio.com/packages/30/4e/27c34b62430286c6d59177a0842ed90dc789ce5d1ed740887653b898779a/pytz-2018.5-py2.py3-none-any.whl (510kB)
100% |████████████████████████████████| 512kB 521kB/s
Installing collected packages: pytz, django
Successfully installed django-1.11.6 pytz-2018.5
(env27) [root@localhost bin]# pip list # 列出env27虚拟环境下所有模块
Package Version
---------- -------
Django 1.11.6
pip 10.0.1
pytz 2018.5
setuptools 40.0.0
wheel 0.31.1
(env27) [root@localhost bin]# pip list | grep Django
Django 1.11.6
(env27) [root@localhost bin]# deactivate # 退出env27虚拟环境
4.安装指定python版本的虚拟环境
[root@localhost ~]# mkvirtualenv env36 -p /usr/local/python3/bin/python3.6 # 指定python解释器路径创建env36虚拟环境
Running virtualenv with interpreter /usr/local/python3/bin/python3.6
Using base prefix '/usr/local/python3'
New python executable in /root/.virtualenvs/env36/bin/python3.6
Also creating executable in /root/.virtualenvs/env36/bin/python
Installing setuptools, pip, wheel...done.
virtualenvwrapper.user_scripts creating /root/.virtualenvs/env36/bin/predeactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/env36/bin/postdeactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/env36/bin/preactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/env36/bin/postactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/env36/bin/get_env_details
(env36) [root@localhost ~]# python # 打开env36虚拟环境中的python解释器
Python 3.6.5rc1 (default, Jul 12 2018, 22:57:46)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
(env36) [root@localhost ~]# pip list # 列出env36虚拟环境下安装的所有模块
Package Version
---------- -------
pip 10.0.1
setuptools 40.0.0
wheel 0.31.1
(env36) [root@localhost ~]# pip install -i https://pypi.douban.com/simple django # 在env36虚拟环境中安装最新版本的Django:2.0.7
Looking in indexes: https://pypi.douban.com/simple
Collecting django
Downloading https://pypi.doubanio.com/packages/ab/15/cfde97943f0db45e4f999c60b696fbb4df59e82bbccc686770f4e44c9094/Django-2.0.7-py3-none-any.whl (7.1MB)
100% |████████████████████████████████| 7.1MB 472kB/s
Collecting pytz (from django)
Downloading https://pypi.doubanio.com/packages/30/4e/27c34b62430286c6d59177a0842ed90dc789ce5d1ed740887653b898779a/pytz-2018.5-py2.py3-none-any.whl (510kB)
100% |████████████████████████████████| 512kB 622kB/s
Installing collected packages: pytz, django
(env36) [root@localhost ~]# pip list # 列出env36虚拟环境下安装的所有模块
Package Version
---------- -------
Django 2.0.7
pip 10.0.1
pytz 2018.5
setuptools 40.0.0
wheel 0.31.1
也可以使用 --python 指定python解释器的方式创建虚拟环境,完整命令如下:
mkvirtualenv env36 --python /usr/local/python3/bin/python3.6
5.使用 lsvirtualenv 命令列出所有虚拟环境
(env36) [root@localhost ~]# lsvirtualenv -b # 以简短方式列出当前系统中所有的虚拟环境
env27
env36
(env36) [root@localhost ~]# lsvirtualenv -l # 以长格式方式列出当前系统中所有的虚拟环境
env27
=====
env36
=====
(env36) [root@localhost ~]# lsvirtualenv -h # 显示当前命令的帮助信息
lsvirtualenv [-blh]
-b -- brief mode
-l -- long mode
-h -- this help message
6.使用 workon 命令切换虚拟环境
(env36) [root@localhost ~]# deactivate # 退出env36虚拟环境
[root@localhost ~]# workon env27 # 进入env27虚拟环境
(env27) [root@localhost ~]# pip list # 列出env27虚拟环境下安装的所有模块
Package Version
---------- -------
Django 1.11.6
pip 10.0.1
pytz 2018.5
setuptools 39.1.0
wheel 0.31.1
(env27) [root@localhost ~]# workon env36 # 进入env36虚拟环境
(env36) [root@localhost ~]# pip list # 列出env36虚拟环境下安装的所有模块
Package Version
---------- -------
Django 2.0.7
pip 10.0.1
pytz 2018.5
setuptools 40.0.0
wheel 0.31.1
(env36) [root@localhost ~]#
7.使用 deactivate 命令注销当前虚拟环境
(env36) [root@localhost ~]# deactivate # 退出env36虚拟环境
[root@localhost ~]# workon env27 # 进入env27虚拟环境
(env27) [root@localhost ~]# python # 打开env27虚拟环境的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.
>>> exit()
(env27) [root@localhost ~]# deactivate # 退出env27虚拟环境
[root@localhost ~]#
8.使用 cpvirtualenv 命令复制虚拟环境
[root@localhost ~]# workon env36 # 进入env36虚拟环境
(env36) [root@localhost ~]# cpvirtualenv env36 env36_bak # 复制env36虚拟环境为env36_bak
Copying env36 as env36_bak...
(env36_bak) [root@localhost ~]# python # 打开env36_bak虚拟环境的python解释器
Python 2.7.5 (default, Aug 4 2017, 00:39:18) # python解释器为2.7.5版本
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
(env36_bak) [root@localhost ~]# pip list # 列出env36_bak虚拟环境下安装的所有模块
Package Version
---------- -------
Django 2.0.7
pip 10.0.1
pytz 2018.5
setuptools 40.0.0
wheel 0.31.1
(env36_bak) [root@localhost ~]# cpvirtualenv env27 env27_bak # 复制env27虚拟环境为env27_bak
Copying env27 as env27_bak...
(env27_bak) [root@localhost ~]# python # 打开env27_bak虚拟环境的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.
>>> exit()
(env27_bak) [root@localhost ~]#
在上面的命令结果里,使用cpvirtualenv命令复制得到新的虚拟环境env36_bak,python解释器版本为python2.7.5,是CentOS7系统内置的python版本
但是使用pip list命令列出env36_bak虚拟环境里安装的Django是2.0.7版本的,这是env36虚拟环境里安装的Django版本。
通过查阅官方文档,官方文档上指明了cpvirtualenv命令不完美,使用cpvirtualenv命令需要注意
9.使用 cdsitepackages 命令进入到当前环境的site-packages目录
使用lssitepackages命令列出当前环境中site-packages内容
(env27_bak) [root@localhost ~]# cdsitepackages # 进入到env27_bak虚拟环境的site-packages目录
(env27_bak) [root@localhost site-packages]# lssitepackages # 列出env27_bak虚拟环境中site-packages内容
easy_install.py pip pkg_resources setuptools-40.0.0.dist-info wheel-0.31.1.dist-info
easy_install.pyc pip-10.0.1.dist-info setuptools wheel
(env27_bak) [root@localhost site-packages]# workon env36 # 切换到env36虚拟环境
(env36) [root@localhost site-packages]# cdsitepackages # 进入env36虚拟环境的site-packages目录
(env36) [root@localhost site-packages]# lssitepackages # 列出env36虚拟环境中site-packages内容
django pip-10.0.1.dist-info pytz-2018.5.dist-info wheel-0.31.1.dist-info
Django-2.0.7.dist-info pkg_resources setuptools
easy_install.py __pycache__ setuptools-40.0.0.dist-info
pip pytz wheel
(env36) [root@localhost site-packages]# python # 打开env36虚拟环境的python解释器
Python 3.6.5rc1 (default, Jul 12 2018, 22:57:46)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
(env36) [root@localhost site-packages]#
10.使用 rmvirtualenv 命令删除环境
(env36) [root@localhost site-packages]# lsvirtualenv -b # 列出当前系统中所有的虚拟环境
env27_bak
env27
env36_bak
env36
(env36) [root@localhost site-packages]# rmvirtualenv env36_bak # 删除env36_bak虚拟环境
Removing env36_bak...
(env36) [root@localhost site-packages]# rmvirtualenv env27_bak # 删除env27_bak虚拟环境
Removing env27_bak...
(env36) [root@localhost site-packages]# lsvirtualenv -b # 列出当前系统中所有的虚拟环境
env27
env36
(env36) [root@localhost site-packages]#
11.使用 wipeenv 命令卸载虚拟环境内所有第三方包
(env36) [root@localhost site-packages]# pip list # 列出env36虚拟环境下所有模块
Package Version
---------- -------
Django 2.0.7
pip 10.0.1
pytz 2018.5
setuptools 40.0.0
wheel 0.31.1
(env36) [root@localhost site-packages]# wipeenv # 卸载env36虚拟环境内所有第三方包
Uninstalling packages:
Django==2.0.7
pytz==2018.5
Uninstalling Django-2.0.7:
Successfully uninstalled Django-2.0.7
Uninstalling pytz-2018.5:
Successfully uninstalled pytz-2018.5
(env36) [root@localhost site-packages]# pip list # 列出env36虚拟环境下所有模块
Package Version
---------- -------
pip 10.0.1
setuptools 40.0.0
wheel 0.31.1
(env36) [root@localhost site-packages]# lsvirtualenv -b # 列出当前系统中所有的虚拟环境
env27
env36
(env36) [root@localhost site-packages]# workon env27 # 切换到env27虚拟环境
(env27) [root@localhost site-packages]# pip list # 列出env27虚拟环境下所有模块
Package Version
---------- -------
Django 1.11.6
pip 10.0.1
pytz 2018.5
setuptools 39.1.0
wheel 0.31.1
(env27) [root@localhost site-packages]# wipeenv # 卸载env27虚拟环境内所有第三方包
Uninstalling packages:
Django==1.11.6
pytz==2018.5
Uninstalling pytz-2018.5:
Successfully uninstalled pytz-2018.5
Uninstalling Django-1.11.6:
Successfully uninstalled Django-1.11.6
(env27) [root@localhost site-packages]# pip list # 列出env27虚拟环境下所有模块
Package Version
---------- -------
pip 10.0.1
setuptools 39.1.0
wheel 0.31.1
(env27) [root@localhost site-packages]# deactivate # 退出env27虚拟环境