第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

Python虛擬環(huán)境之virtualenvwrapper vs Pipenv

標(biāo)簽:
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等工具集成也问题很大。

點(diǎn)擊查看更多內(nèi)容
7人點(diǎn)贊

若覺(jué)得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說(shuō)多少就多少
贊賞金額會(huì)直接到老師賬戶(hù)
支付方式
打開(kāi)微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得

100積分直接送

付費(fèi)專(zhuān)欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消