[root@localhost ~]# pip install virtualenvwrapper # 安裝virtualenvwrapper [root@localhost ~]# pip list | grep virtualenvwrapper virtualenvwrapper (4.8.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
[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虛擬環境
[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解釋器的方式建立虛擬環境,完整命令以下:python
mkvirtualenv env36 --python /usr/local/python3/bin/python3.6
(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
(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 ~]#
(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 ~]#
[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版本react
可是使用pip list命令列出env36_bak虛擬環境裏安裝的Django是2.0.7版本的,這是env36虛擬環境裏安裝的Django版本。linux
經過查閱官方文檔,官方文檔上指明瞭cpvirtualenv命令不完美,使用cpvirtualenv命令須要注意django
使用lssitepackages命令列出當前環境中site-packages內容bash
(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]#
(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]#
(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虛擬環境