上一篇章写了单独安装python3 virtualenv来解决python的虚拟环境问题,但是发现执行命令还是有些复杂。
本次加上安装virtualenvwrapper
来解决命令复杂的问题。下面重头开始安装一遍。
pip3 安装 virtualenv
pip3 install virtualenv
执行将virtualenv
的二进制可执行文件软链接到/usr/bin
目录即可。
ln -s /usr/local/python3/bin/virtualenv /usr/bin/virtualenv
pip3 安装 virtualenvwrapper
安装虚拟环境包装器的目的是使用更加简单的命令来管理虚拟环境。
pip3 install virtualenvwrapper
配置环境变量
修改用户home目录下的配置文件.bashrc,添加如下内容:
export WORKON_HOME=$HOME/.virtualenvs
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
source /usr/local/python3/bin/virtualenvwrapper.sh
使用source .bashrc
命令使配置文件生效。
注意:有时候/usr/local/python3/bin/virtualenvwrapper.sh
并不一定是在这个路径下,可以按照下面的方法查找一下。
[root@centos6-test ~]# find / -name 'virtualenvwrapper.sh'
/usr/local/python3/bin/virtualenvwrapper.sh
执行操作如下:
[root@centos6 ~]# ls /usr/local/bin/virtualenvwrapper.sh
/usr/local/bin/virtualenvwrapper.sh
[root@centos6 ~]# cd ~
[root@centos6 ~]# vim .bashrc
export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh
[root@centos6 ~]# source .bashrc
-bash: /root/pyenv/python27/bin/activate: No such file or directory
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@centos6 ~]#
[root@centos6 ~]# ls -ll -a | grep vir
drwxr-xr-x 2 root root 4096 May 28 15:49 .virtualenvs
[root@centos6 ~]#
创建python3虚拟环境
mkvirtualenv -p python3 虚拟环境名称
例:
mkvirtualenv -p python3 py_django
操作如下:
# 创建一个放置虚拟环境的路径
[root@centos6 opt]# mkdir test_venv
[root@centos6 opt]# cd test_venv/
[root@centos6 test_venv]# ls
# 创建虚拟环境
[root@centos6 test_venv]# mkvirtualenv -p python3 py_django
Running virtualenv with interpreter /usr/local/bin/python3
Using base prefix '/usr/local'
New python executable in /root/.virtualenvs/py_django/bin/python3
Not overwriting existing python script /root/.virtualenvs/py_django/bin/python (you must use /root/.virtualenvs/py_django/bin/python3)
Installing setuptools, pip, wheel...
done.
(py_django) [root@centos6 test_venv]# ls
(py_django) [root@centos6 test_venv]#
退出虚拟环境
deactivate
(py_django) [root@centos6 test_venv]# de
deactivate deallocvt debugfs declare delpart depmod
(py_django) [root@centos6 test_venv]# deactivate
[root@centos6 test_venv]# ls
[root@centos6 test_venv]#
查看所有虚拟环境
查看所有虚拟环境的命令如下:
提示:workon后面有个空格,再按两次tab键。
workon 两次tab键
[root@centos6 test_venv]# workon py_django
py_django
[root@centos6 test_venv]#
重新进入虚拟环境工作
[root@centos6 test_venv]# workon py_django
(py_django) [root@centos6 test_venv]#
(py_django) [root@centos6 test_venv]#
删除虚拟环境
删除虚拟环境的命令如下:
rmvirtualenv 虚拟环境名称
例:
先退出:deactivate
再删除:rmvirtualenv py_django
(py_django) [root@centos6 test_venv]#
(py_django) [root@centos6 test_venv]# deactivate
[root@centos6 test_venv]#
[root@centos6 test_venv]# rmvirtualenv py_django
Removing py_django...
[root@centos6 test_venv]#