为了账号安全,请及时绑定邮箱和手机立即绑定

Python虚拟环境之virtualenvwrapper vs Pipenv

标签:
Python

今年年初,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等工具集成也问题很大。

点击查看更多内容
7人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消