今年年初,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的依赖到虚拟环境里,并且进入到虚拟环境。

➜  tmp mkdir demo
➜  tmp cd demo
➜  demo touch requirements.txt
➜  demo echo "flask" >> requirements.txt
➜  demo 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

➜  tmp cd demo
➜  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…
➜  demo ls
Pipfile
➜  demo 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)!
➜  demo ls
Pipfile      Pipfile.lock
➜  demo

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

run

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

➜  demo 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。但是在多个环境和项目管理上有所欠缺。