virtualenv 通过创建独立Python开发环境的工具,来解决依赖,版本以及间接权限问题,比如一个项目依赖Django1.3而当前全局开发环境Django1.7.版本跨度过大,导致不兼容是项目无法正常运行,使用virtualenv可以解决这些问题
virtualenv创建一个拥有自己安装目录环境,这个环境不与其他环境共享库,能够方便的管理python版本和管理python库
1、安装Virtualenv
pip install virtualenv
或者由于权限问题使用sudo临时提升权限
sudo pip install virtualenv
student@student-VirtualBox:~$ sudo pip install virtualenv
[sudo] password for student:
# 输入密码后回车, 显示以下内容
Downloading/unpacking virtualenv
Downloading virtualenv-15.1.0.tar.gz (1.9Mb): 1.9Mb downloaded
Running setup.py egg_info for package virtualenv
warning: no previously-included files matching '*' found under directory 'docs/_templates'
warning: no previously-included files matching '*' found under directory 'docs/_build'
Installing collected packages: virtualenv
Running setup.py install for virtualenv
warning: no previously-included files matching '*' found under directory 'docs/_templates'
warning: no previously-included files matching '*' found under directory 'docs/_build'
Installing virtualenv script to /usr/local/bin
Successfully installed virtualenv
Cleaning up...
2、virtualenv的基本使用
现在开始使用virtualenv管理python环境
student@student-VirtualBox:~$ mkdir virtualenv_demo
# 创建一个名为 'virtualenv_demo' 的文件夹
student@student-VirtualBox:~$ cd virtualenv_demo/
# 进入到该文件夹
student@student-VirtualBox:~/virtualenv_demo$ virtualenv env_local
# 创建一个名为env_local的目录,并安装了env/bin/python,创建了lib,include,
bin目录,安装了pip
Using base prefix '/home/student/.pyenv/versions/3.4.3'
New python executable in /home/student/virtualenv_demo/env_local/bin/python
Installing setuptools, pip, wheel...done.
student@student-VirtualBox:~$ cd env # 进入到env
student@student-VirtualBox:~/env$ ls # 查看文件下的内容
bin include lib pip-selfcheck.json
lib 所有安装的python库都会放在这个目录中的lib/pythonx.x/site-packags/下
bin bin/python是在当前环境时使用的python解释器
如果在命令行中运行virtualenv --system-site-packages env,会继承/usr/bin/python2.7/site-packages下的所有库,最新版本virtualenv把访问全局site-packages作为默认行为
default behavior。
2.1 激活virtualenv
# 激活env_local
student@student-VirtualBox:~/virtualenv_demo$ source env_local/bin/activate
# 查看该环境下的python版本, 这种方式创建的是全局的python版本
(env_local) student@student-VirtualBox:~/virtualenv_demo$ python
Python 3.4.3 (default, Aug 25 2017, 11:42:22)
[GCC 4.6.3] on linux
Type "help", "copyright", "credits" or "license" for more information.
# 退出python
>>> exit()
2.2 关闭virtualenv
(env_local) student@student-VirtualBox:~/virtualenv_demo$ deactivate
2.3 指定python版本
可以使用 -p PYTHON_EXE选项在创建虚拟环境的时候指定python版本
# 找到python2.7版本的文件位置
student@student-VirtualBox:~/virtualenv_demo$ which python2.7
/usr/bin/python2.7
# 创建python2.7虚拟环境
student@student-VirtualBox:~/virtualenv_demo$ virtualenv -p /usr/bin/python2.7 env2.7
Running virtualenv with interpreter /usr/bin/python2.7
New python executable in /home/student/virtualenv_demo/env2.7/bin/python2.7
Also creating executable in /home/student/virtualenv_demo/env2.7/bin/python
Installing setuptools, pip, wheel...done.
# 激活
student@student-VirtualBox:~/virtualenv_demo$ source env2.7/bin/activate
# 查看python版本
(env2.7) student@student-VirtualBox:~/virtualenv_demo$ python
Python 2.7.3 (default, Oct 26 2016, 21:01:49)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
# 退出
>>> exit()
# 关闭
(env2.7) student@student-VirtualBox:~/virtualenv_demo$ deactivate
# 找到 python3.4文件位置
student@student-VirtualBox:~/virtualenv_demo$ which python3.4
/home/student/.pyenv/shims/python3.4
# 创建python3.4虚拟环境
student@student-VirtualBox:~/virtualenv_demo$ virtualenv -p /home/student/.pyenv/shims/python3.4 env3.4
Running virtualenv with interpreter /home/student/.pyenv/shims/python3.4
Using base prefix '/home/student/.pyenv/versions/3.4.3'
New python executable in /home/student/virtualenv_demo/env3.4/bin/python3.4
Also creating executable in /home/student/virtualenv_demo/env3.4/bin/python
Installing setuptools, pip, wheel...done.
# 激活
student@student-VirtualBox:~/virtualenv_demo$ source env3.4/bin/activate
# 查看python版本
(env3.4) student@student-VirtualBox:~/virtualenv_demo$ python
Python 3.4.3 (default, Aug 25 2017, 11:42:22)
[GCC 4.6.3] on linux
Type "help", "copyright", "credits" or "license" for more information.
# 退出
>>> exit()
#关闭
(env3.4) student@student-VirtualBox:~/virtualenv_demo$ deactivate
到此已经解决python版本冲突问题和python库不同版本的问题
3 生成可打包环境
某些特殊需求下,可能没有网络,我们期望打包一个env,可以解压后直接使用,这时候可以使用virtualenv -relocatable 指令将env修改为可更改位置的env
# 进入到想要打包的虚拟环境中
student@student-VirtualBox:~/virtualenv_demo$ cd env_local/
student@student-VirtualBox:~/virtualenv_demo/env_local$ ls
bin include lib pip-selfcheck.json
# 对当前已经创建的虚拟环境更改为可迁移
student@student-VirtualBox:~/virtualenv_demo/env_local$ virtualenv --relocatable ./
Making script /home/student/virtualenv_demo/env_local/bin/pip relative
Making script /home/student/virtualenv_demo/env_local/bin/easy_install-3.4 relative
Making script /home/student/virtualenv_demo/env_local/bin/easy_install relative
Making script /home/student/virtualenv_demo/env_local/bin/wheel relative
Making script /home/student/virtualenv_demo/env_local/bin/python-config relative
Making script /home/student/virtualenv_demo/env_local/bin/pip3 relative
Making script /home/student/virtualenv_demo/env_local/bin/pip3.4 relative