首先放上virtualenv和virtualenvwrapper的文档,自己动手,丰衣足食。
http://virtualenv-chinese-docs.readthedocs.io/en/latest/#id29
http://virtualenvwrapper.readthedocs.io/en/latest/index.html#
配置Ubuntu,安装虚拟环境相关包virtualenv和virtualenvwrapper
使用如下两条命令,视情况在命令前加sudo
pip install virtualenv
pip install virtualenvwrapper
安装好后编辑`~`目录下`.bashrc'文件,加入三行代码
export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh
VIRTUALENVWRAPPER_PYTHON=/usr/bin/python
保存后使用命令`source .bashrc`使文件生效,此时系统报错
root@vultr:~# source .bashrc
/usr/bin/python: No module named virtualenvwrapper
virtualenvwrapper.sh: There was a problem running the initialization hooks.
If Python could not import the module virtualenvwrapper.hook_loader,
check that virtualenvwrapper has been installed for
VIRTUALENVWRAPPER_PYTHON=/usr/bin/python and that PATH is
set properly.
`cd`到`/usr/bin/`目录下`ll`查看`python`文件
lrwxrwxrwx 1 root root 9 Nov 24 2017 python -> python2.7*
lrwxrwxrwx 1 root root 9 Nov 24 2017 python2 -> python2.7*
-rwxr-xr-x 1 root root 3492656 Dec 5 2017 python2.7*
lrwxrwxrwx 1 root root 33 Dec 5 2017 python2.7-config -> x86_64-linux-gnu-python2.7-config*
lrwxrwxrwx 1 root root 16 Nov 24 2017 python2-config -> python2.7-config*
lrwxrwxrwx 1 root root 9 Jun 8 04:55 python3 -> python3.5*
-rwxr-xr-x 2 root root 4464400 Nov 29 2017 python3.5*
再查看`pip`命令
root@vultr:~# pip --version
pip 8.0.2 from /usr/local/lib/python3.5/dist-packages/pip-8.0.2-py3.5.egg (python 3.5)
原因很明显了,再查找一下virtualenvwrapper,`cd /`进入根目录,`find / -name "virtualenv*"`,结果如下
root@vultr:/# find / -name "virtualenv*"
/usr/local/bin/virtualenv
/usr/local/bin/virtualenv-clone
/usr/local/bin/virtualenvwrapper.sh
/usr/local/bin/virtualenvwrapper_lazy.sh
/usr/local/lib/python3.5/dist-packages/virtualenv_clone-0.3.0.dist-info
/usr/local/lib/python3.5/dist-packages/virtualenv-16.0.0.dist-info
/usr/local/lib/python3.5/dist-packages/__pycache__/virtualenv.cpython-35.pyc
/usr/local/lib/python3.5/dist-packages/virtualenv_support
/usr/local/lib/python3.5/dist-packages/virtualenvwrapper-4.8.2.dist-info
/usr/local/lib/python3.5/dist-packages/virtualenv.py
/usr/local/lib/python3.5/dist-packages/virtualenvwrapper-4.8.2-py2.7-nspkg.pth
/usr/local/lib/python3.5/dist-packages/virtualenvwrapper
virtualenvwrapper安装没问题
Ubuntu安装了2.7和3.5两个版本的python,在安装时使用的是`pip install virtualenvwrapper`
在我运行的时候默认使用的是python2.7,但在python2.7中不存在对应的模块。
再回头编辑`.bashrc`,修改配置
export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh
VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
保存后再`source .bashrc`
root@vultr:~# source .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
然后就可以愉快地mkvirtualenv了。
root@vultr:~# mkvirtualenv test
Using base prefix '/usr'
New python executable in /root/.virtualenvs/test/bin/python3
Not overwriting existing python script /root/.virtualenvs/test/bin/python (you must use /root/.virtualenvs/test/bin/python3)
Installing setuptools, pip, wheel...done.
virtualenvwrapper.user_scripts creating /root/.virtualenvs/test/bin/predeactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/test/bin/postdeactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/test/bin/preactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/test/bin/postactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/test/bin/get_env_details
(test) root@vultr:~# deactivate
root@vultr:~#
删除虚拟环境
root@vultr:/# rmvirtualenv test
Removing test...
此时创建的虚拟环境默认使用python3.5
root@vultr:/# mkvirtualenv flask_py2
Using base prefix '/usr'
New python executable in /root/.virtualenvs/flask_py2/bin/python3
Not overwriting existing python script /root/.virtualenvs/flask_py2/bin/python (you must use /root/.virtualenvs/flask_py2/bin/python3)
Installing setuptools, pip, wheel...done.
virtualenvwrapper.user_scripts creating /root/.virtualenvs/flask_py2/bin/predeactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/flask_py2/bin/postdeactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/flask_py2/bin/preactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/flask_py2/bin/postactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/flask_py2/bin/get_env_details
(flask_py2) root@vultr:/# python --version
Python 3.5.2
(flask_py2) root@vultr:/# deactivate
root@vultr:/# rmvirtualenv flask_py2
Removing flask_py2...
想要创建使用python2.7的虚拟环境,使用如下命令
root@vultr:/home/flask_py2# mkvirtualenv flask_py2 --python=python2.7
Running virtualenv with interpreter /usr/bin/python2.7
New python executable in /root/.virtualenvs/flask_py2/bin/python2.7
Also creating executable in /root/.virtualenvs/flask_py2/bin/python
Installing setuptools, pip, wheel...done.
virtualenvwrapper.user_scripts creating /root/.virtualenvs/flask_py2/bin/predeactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/flask_py2/bin/postdeactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/flask_py2/bin/preactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/flask_py2/bin/postactivate
virtualenvwrapper.user_scripts creating /root/.virtualenvs/flask_py2/bin/get_env_details
(flask_py2) root@vultr:/home/flask_py2# python --version
Python 2.7.12
(flask_py2) root@vultr:/home/flask_py2#