继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

Python虚拟环境之virtualenvwrapper vs Pipenv

2018-01-05 16:18:3516157浏览

麦兜搞IT

2实战 · 11手记 · 12推荐
TA的实战

今年年初,Kenneth Reitz发布了Pipenv (https://www.kennethreitz.org/essays/announcing-pipenv), 目前已经是Python官方推荐的包管理工具(https://docs.pipenv.org/)。
个人之前一直都是用virtualenvwrapper (https://virtualenvwrapper.readthedocs.io/en/latest/),
virtualenvwrapper基本保持了大家之前对pip,virtualenv和requirements.txt的使用习惯,但是强大和方便了许多。如果要迁移到pipenv,需要对已有项目的包管理方式做较大的改变,因为pipenv引入了新的机制。

virtualenvwrapper

virtualenvwrapper的方便之处在于不同项目的切换,以及同一项目不同虚拟环境之间的快速切换。

install
$ pip install virtualenvwrapper
...
$ export WORKON_HOME=~/Envs
$ mkdir -p $WORKON_HOME
$ source /usr/local/bin/virtualenvwrapper.sh

安装完以后,两个常用的命令一个是mkvirtualenvrmvirtualenv. 所create的虚拟环境都会放到WORKON_HOME里。

create a project

可以创建一个python3的虚拟环境。创建的同时,安装requirements.txt的依赖到虚拟环境里,并且进入到虚拟环境。

  $ mkdir demo
  $ cd demo
  $ touch requirements.txt
  $ echo "flask" >> requirements.txt
  $ mkvirtualenv --python=/usr/local/bin/python3.5 demo3 -a . -r requirements.txt
Running virtualenv with interpreter /usr/local/bin/python3.5
Using base prefix '/Library/Frameworks/Python.framework/Versions/3.5'
New python executable in demo3/bin/python3.5
Also creating executable in demo3/bin/python
Installing setuptools, pip, wheel...done.
virtualenvwrapper.user_scripts creating /Users/penxiao/PycharmProjects/virtualenv/demo3/bin/predeactivate
virtualenvwrapper.user_scripts creating /Users/penxiao/PycharmProjects/virtualenv/demo3/bin/postdeactivate
virtualenvwrapper.user_scripts creating /Users/penxiao/PycharmProjects/virtualenv/demo3/bin/preactivate
virtualenvwrapper.user_scripts creating /Users/penxiao/PycharmProjects/virtualenv/demo3/bin/postactivate
virtualenvwrapper.user_scripts creating /Users/penxiao/PycharmProjects/virtualenv/demo3/bin/get_env_details
Setting project for demo3 to /Users/penxiao/tmp/demo
You are using pip version 7.1.0, however version 9.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
Collecting flask (from -r requirements.txt (line 1))
  Using cached Flask-0.12.2-py2.py3-none-any.whl
....
....
Successfully installed Jinja2-2.10 MarkupSafe-1.0 Werkzeug-0.13 click-6.7 flask-0.12.2 itsdangerous-0.24
(demo3)  demo

可以再创建一个Python2的虚拟环境。

(demo3)  demo$ mkvirtualenv demo2 -a . -r requirements.txt
New python executable in demo2/bin/python
Installing setuptools, pip, wheel...done.
virtualenvwrapper.user_scripts creating /Users/penxiao/PycharmProjects/virtualenv/demo2/bin/predeactivate
virtualenvwrapper.user_scripts creating /Users/penxiao/PycharmProjects/virtualenv/demo2/bin/postdeactivate
virtualenvwrapper.user_scripts creating /Users/penxiao/PycharmProjects/virtualenv/demo2/bin/preactivate
virtualenvwrapper.user_scripts creating /Users/penxiao/PycharmProjects/virtualenv/demo2/bin/postactivate
virtualenvwrapper.user_scripts creating /Users/penxiao/PycharmProjects/virtualenv/demo2/bin/get_env_details
Setting project for demo2 to /Users/penxiao/tmp/demo
You are using pip version 7.1.0, however version 9.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
Collecting flask (from -r requirements.txt (line 1))
  Using cached Flask-0.12.2-py2.py3-none-any.whl
....
(demo2)  demo

通过workon命令可以在不同的project和虚拟环境里切换。并且可以自动cd到项目目录。

(demo2)  demo$ workon demo3
(demo3)  demo$ deactivate
cd%                                                                                                                                                                           demo cd
  ~ workon demo2
(demo2)  demo
Pipenv

pipenv基本抛弃了requirements.txt这一套东西,引入了PipfilePipfile.lock两个文件对虚拟环境进行管理。

install
$ pip install pipenv
create a project
  $ cd demo
  $ pipenv --python 2.7
Creating a virtualenv for this project…
Using /usr/bin/python2.7 to create virtualenv…
⠋Running virtualenv with interpreter /usr/bin/python2.7
New python executable in /Users/penxiao/PycharmProjects/virtualenv/demo-hgu7fb10/bin/python
Installing setuptools, pip, wheel...done.

Virtualenv location: /Users/penxiao/PycharmProjects/virtualenv/demo-hgu7fb10
Creating a Pipfile for this project…
  $ ls
Pipfile
  $ pipenv install flask
Installing flask…
Collecting flask
  Using cached Flask-0.12.2-py2.py3-none-any.whl
Collecting click>=2.0 (from flask)
  Using cached click-6.7-py2.py3-none-any.whl
Collecting itsdangerous>=0.21 (from flask)
Collecting Werkzeug>=0.7 (from flask)
  Using cached Werkzeug-0.13-py2.py3-none-any.whl
Collecting Jinja2>=2.4 (from flask)
  Using cached Jinja2-2.10-py2.py3-none-any.whl
Collecting MarkupSafe>=0.23 (from Jinja2>=2.4->flask)
Installing collected packages: click, itsdangerous, Werkzeug, MarkupSafe, Jinja2, flask
Successfully installed Jinja2-2.10 MarkupSafe-1.0 Werkzeug-0.13 click-6.7 flask-0.12.2 itsdangerous-0.24

Adding flask to Pipfile's [packages]…
Locking [dev-packages] dependencies…
Locking [packages] dependencies…
Updated Pipfile.lock (36eec0)!
  $ ls
Pipfile      Pipfile.lock
  $

Pipfile里存储了project demo的依赖,比如当前是在Python2.7环境下依赖flask。 而Pipfile.lock文件里则详细记录了深层次的依赖关系,包括flask的当前版本,哈希,已经flask
所依赖的所有package的版本号和哈希值。

run

如果想在虚拟环境里运行代码,可以pipenv shell进入虚拟环境,然后可以run代码,最后通过exit退出。

  $ pipenv shell
Spawning environment shell (/bin/zsh). Use 'exit' to leave.
source /Users/penxiao/PycharmProjects/virtualenv/demo-hgu7fb10/bin/activate
  demo source /Users/penxiao/PycharmProjects/virtualenv/demo-hgu7fb10/bin/activate
(demo-hgu7fb10)  demo
(demo-hgu7fb10)  demo exit
  demo

Pipenv目前有一个feature没有实现,就是如何一个项目里并行多个虚拟环境,就像之前virtualenvwrapper一个project,Python2和Python3自由切换(https://github.com/pypa/pipenv/issues/1071
但是官方貌似不打算实现这个。

更多的详细内容可以阅读pipenv的文档或者github repo

https://github.com/pypa/pipenv

https://docs.pipenv.org/

summary

总体来说,pipenv在严谨性和安全性上完全胜出,在易用性上也ok。但是在多个环境和项目管理上有所欠缺,如何和tox等工具集成也问题很大。

打开App,阅读手记
7人推荐
发表评论
随时随地看视频慕课网APP

热门评论

pipenv缺少的是指多个虚拟环境的切换吧,其实用过virtaulevnwrapper就知道workon的好,而pipenv是没有workon那一套快速切换的,这个在官方github里面也有讨论,但是作者推荐用pew解决。就我角度来看,pipenv还不成熟,我比较看好的是他graph的功能,因为在原来requirements.txt一套里面很难看得出一来关系,如果想后期挑出top-level的包,还得另外下软件.希望pipenv能够集成workon那一套,那么真的一站式解决包管理和环境管理的神器就出世了。

查看全部评论