首页技术文章正文

搭建Python独立环境:virtualenv的应用

更新时间:2018-07-27 来源:黑马程序员技术社区 浏览量:

virtualenv简介和安装

virtualenv是Github上的一个项目,按照它的原话就是『Python虚拟环境的构建者』,这是它的项目地址:Virtual Python Environment builder

这里简单介绍下它的工作原理:virtualenv把运行Python程序必须的基本环境,包括二进制Python自身、Python标准库、pip安装器,以及至关重要的site-packages目录,全部拷贝一份到一个完全隔离的目录下,这个目录就是你的独立开环境

在项目首页最下面有安装教程的超链接:Installation,你可以简单敲pip命令进行安装,这是最简便的方法

pip install virtualenv

另外还用第二种方法:把项目整个当下来,然后手动安装

# Use virtualenv globally
$ curl -O https://pypi.python.org/packages ... rtualenv-X.X.tar.gz
$ tar xvfz virtualenv-X.X.tar.gz
$ cd virtualenv-X.X
$ [sudo] python setup.py install

# Use virtualenv locally
$ curl -O https://pypi.python.org/packages ... rtualenv-X.X.tar.gz
$ tar xvfz virtualenv-X.X.tar.gz
$ cd virtualenv-X.X
$ python virtualenv.py myVE
事实上你需要全局安装的Python套件只有两个,即pip和virtualenv,如果 把virtualenv包括的pip算进去,那其实只要安装一个virtualenv已足矣

使用virtualenv创建独立项目

在安装好virtualenv后,就可以创建隔离环境的Python项目了,下面以demo这个项目为例

1. 创建项目根目录,命名为demo并进入该目录

➜  ~ mkdir demo
➜  ~ cd demo
➜  demo 

2. 使用virtualenv命令,建议加上–no-site-packages选项

➜  demo virtualenv --no-site-packages venv
Using base prefix '/Library/Frameworks/Python.framework/Versions/3.6'
New python executable in /Users/macbookpro/demo/venv/bin/python3.6
Also creating executable in /Users/macbookpro/demo/venv/bin/python
Installing setuptools, pip, wheel...done.

3. 如果想要手动指定Python版本,请使用–python=$PYTHON_VERSION选项

➜  demo virtualenv --python=python2.7 --no-site-packages venv1
Running virtualenv with interpreter /usr/local/bin/python2.7
New python executable in /Users/macbookpro/demo/venv1/bin/python2.7
Also creating executable in /Users/macbookpro/demo/venv1/bin/python
Installing setuptools, pip, wheel...done.

4. 你甚至可以指定系统没有安装的Python版本,比如我这里的Python3.7,但是绝对不要想当然的填写Pyhton版本号否则会报错“doesn’t exist”

➜  demo virtualenv --python=python3.7 --no-site-packages venv2
Running virtualenv with interpreter /usr/local/bin/python3.7
Using base prefix '/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7'
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/virtualenv.py:1041: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses
  import imp
New python executable in /Users/macbookpro/demo/venv2/bin/python3.7
Also creating executable in /Users/macbookpro/demo/venv2/bin/python
Installing setuptools, pip, wheel...done.
➜  demo virtualenv --python=python4.0 --no-site-packages venv3
The path python4.0 (from --python=python4.0) does not exist


5. 在新建的venv目录下,重点关注bin目录,它包括了pip管理器和python对应版本的解释器Pythonx.x.

➜  venv ls
bin                include            lib                pip-selfcheck.json
➜  venv cd bin
➜  bin ls
activate         easy_install     pip3.6           python3.6
activate.csh     easy_install-3.6 python           wheel
activate.fish    pip              python-config
activate_this.py pip3             python3

➜  demo venv/bin/pip install requests
Collecting requests
  Downloading https://files.pythonhosted.org/p ... y2.py3-none-any.whl (91kB)
    100% |████████████████████████████████| 92kB 138kB/s 
Collecting idna<2.8,>=2.5 (from requests)
  Downloading https://files.pythonhosted.org/p ... y2.py3-none-any.whl (58kB)
    100% |████████████████████████████████| 61kB 52kB/s 
Collecting urllib3<1.24,>=1.21.1 (from requests)
  Downloading https://files.pythonhosted.org/p ... y2.py3-none-any.whl (133kB)
    100% |████████████████████████████████| 143kB 29kB/s 
Collecting chardet<3.1.0,>=3.0.2 (from requests)
  Downloading https://files.pythonhosted.org/p ... y2.py3-none-any.whl (133kB)
    100% |████████████████████████████████| 143kB 53kB/s 
Collecting certifi>=2017.4.17 (from requests)
  Downloading https://files.pythonhosted.org/p ... y2.py3-none-any.whl (150kB)
    100% |████████████████████████████████| 153kB 41kB/s 
Installing collected packages: idna, urllib3, chardet, certifi, requests
Successfully installed certifi-2018.4.16 chardet-3.0.4 idna-2.7 requests-2.19.1 urllib3-1.23


6. 用virtual内置pip安装的包被放在lib目录下

➜  demo venv/bin/pip show requests
Name: requests
Version: 2.19.1
Summary: Python HTTP for Humans.
Home-page: http://python-requests.org
Author: Kenneth Reitz
Author-email: me@kennethreitz.org
License: Apache 2.0
Location: /Users/macbookpro/demo/venv/lib/python3.6/site-packages
Requires: idna, certifi, chardet, urllib3
Required-by:

7. 为了减少打字,可激活activate文件,这样原来的venv/bin/pip就要写pip,代替了系统原来的pip命令,不用时激活deactivate命令即可

➜  demo source venv/bin/activate
(venv) ➜  demo pip list
Package    Version  
---------- ---------
certifi    2018.4.16
chardet    3.0.4    
idna       2.7      
pip        10.0.1   
requests   2.19.1   
setuptools 40.0.0   
urllib3    1.23     
wheel      0.31.1   
(venv) ➜  demo pip uninstall requests
Uninstalling requests-2.19.1:
  Would remove:
    /Users/macbookpro/demo/venv/lib/python3.6/site-packages/requests-2.19.1.dist-info/*
    /Users/macbookpro/demo/venv/lib/python3.6/site-packages/requests/*
Proceed (y/n)? y
  Successfully uninstalled requests-2.19.1
(venv) ➜  demo pip list
Package    Version  
---------- ---------
certifi    2018.4.16
chardet    3.0.4    
idna       2.7      
pip        10.0.1   
setuptools 40.0.0   
urllib3    1.23     
wheel      0.31.1   
(venv) ➜  demo 
(venv) ➜  demo deactivate 
➜  demo 


8. pip和virtualenv合作使用起来很爽,pip使用-r参数指定requirement文件

pip install -r requirements.txt
1
ok,这样基本的隔离Python开发环境就被创建出来啦~关于virtualenv暂就讲到这里,还有些比较方便的功能有待大家共同探索^ ^

参考 
ಥ_ಥ A non-magical introduction to Pip and Virtualenv for Python beginners 
ಥ_ಥ Requirements Files

备注:Python2.x和3.x的site-packages目录 
Python2.7:/Library/Python/2.7/site-packages 
Python3.6:/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages


本文版权归黑马程序员JavaEE学院所有,欢迎转载,转载请注明作者出处。谢谢!

作者:黑马程序员JavaEE培训学院

首发:http://java.itheima.com/

分享到:
在线咨询 我要报名
和我们在线交谈!