pipenv 入門指南

爲了滿足不同的開發需求,我們需要在自己的電腦上安裝多個 Python 版本,並且項目之間進行環境隔離。通常用得比較多的是 pyenv + virtualenv,其中:

  • virtualenv 是 Python 虛擬環境管理工具,通過切換目錄來實現不同包環境間的切換。
  • pyenv 是 python 版本管理工具,通過修改環境變量的方式實現。

但今天我們的要介紹的是 Pipenv 這個工具包。

Pipenv 介紹


pipenv 是 Python 官方推薦的包管理工具,是 requests 庫作者 Kenneth Reitz 編寫的一個用於創建和管理 Python 虛擬環境的工具。它綜合了 virtualenv , pip 和 pyenv 三者的功能。它的目標是將所有包管理領域 (bundler, composer, npm, cargo, yarn, 等) 的最好體驗帶到 python 世界中來。

pipenv 會自動創建並管理一個 virtualenv,在 Pipfile 文件中添加/刪除包就相當於安裝/卸載包,並且還會生成 Pipfile.lock 文件用於產生確定的構建。你可以使用 pipenv 這一個工具來安裝、卸載、跟蹤和記錄依賴性,並創建、使用和組織你的虛擬環境。

Pipenv 試圖解決以下的問題:

  • 你不需要再單獨的使用 pipvirtualenv 。它們可以一起工作。
  • 管理 requirements.txt 文件可能會有問題,所以 Pipenv 使用 PipfilePipfile.lock 來分離抽象的依賴聲明。
  • 使用散列無處不在,總是這樣。安全。自動暴露安全漏洞。
  • 強烈鼓勵使用最新版本的依賴項,來最小化因過時組件所引起的安全風險。
  • 洞察你的依賴圖(例如 $ pipenv graph)。
  • 通過加載 .env 文件流線型的開發工作流。

安裝


pip 默認安裝包路徑爲 /usr/local/lib/python2.7/site-packages。你可以使用命令 python -m site --user-site 來查看 site-packages 的目錄所在。

# Windows:
pip install pipenv

# Linux:
sudo pip install pipenv

# Mac OSX:
$ brew install pipenv

無法用pip管理的包,pipenv同樣無法使用。

pipenv 依賴:psutil, virtualenv-clone, pew, certifi, urllib3, chardet, requests, mccabe, pyflakes, pycodestyle, flake8 等第三方模塊。

使用


# 進入項目目錄
$ cd myproject

# 如果不存在 pipfile,安裝它
$ pipenv install

# 或添加一個包到項目
$ pipenv install <package>

這會創建一個 Pipfile, 如果目錄下不存在,它會隨着你提供的包自動被編輯。

激活 Pipenv shell

$ pipenv shell
$ python --version

更新包

# 更新所有包
$ pipenv update

# 一次更新一個包
$ pipenv update <pkg>

從 requirements.txt 中導入包

$ pipenv install -r path/to/requirements.txt

pipenv 會自動導入 txt 文件中的所有內容,並生成 Pipfile。如果你的 requirements.txt 中固定了版本,並且你想修改它們,可以編輯新的 Pipfile 並移除版本號,讓 pipenv 跟蹤它們。如果你想保留 Pipfile.lock 中固定的版本,你可以運行 pipenv lock --keep-outdated

指定包版本

$ pipenv install "requests~=1.2"   # equivalent to requests~=1.2.0 ,這會自動更新 Pipfile
$ pipenv install "requests>=1.4"   # will install a version equal or larger than 1.4.0
$ pipenv install "requests<=2.13"  # will install a version equal or lower than 2.13.0
$ pipenv install "requests>2.19"   # will install 2.19.1 but not 2.19.0

指定 python 版本

創建一個新的 virtualenv,使用特定版本的 Python 安裝

# 使用 python 3 安裝
$ pipenv --python 3

# 使用 python 3.6 安裝
$ pipenv --python 3.6

# 使用 python  2.7.14 安裝
$ pipenv --python 2.7.14

當給定一個 Python 版本時,Pipenv 會根據 python 版本自動掃描您的系統,如果系統中沒有該版本的 python 會提示你安裝。

Pipenv 管理環境

$ pipenv install [package names]

用戶可以提供以下額外參數:

  • --two — Performs the installation in a virtualenv using the system python2 link.
  • --three — Performs the installation in a virtualenv using the system python3 link.
  • --python — Performs the installation in a virtualenv using the provided Python interpreter.
  • --dev — Install both develop and default packages from Pipfile.
  • --system — Use the system pip command rather than the one from your virtualenv.
  • --ignore-pipfile — Ignore the Pipfile and install from the Pipfile.lock.
  • --skip-lock — Ignore the Pipfile.lock and install from the Pipfile. In addition, do not write out a Pipfile.lock reflecting changes to the Pipfile.

Pipfile.lock 是什麼

Pipfile.lock 非常重要,因爲它做了兩件事情:

  1. 通過保持每個安裝包的 hash 值,提供了良好的安全性。
  2. 固定所有依賴項和子依賴項,給你一個可複用的環境。
{
    "_meta": {
        "hash": {
            "sha256": "627ef89...64f9dd2"
        },
        "pipfile-spec": 6,
        "requires": {
            "python_version": "3.7"
        },
        "sources": [
            {
                "name": "pypi",
                "url": "https://pypi.org/simple",
                "verify_ssl": true
            }
        ]
    },
    "default": {
        "django": {
            "hashes": [
                "sha256:acdcc1...ab5bb3",
                "sha256:efbcad...d16b45"
            ],
            "index": "pypi",
            "version": "==2.1.2"
        },
        "pytz": {
            "hashes": [
                "sha256:a061aa...669053",
                "sha256:ffb9ef...2bf277"
            ],
            "version": "==2018.5"
        }
    },
    "develop": {}
}

注意到每個依賴包的版本都是固定的。沒有特殊情況,你應該將它加入源代碼控制。

參考

  1. https://blog.csdn.net/watermusicyes/article/details/72909752
  2. https://pipenv.readthedocs.io/en/latest/
  3. https://pylixm.cc/posts/2018-01-13-python-pipenv.html
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章