Pipenv 快速上手

作過 Python Web 項目的夥伴可能都有體會,每次新建一個項目,本身得手動建一個虛擬環境,把包裝好了以後,還得本身把裝的包導入到文件中,方便部署。時間久了以後,感受重複勞動太多,應該改變一下了,這不就找到 Pipenv 了。python

Pipenv 是 Kenneth Reitz 開發的又一個 for Humans 項目,於 2017 年 1 月份建立,僅僅用了一年左右的時間便成了官方推薦工具。Kenneth Reitz,做爲一個 Python 開發者應該都知道吧,不知道能夠去面壁了,哈哈。就算你不知道他,你也必定用過他的一個庫 requests/requests,它寫的庫基本上都有 for Humans 標籤,用起來,也確實像標籤說的那樣,簡單好用。git

Pipenv 我的以爲主要解決了下面幾個麻煩事:github

(1)整合了 pip 和 virtualenv,如今沒必要將這兩個工具分開使用了shell

(2)不用本身新建 virtualenv 了flask

(3)不用本身導出包依賴到 requirements.txt 了後端

1 安裝

若是是 Mac 的話,直接bash

brew install pipenv
複製代碼

就搞定了。若是沒有 Python 它會自動給你安裝好。app

其它平臺的話,首先確保你有安裝 Python,才能安裝 Pipenv。若是沒裝的話,能夠本身去百度/谷歌/官網尋找安裝方法。curl

若是你以前裝過 pip,可使用以下命令將 Pipenv 裝到你的用戶目錄工具

pip install --user pipenv
複製代碼

若是以前沒裝過 pip 的話,可使用下面的命令,簡單粗暴

curl https://raw.githubusercontent.com/kennethreitz/pipenv/master/get-pipenv.py | python
複製代碼

而後看下是否安裝成功,終端執行

➜  ~ pipenv --version
pipenv, version 2018.7.1
複製代碼

看來已經 OK,若是你的提示命令不存在之類的問題,說明沒有安裝成功,能夠去看看官網的安裝文檔。

好了,下面咱們來講說 Pipenv 的簡單使用。便於說明,咱們打算新建一個基於 Flask 的 hello_world 應用並運行起來。

2 開始

Pipenv 的虛擬環境是基於項目的,一個項目便會建立一個虛擬環境,不過沒問題,咱們以前開發也是這樣的啊。

建立項目文件夾

➜  ~ mkdir hello_world
複製代碼

好,咱們如今準備建立一個 Python 3.6.0 的虛擬環境

➜  ~ cd hello_world
➜  hello_world pipenv --python 3.6.0
複製代碼

等待一小會兒就安裝成功了。

看下虛擬環境在什麼目錄

➜  hello_world pipenv --venv
/Users/kevinbai/.local/share/virtualenvs/hello_world-qymNYcuh
複製代碼

看下當前目錄多了什麼文件

➜  hello_world ls
Pipfile
複製代碼

看下它的內容

[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]

[dev-packages]

[requires]
python_version = "3.6"
複製代碼

其中,[[source]] 小節記錄安裝源信息,[packages] 記錄依賴包信息,[dev-packages] 記錄開發依賴包信息,[requires] 記錄依賴的環境信息,這裏要求 Python 版本必須等於 3.6。

好了,咱們安裝一個包試試

➜  hello_world pipenv install requests
複製代碼

執行成功後,Pipfile 會在 [packages] 小節下會添加一行 requests 信息

[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]
requests = "*"

[dev-packages]

[requires]
python_version = "3.6"
複製代碼

同時生成一個 Pipfile.lock 文件,該文件記錄了安裝包的具體版本。線上部署時執行命令

➜  hello pipenv install
複製代碼

會自動給你生成相應的虛擬環境並安裝 Pipfile.lock 中的包,線上環境和開發環境的包的版本能夠達到徹底一致。

若是沒有 Pipfile.lock,會根據 Pipfile 生成虛擬環境(若是沒有的話)並安裝相應的包。

若是兩個文件都沒有的話,會生成一個默認的虛擬環境與兩個帶默認信息的 Pipfile 和 Pipfile.lock 文件。

這裏咱們刪除 requests 包

➜  hello_world pipenv uninstall requests
複製代碼

安裝 Flask

➜  hello_world pipenv install flask
複製代碼

安裝好後,咱們能夠看下包的依賴信息

➜  hello_world pipenv graph
certifi==2018.4.16
chardet==3.0.4
Flask==1.0.2
  - click [required: >=5.1, installed: 6.7]
  - itsdangerous [required: >=0.24, installed: 0.24]
  - Jinja2 [required: >=2.10, installed: 2.10]
    - MarkupSafe [required: >=0.23, installed: 1.0]
  - Werkzeug [required: >=0.14, installed: 0.14.1]
idna==2.7
urllib3==1.23
複製代碼

3 編寫代碼並執行應用

新建文件 hello_world/main.py

# coding=utf-8

from flask import Flask

app = Flask(__name__)


@app.route('/', methods=['GET'])
def index():
    return 'Hello, World!'


if __name__ == '__main__':
    app.run()
複製代碼

終端激活虛擬環境

➜  hello_world pipenv shell
複製代碼

而後運行應用

(hello_world-qymNYcuh) ➜  hello_world python main.py
 * Serving Flask app "main" (lazy loading)
 * Environment: production
   WARNING: Do not use the development server in a production environment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
複製代碼

訪問 http://127.0.0.1:5000,看到 Hello, World! 信息,搞定。

終端若是要退出虛擬環境,執行以下命令便可。

(hello_world-qymNYcuh) ➜  hello_world exit
複製代碼

4 其它經常使用命令或選項

  • pipenv run 執行當前項目對應的虛擬環境中的已安裝的腳本

若是須要調用虛擬環境中的 pip 命令,除了使用 pipenv shell 進入環境執行之外,也可使用 pipenv run。好比

➜  hello_world pipenv run pip freeze
certifi==2018.4.16
chardet==3.0.4
click==6.7
Flask==1.0.2
idna==2.7
itsdangerous==0.24
Jinja2==2.10
MarkupSafe==1.0
urllib3==1.23
Werkzeug==0.14.1
複製代碼
  • --where 當前項目路徑
➜  hello_world pipenv --where
/Users/kevinbai/hello_world
複製代碼
  • --py 當前項目的虛擬環境的解釋器所在路徑
➜  hello_world pipenv --py
/Users/kevinbai/.local/share/virtualenvs/hello_world-qymNYcuh/bin/python
複製代碼
  • --rm 刪除當前項目的虛擬環境目錄
➜  hello_world pipenv --rm
Removing virtualenv (/Users/kevinbai/.local/share/virtualenvs/hello_world-qymNYcuh)...
複製代碼
  • -h 幫助,查看可用的命令以及選項介紹

本文首發於公衆號「小小後端」。

相關文章
相關標籤/搜索