以前我一直在用 virtualenv + virtualenvwrapper 插件,其實用的也好好的稽煤,就是依賴不能像 node 的 yarn 一樣區(qū)分開發(fā)和生產(chǎn)核芽,而且沒有 lock 文件,鎖定版本都靠 requirements.txt 文件酵熙,略有不便轧简。今天無聊搜了一下 python 的包管理軟件,發(fā)現(xiàn)了 pipenv匾二。初步看了下介紹 哮独,這不就是我想要的嘛拳芙。遂嘗試將以前的一個爬蟲項目轉(zhuǎn)為使用 pipenv 管理。
安裝 pipenv
安裝 pipenv 非常容易皮璧,只要用 pip install pipenv
命令安裝就好舟扎。
基本使用方法
先進入項目目錄
cd ~/dev/mh
安裝相關(guān)依賴
pipenv install Scrapy
這時候注意了,當(dāng)項目還沒有建立 env 的時候悴务,pipenv 會自動的建立一個對應(yīng)當(dāng)前項目的 env 環(huán)境睹限,并把依賴包安裝進去。在我的機器上是在 ~/.virtualenvs
文件夾里面讯檐。然后會生成 Pipfile
文件邦泄,里面有依賴包的信息,格式是 toml裂垦。我注意到里面還有指定 url 可以使用國內(nèi)的鏡像來加速安裝。
pipenv install ipython --dev
當(dāng)加入 --dev
參數(shù)時肌索,安裝的依賴就會放到 [dev-packages]
下面蕉拢,在生產(chǎn)環(huán)境可以不安裝這個里面的包,減小體積诚亚。
鎖定版本
當(dāng)安裝包所有的依賴包后晕换,執(zhí)行 pipenv lock
會在 Pipfile.lock
里面記錄當(dāng)前所有依賴的版本,當(dāng)部署到新機器上的時候執(zhí)行 pipenv sync
就可以安裝對應(yīng)的依賴了站宗,非常方便闸准。
在新機器上同步環(huán)境
# 安裝環(huán)境,生產(chǎn)環(huán)境用這條
pipenv sync
# 同時安裝開發(fā)依賴
pipenv sync -d
查看依賴關(guān)系
$ pipenv graph
autopep8==1.3.5
- pycodestyle [required: >=2.3, installed: 2.3.1]
flake8==3.5.0
- mccabe [required: >=0.6.0,<0.7.0, installed: 0.6.1]
- pycodestyle [required: >=2.0.0,<2.4.0, installed: 2.3.1]
- pyflakes [required: >=1.5.0,<1.7.0, installed: 1.6.0]
ipython==6.4.0
- appnope [required: Any, installed: 0.1.0]
- backcall [required: Any, installed: 0.1.0]
- decorator [required: Any, installed: 4.3.0]
- jedi [required: >=0.10, installed: 0.12.1]
- parso [required: >=0.3.0, installed: 0.3.0]
- pexpect [required: Any, installed: 4.6.0]
- ptyprocess [required: >=0.5, installed: 0.6.0]
- pickleshare [required: Any, installed: 0.7.4]
- prompt-toolkit [required: >=1.0.15,<2.0.0, installed: 1.0.15]
- six [required: >=1.9.0, installed: 1.11.0]
- wcwidth [required: Any, installed: 0.1.7]
- pygments [required: Any, installed: 2.2.0]
- setuptools [required: >=18.5, installed: 39.2.0]
- simplegeneric [required: >0.8, installed: 0.8.1]
- traitlets [required: >=4.2, installed: 4.3.2]
- decorator [required: Any, installed: 4.3.0]
- ipython-genutils [required: Any, installed: 0.2.0]
- six [required: Any, installed: 1.11.0]
Pillow==5.2.0
Scrapy==1.5.0
- cssselect [required: >=0.9, installed: 1.0.3]
- lxml [required: Any, installed: 4.2.3]
- parsel [required: >=1.1, installed: 1.4.0]
- cssselect [required: >=0.9, installed: 1.0.3]
- lxml [required: >=2.3, installed: 4.2.3]
- six [required: >=1.5.2, installed: 1.11.0]
- w3lib [required: >=1.8.0, installed: 1.19.0]
- six [required: >=1.4.1, installed: 1.11.0]
- PyDispatcher [required: >=2.0.5, installed: 2.0.5]
- pyOpenSSL [required: Any, installed: 18.0.0]
- cryptography [required: >=2.2.1, installed: 2.2.2]
- asn1crypto [required: >=0.21.0, installed: 0.24.0]
- cffi [required: >=1.7, installed: 1.11.5]
- pycparser [required: Any, installed: 2.18]
- idna [required: >=2.1, installed: 2.7]
- six [required: >=1.4.1, installed: 1.11.0]
- six [required: >=1.5.2, installed: 1.11.0]
- queuelib [required: Any, installed: 1.5.0]
- service-identity [required: Any, installed: 17.0.0]
- attrs [required: Any, installed: 18.1.0]
- pyasn1 [required: Any, installed: 0.4.3]
- pyasn1-modules [required: Any, installed: 0.2.2]
- pyasn1 [required: >=0.4.1,<0.5.0, installed: 0.4.3]
- pyopenssl [required: >=0.12, installed: 18.0.0]
- cryptography [required: >=2.2.1, installed: 2.2.2]
- asn1crypto [required: >=0.21.0, installed: 0.24.0]
- cffi [required: >=1.7, installed: 1.11.5]
- pycparser [required: Any, installed: 2.18]
- idna [required: >=2.1, installed: 2.7]
- six [required: >=1.4.1, installed: 1.11.0]
- six [required: >=1.5.2, installed: 1.11.0]
- six [required: >=1.5.2, installed: 1.11.0]
- Twisted [required: >=13.1.0, installed: 18.4.0]
- Automat [required: >=0.3.0, installed: 0.7.0]
- attrs [required: >=16.1.0, installed: 18.1.0]
- six [required: Any, installed: 1.11.0]
- constantly [required: >=15.1, installed: 15.1.0]
- hyperlink [required: >=17.1.1, installed: 18.0.0]
- idna [required: >=2.5, installed: 2.7]
- incremental [required: >=16.10.1, installed: 17.5.0]
- zope.interface [required: >=4.4.2, installed: 4.5.0]
- setuptools [required: Any, installed: 39.2.0]
- w3lib [required: >=1.17.0, installed: 1.19.0]
- six [required: >=1.4.1, installed: 1.11.0]
執(zhí)行 pipenv graph
命令會像上面那樣顯示出各個包的依賴關(guān)系梢灭。
配合 direnv 使用
direnv 工具可以在進入目錄時自動切換當(dāng)前的環(huán)境夷家,不需要手動執(zhí)行 pipenv shell
。我用的是mac敏释,使用 brew install direnv
命令安裝库快,其它的系統(tǒng)參考 https://github.com/direnv/direnv。
在 shell 啟動時載入 direnv
我用的是 fish 钥顽,按照官方的說明 https://github.com/direnv/direnv#fish 义屏,只要在 fish 的啟動配置 (一般是在 ~/.config/fish/config.fish
,我用了 oh-my-fish
蜂大,所以在 ~/.config/omf/init.fish
) 里面加入 eval (direnv hook fish)
這條命令就可以了闽铐。
在項目目錄里面啟用
現(xiàn)在已經(jīng)安裝好 direnv 了,到項目的目錄里面添加一個 .envrc
文件奶浦,內(nèi)容是 layout_pipenv
兄墅,保存后再執(zhí)行 direnv allow
。這樣就啟動了當(dāng)前目錄的自動切換環(huán)境功能财喳,只要進入項目目錄就會自動執(zhí)行 pipenv shell
察迟。
PS: 我發(fā)現(xiàn)在 vscode 里面斩狱,安裝了 python 插件后也能自動識別 .envrc
文件,打開項目后自動加載對應(yīng)的環(huán)境扎瓶。