Python虛擬環境管理工具之pipenv

一、pipenv簡介

pipenv是由Kenneth Reitz編寫,用於管理虛擬環境的工具。

二、pipenv的安裝、升級與卸載

下面都是使用pip(某些情況下語法爲pip3)進行操作。

2.1、安裝

windows系統:

>pip install pipenv

Linux系統:

$sudo pip install pipenv

Mac系統:

$ brew install pipenv

實例:

2.2、升級

windows系統:

>pip install --upgrade pipenv

Linux系統:

$ pip install --upgrade pipenv

Mac系統:

$ pip install --upgrade pipenv

實例:

2.3、卸載

windows系統:

> pip uninstall pipenv

Linux系統:

$ pip uninstall pipenv

Mac系統:

$ pip uninstall pipenv

實例:

三、pipenv --help 

基本每一個庫都有一個幫助文檔,使用--help即可查看幫助文檔。pipenv也是如此,我們可以通過幫助命令對pipenv有一個大致的瞭解,或者不記得的某個命令的具體用法的時可以查看幫助文檔。

實例:

D:\myproject>pipenv --help
Usage: pipenv [OPTIONS] COMMAND [ARGS]...

Options:
  --where             Output project home information.
  --venv              Output virtualenv information.
  --py                Output Python interpreter information.
  --envs              Output Environment Variable options.
  --rm                Remove the virtualenv.
  --bare              Minimal output.
  --completion        Output completion (to be eval'd).
  --man               Display manpage.
  --support           Output diagnostic information for use in GitHub issues.
  --site-packages     Enable site-packages for the virtualenv.  [env var:
                      PIPENV_SITE_PACKAGES]
  --python TEXT       Specify which version of Python virtualenv should use.
  --three / --two     Use Python 3/2 when creating virtualenv.
  --clear             Clears caches (pipenv, pip, and pip-tools).  [env var:
                      PIPENV_CLEAR]
  -v, --verbose       Verbose mode.
  --pypi-mirror TEXT  Specify a PyPI mirror.
  --version           Show the version and exit.
  -h, --help          Show this message and exit.


Usage Examples:
   Create a new project using Python 3.7, specifically:
   $ pipenv --python 3.7

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

   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

   Install a local setup.py into your virtual environment/Pipfile:
   $ pipenv install -e .

   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.
  update     Runs lock, then sync.

D:\myproject>

四、pipenv創建虛擬環境

4.1創建虛擬環境

創建虛擬環境的過程兩步即可實現:一、進入到項目目錄;二、運行pipenv install 命令。

windows系統實例(假設項目爲D盤下面的myproject):

D:\>cd myproject

D:\myproject>pipenv install
Creating a virtualenv for this project…
Pipfile: D:\myproject\Pipfile
Using d:\qxh\tool\python36\python3.exe (3.6.5) to create virtualenv…
[    ] Creating virtual environment...Already using interpreter d:\qxh\tool\python36\python3.exe
Using base prefix 'd:\\qxh\\tool\\python36'
New python executable in C:\Users\Administrator\.virtualenvs\myproject-1qCvww8S\Scripts\python3.exe
Also creating executable in C:\Users\Administrator\.virtualenvs\myproject-1qCvww8S\Scripts\python.exe
Installing setuptools, pip, wheel...
done.

Successfully created virtual environment!
Virtualenv location: C:\Users\Administrator\.virtualenvs\myproject-1qCvww8S
Creating a Pipfile for this project…
Pipfile.lock not found, creating…
Locking [dev-packages] dependencies…
Locking [packages] dependencies…
Updated Pipfile.lock (ca72e7)!
Installing dependencies from Pipfile.lock (ca72e7)…
  ================================ 0/0 - 00:00:00
To activate this project's virtualenv, run pipenv shell.
Alternatively, run a command inside the virtualenv with pipenv run.

D:\myproject>

linux系統實例(假設項目爲/var/www/目錄下的myproject):

root@iZwz94kwqu5mk9oxpv2m2tZ:/var/www# cd myproject/
root@iZwz94kwqu5mk9oxpv2m2tZ:/var/www/myproject# pipenv install
Creating a virtualenv for this project...
Pipfile: /var/www/myproject/Pipfile
Using /usr/bin/python (2.7.15+) to create virtualenv...
⠴ Creating virtual environment...Already using interpreter /usr/bin/python
New python executable in /root/.local/share/virtualenvs/myproject-jyD3CuVy/bin/python
Please make sure you remove any previous custom paths from your /root/.pydistutils.cfg file.
Installing setuptools, pip, wheel...
done.
✔ Successfully created virtual environment! 
Virtualenv location: /root/.local/share/virtualenvs/myproject-jyD3CuVy
Creating a Pipfile for this project...
Pipfile.lock not found, creating...
Locking [dev-packages] dependencies...
Locking [packages] dependencies...
Updated Pipfile.lock (dfae9f)!
Installing dependencies from Pipfile.lock (dfae9f)...
  ?   ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 0/0 — 00:00:00
To activate this project's virtualenv, run pipenv shell.
Alternatively, run a command inside the virtualenv with pipenv run.
root@iZwz94kwqu5mk9oxpv2m2tZ:/var/www/myproject# 
 

如上圖所示,已成功創建虛擬環境。

4.2創建虛擬環境過程解析

4.2.1當運行pipenv install 命令時, pipenv 會在項目目錄下尋找Pipfile文件,如果沒有則生成一個Pipfile文件。installuninstall和lock等命令都會生成Pipfile.lock文件。兩個文件互相配合,完成虛擬環境的管理工作。

4.2.2 pipenv install 創建的虛擬環境名字格式爲:項目目錄名+隨機字符串。如上面的:myproject-1qCvww8Smyproject-jyD3CuVy。在windows系統中,默認的路徑爲C:\Users\Administrator\.virtualenvs\下。而在linux環境系統中,默認的路徑爲:~/.local/share/virtualenvs/。如果你想在項目目錄內創建虛擬環境文件夾,可以設置環境變量PIPENV_VENV_IN_PROJECT,這時名爲.venv的虛擬環境文件夾將在項目根目錄下創建。

4.2.3 創建虛擬環境時,如果要指定Python版本,可以使用--two指定python2版本;使用--three指定Python3版本;或是使用 --python 選項指定具體的版本號。

root@iZwz94kwqu5mk9oxpv2m2tZ:/var/www/myproject# pipenv install --three
Virtualenv already exists!
Removing existing virtualenv...
Creating a virtualenv for this project...
Pipfile: /var/www/myproject/Pipfile
Using /usr/bin/python3 (3.6.8) to create virtualenv...
⠹ Creating virtual environment...Already using interpreter /usr/bin/python3
Using base prefix '/usr'
New python executable in /root/.local/share/virtualenvs/myproject-jyD3CuVy/bin/python3
Also creating executable in /root/.local/share/virtualenvs/myproject-jyD3CuVy/bin/python
Please make sure you remove any previous custom paths from your /root/.pydistutils.cfg file.
Installing setuptools, pip, wheel...
done.
Running virtualenv with interpreter /usr/bin/python3
✔ Successfully created virtual environment! 
Virtualenv location: /root/.local/share/virtualenvs/myproject-jyD3CuVy
Creating a Pipfile for this project...
Pipfile.lock not found, creating...
Locking [dev-packages] dependencies...
Locking [packages] dependencies...
Updated Pipfile.lock (ca72e7)!
Installing dependencies from Pipfile.lock (ca72e7)...
  ?   ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 0/0 — 00:00:00
To activate this project's virtualenv, run pipenv shell.
Alternatively, run a command inside the virtualenv with pipenv run.
root@iZwz94kwqu5mk9oxpv2m2tZ:/var/www/myproject# 
 

4.4.4 如果要顯示激活虛擬環境命令,使用:pipenv shell,退出虛擬環境則使用exit。當然,也可以使用pipenv run命令隱式激活虛擬環境。

五、使用pipenv管理虛擬環境

5.1 安裝install

可以使用pipenv install可以安裝第三方庫。示例:

D:\myproject>pipenv install requests
Installing requests…
Adding requests to Pipfile's [packages]…
Installation Succeeded
Installing dependencies from Pipfile.lock (b14837)…
  ================================ 5/5 - 00:00:02
To activate this project's virtualenv, run pipenv shell.
Alternatively, run a command inside the virtualenv with pipenv run.

D:\myproject>

5.2 更新update

如果要更新依賴,則可以使用pipenv update 命令。

5.3 卸載uninstall

使用pipenv uninstall 可以卸載第三方庫。如果要卸載所有的包,使用pipenv uninstall --all。示例:

D:\myproject>pipenv uninstall requests
Uninstalling requests…
Uninstalling requests-2.22.0:
  Successfully uninstalled requests-2.22.0

Removing requests from Pipfile…
Locking [dev-packages] dependencies…
Locking [packages] dependencies…
Updated Pipfile.lock (ca72e7)!

D:\myproject>
 

5.4 lock

使用pipenv lock用於聲明虛擬環境中所有庫及其版本。類似於pip freeze >requirements.txt

5.5 graph

可以使用pipenv graph命令查看當前環境下的依賴情況(類似於在虛擬環境中使用pip list查看依賴)。

六、pipenv常用命令

6.1 查看項目路徑

$ pipenv --where                                                                                                                                                                        D:\helloflask              

6.2 查看項目虛擬環境路徑(例如在pycharm選擇虛擬環境目錄的時候用到)

$ pipenv --venv
C:\Users\95232\.virtualenvs\helloflask-hUtz0ICQ

6.3 查看項目python解釋器

$ pipenv --py
C:\Users\95232\.virtualenvs\helloflask-hUtz0ICQ\Scripts\python.exe

七、參考資料

[1] 劉江的博客,pipenv 新款Python虛擬環境工具詳解:http://www.liujiangblog.com/blog/18/

[2] pipenv 源碼:https://github.com/pypa/pipenv

[3] pipenv docs: https://docs.pipenv.org/en/latest/

[4]Kenneth Reitz github:https://github.com/kennethreitz

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章