python 工具鏈 虛擬環境和包管理工具 pipenv

Pipenv is a tool that aims to bring the best of all packaging worlds (bundler, composer, npm, cargo, yarn, etc.) to the Python world. Windows is a first-class citizen, in our world.python

pipenv 是Kenneth Reitz大神的做品,可以有效管理Python多個環境,各類包。過去咱們通常用virtualenv搭建虛擬環境,管理python版本,可是跨平臺的使用不太一致,且有時候處理包之間的依賴總存在問題;過去也經常用 pip進行包的管理,pip已經足夠好,可是仍然推薦pipenv,至關於virtualenv和pip的合體,且更增強大。shell

pipenv主要有如下特性

  • pipenv集成了pip,virtualenv二者的功能,且完善了二者的一些缺陷。
  • 過去用virtualenv管理requirements.txt文件可能會有問題,Pipenv使用Pipfile和Pipfile.lock,後者存放將包的依賴關係,查看依賴關係是十分方便。
  • 各個地方使用了哈希校驗,不管安裝仍是卸載包都十分安全,且會自動公開安全漏洞。。
  • 經過加載.env文件簡化開發工做流程。
  • 支持Python2 和 Python3,在各個平臺的命令都是同樣的。

安裝

pip install pipenv

Usages

$ pipenv
Usage: pipenv [OPTIONS] COMMAND [ARGS]...

Options:
  # 輸出項目目錄
  --where          Output project home information.
  # 顯示虛擬環境目錄
  --venv           Output virtualenv information.
  # python可執行命令路徑
  --py             Output Python interpreter information.
  # pipenv環境變量
  --envs           Output Environment Variable options.
  # 刪除
  --rm             Remove the virtualenv.
  --bare           Minimal output.
  --completion     Output completion (to be eval'd).
  --man            Display manpage.
  # 建立虛擬環境
  --three / --two  Use Python 3/2 when creating virtualenv.
  --python TEXT    Specify which version of Python virtualenv should use.
  
  --site-packages  Enable site-packages for the virtualenv.
  --version        Show the version and exit.
  -h, --help       Show this message and exit.


Usage Examples:
   Create a new project using Python 3.7, specifically:
   # 用 python3.7 建立一個project
   $ pipenv --python 3.7

	 # 刪除 project
   Remove project virtualenv (inferred from current directory):
   $ pipenv --rm

   # 根據 Pipfile 初始化環境(包括 dev 環境)
   Install all dependencies for a project (including dev):
   $ pipenv install --dev

	 # 
   Create a lockfile containing pre-releases:
   $ pipenv lock --pre

	 # 查看已安裝的包的依賴關係
   Show a graph of your installed dependencies:
   $ pipenv graph

   # 檢查已安裝的依賴項是否存在安全漏洞
   Check your installed dependencies for security vulnerabilities:
   $ pipenv check

	 # 使用本地setup.py在虛擬環境/Pipfile安裝一個軟件
   Install a local setup.py into your virtual environment/Pipfile:
   $ pipenv install -e .

   # 使用低級別的pip命令
   Use a lower-level pip command:
   $ pipenv run pip freeze

Commands:
  check      Checks for security vulnerabilities and against PEP 508 markers
             provided in Pipfile.
  clean      Uninstalls all packages not specified in Pipfile.lock.
  graph      Displays currently–installed dependency graph information.
  install    Installs provided packages and adds them to Pipfile, or (if no
             packages are given), installs all packages from Pipfile.
  lock       Generates Pipfile.lock.
  open       View a given module in your editor.
  run        Spawns a command installed into the virtualenv.
  shell      Spawns a shell within the virtualenv.
  sync       Installs all packages specified in Pipfile.lock.
  uninstall  Un-installs a provided package and removes it from Pipfile.

Locate the project:npm

$ pipenv --where
/Users/kennethreitz/Library/Mobile Documents/com~apple~CloudDocs/repos/kr/pipenv/test

Locate the virtualenv:json

$ pipenv --venv
/Users/kennethreitz/.local/share/virtualenvs/test-Skyy4vre

Locate the Python interpreter:windows

$ pipenv --py
/Users/kennethreitz/.local/share/virtualenvs/test-Skyy4vre/bin/python

Install packages:安全

$ pipenv install
Creating a virtualenv for this project...
...
No package provided, installing all dependencies.
Virtualenv location: /Users/kennethreitz/.local/share/virtualenvs/test-EJkjoYts
Installing dependencies from Pipfile.lock...
...

To activate this project's virtualenv, run the following:
$ pipenv shell

加速

在Pipfile中增長bash

[[source]]
name = "pypi"
url = "https://pypi.tuna.tsinghua.edu.cn/simple/"
verify_ssl = tru

開發流程

若是在開發環境已經完成開發,如何構建生產環境呢?這時候就要使用Pipfile.lock了,運行如下命令,把當前環境的模塊lock住, 它會更新Pipfile.lock文件,該文件是用於生產環境的,你永遠不該該編輯它。app

$ pipenv lock

而後只須要把代碼和Pipfile和Pipfile.lock放到生產環境,運行下面的代碼,就能夠建立和開發環境同樣的環境,Pipfile.lock裏記錄了全部包和子依賴包的確切版本,所以是肯定構建composer

$ pipenv install

若是要在另外一個開發環境作開發,則將代碼和Pipfile複製過去,運行如下命令:ide

安裝包括 dev 開發中的包

$ pipenv install --dev

執行命令

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

[dev-packages]

[packages]
requests = "*"
jinja2 = "*"
tobe = "*"

[requires]
python_version = "2.7"

[scripts]
start = 'python main.py'
list = 'pip list'

pipenv run start

vscode使用pipenv虛擬環境

  1. 獲取當前虛擬環境的位置

    pipenv --venv
  2. 編輯 user 的setting.json

    {
        // Necessary for pipenv
        "python.pythonPath": "/Users/yangyanan/.local/share/virtualenvs/daily_report-lBui7cTH/bin/python",
        // For unsolved-import
        "python.jediEnabled": true,
        // Limit for Editor windows
        "workbench.editor.limit.value": 5
    }

參考

  1. 視頻教程 https://www.youtube.com/watch?v=Ib1vO2Tbogo&t=12s
相關文章
相關標籤/搜索