ubuntu16已经安装好了py2和py3(自带的)
bigni@bigni-Latitude-E6230:~/python_file/Django_project$ python
python python2-config python3.5m python3m-config
python2 python3 python3.5m-config python-config
python2. python3. python3-config
python2.-config python3.-config python3m
我要用的是py3,所以安装virtulenv+virtualenvwrapper,我用的是pip3,使用:apt-get install python3-pip
可以完成
通过pip安装virtualenv:
$ pip3 install virtualenv
测试你的安装
$ virtualenv --version 使用virtualenv这里就不多说,继续安装virtualenvwrapper
pip3 install virtualenvwrapper
要想使用virtualenvwrapper,需要启动virtualenvwrapper.sh脚本,一般是放在
/usr/local/bin/virtualenvwrapper.sh
不过我安装后没找到,用find命令搜到是在 $HOME/.local/bin/virtualenvwrapper.sh
打开家目录下的.bashrc 在最底部加上下面这一段:
#python虚拟环境配置
export VIRTUALENV_USE_DISTRIBUTE= # 总是使用 pip/distribute
#export WORKON_HOME=$HOME/.local/virtualenvs # 所有虚拟环境存储的目录
if [ -e $HOME/.local/bin/virtualenvwrapper.sh ];then
export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=$HOME/.virtualenvs
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3 #指定py3
source $HOME/.local/bin/virtualenvwrapper.sh
else if [ -e /usr/local/bin/virtualenvwrapper.sh ];then
source /usr/local/bin/virtualenvwrapper.sh
fi
fi
export PIP_VIRTUALENV_BASE=$WORKON_HOME
export PIP_RESPECT_VIRTUALENV=true
PS: 我的VIRTUALENVWRAPPER_PYTHON变量指向的是py3,
启动source ~/.bashrc ,出现下面信息,说明环境配置好了。
bigni@bigni-Latitude-E6230:~/python_file/Django_project$ source ~/.bashrc
virtualenvwrapper.user_scripts creating /home/bigni/Envs/initialize
virtualenvwrapper.user_scripts creating /home/bigni/Envs/premkvirtualenv
virtualenvwrapper.user_scripts creating /home/bigni/Envs/postmkvirtualenv
virtualenvwrapper.user_scripts creating /home/bigni/Envs/prermvirtualenv
virtualenvwrapper.user_scripts creating /home/bigni/Envs/postrmvirtualenv
virtualenvwrapper.user_scripts creating /home/bigni/Envs/predeactivate
virtualenvwrapper.user_scripts creating /home/bigni/Envs/postdeactivate
virtualenvwrapper.user_scripts creating /home/bigni/Envs/preactivate
virtualenvwrapper.user_scripts creating /home/bigni/Envs/postactivate
virtualenvwrapper.user_scripts creating /home/bigni/Envs/get_env_details
virtualenvwrapper.user_scripts creating /home/bigni/Envs/premkproject
virtualenvwrapper.user_scripts creating /home/bigni/Envs/postmkproject
测试下:创建虚拟环境:second_project
bigni@bigni-Latitude-E6230:~/python_file/Django_project$ mkvirtualenv second_project
Using base prefix '/usr'
New python executable in /home/bigni/.virtualenvs/second_project/bin/python3
Also creating executable in /home/bigni/.virtualenvs/second_project/bin/python
Installing setuptools, pip, wheel...done.
virtualenvwrapper.user_scripts creating /home/bigni/.virtualenvs/second_project/bin/predeactivate
virtualenvwrapper.user_scripts creating /home/bigni/.virtualenvs/second_project/bin/postdeactivate
virtualenvwrapper.user_scripts creating /home/bigni/.virtualenvs/second_project/bin/preactivate
virtualenvwrapper.user_scripts creating /home/bigni/.virtualenvs/second_project/bin/postactivate
virtualenvwrapper.user_scripts creating /home/bigni/.virtualenvs/second_project/bin/get_env_details
进入虚拟环境, 安装requests
bigni@bigni-Latitude-E6230:~/python_file/Django_project$ workon second_project
(second_project) bigni@bigni-Latitude-E6230:~/python_file/Django_project$ pip
pip pip2 pip3 pip3.
(second_project) bigni@bigni-Latitude-E6230:~/python_file/Django_project$ pip3 install requests
Collecting requests
Using cached requests-2.18.-py2.py3-none-any.whl
Collecting idna<2.6,>=2.5 (from requests)
Using cached idna-2.5-py2.py3-none-any.whl
Collecting chardet<3.1.,>=3.0. (from requests)
Using cached chardet-3.0.-py2.py3-none-any.whl
Collecting urllib3<1.23,>=1.21. (from requests)
Using cached urllib3-1.22-py2.py3-none-any.whl
Collecting certifi>=2017.4. (from requests)
Using cached certifi-2017.7.27.1-py2.py3-none-any.whl
Installing collected packages: idna, chardet, urllib3, certifi, requests
Successfully installed certifi-2017.7.27.1 chardet-3.0. idna-2.5 requests-2.18. urllib3-1.22
(second_project) bigni@bigni-Latitude-E6230:~/python_file/Django_project$
或者,你可以创建一个项目,它会创建虚拟环境,并在 $PROJECT_HOME
中创建一个项目目录。 当你使用 workon myproject
时,会 cd
-ed 到项目目录中。
bigni@bigni-Latitude-E6230:~$ vim .bashrc
bigni@bigni-Latitude-E6230:~$ source .bashrc
bigni@bigni-Latitude-E6230:~$ mkproject third_project
Using base prefix '/usr'
New python executable in /home/bigni/.virtualenvs/third_project/bin/python3
Also creating executable in /home/bigni/.virtualenvs/third_project/bin/python
Installing setuptools, pip, wheel...done.
virtualenvwrapper.user_scripts creating /home/bigni/.virtualenvs/third_project/bin/predeactivate
virtualenvwrapper.user_scripts creating /home/bigni/.virtualenvs/third_project/bin/postdeactivate
virtualenvwrapper.user_scripts creating /home/bigni/.virtualenvs/third_project/bin/preactivate
virtualenvwrapper.user_scripts creating /home/bigni/.virtualenvs/third_project/bin/postactivate
virtualenvwrapper.user_scripts creating /home/bigni/.virtualenvs/third_project/bin/get_env_details
Creating /home/bigni/.virtualenvs/third_project
Setting project for third_project to /home/bigni/.virtualenvs/third_project
(third_project) bigni@bigni-Latitude-E6230:~/.virtualenvs/third_project$
virtualenvwrapper 提供环境名字的tab补全功能。当你有很多环境, 并且很难记住它们的名字时,这就显得很有用。
workon
也能停止你当前所在的环境,所以你可以在环境之间快速的切换。
- 停止是一样的:
$ deactivate
- 删除:
$ rmvirtualenv my_project
其他有用的命令
lsvirtualenv
- 列举所有的环境。
cdvirtualenv
- 导航到当前激活的虚拟环境的目录中,比如说这样你就能够浏览它的
site-packages
。 cdsitepackages
- 和上面的类似,但是是直接进入到
site-packages
目录中。 lssitepackages
- 显示
site-packages
目录中的内容。
export PROJECT_HOME=$HOME/.virtualenvs