Python虚拟环境(4)—pipenv使用

◎知识点

  1. 概述和安装

  2. pipenv使用

  3. 使用pipenv时的项目交接


1.概述

virtualenv和virtualenvwrapper实现了Python第三方库的有效管理,但是仍然存在一些可以改进的地方。比如:requirements.txt的功能较弱,在requirements.txt中仅仅列出了虚拟环境中安装的所有第三方库的名称和版本号, 没有反应出第三库之间的依赖关系。再比如:requirements.txt需要手动生成和更新。要手动的执行 pip3 freeze > requirements.txt 才能得到requirements.txt。如果安装或卸载了第三方库,需要再次执行指令以得到更新的requirements.txt。再比如: pip和virtualenv是两个独立的工具,能和成一个工具是不是更好呢? 第三方库 Pipenv: Python Development Workflow for Humans 就是综上所述的改进工具,pip + virtualenv的结合体,也是Python官方推荐的第三方库管理工具。

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.

It automatically creates and manages a virtualenv for your projects, as well as adds/removes packages from your Pipfile as you install/uninstall packages. It also generates the ever-important Pipfile.lock, which is used to produce deterministic builds.


The problems that Pipenv seeks to solve are multi-faceted:

  • You no longer need to use pip and virtualenv separately. They work together.

  • Managing a requirements.txt file can be problematic, so Pipenv uses the upcoming Pipfile and Pipfile.lock instead, which is superior for basic use cases.

  • Hashes are used everywhere, always. Security. Automatically expose security vulnerabilities.

  • Give you insight into your dependency graph (e.g. $ pipenv graph).

  • Streamline development workflow by loading .env files.

Python虚拟环境(4)—pipenv使用

➜  ~ pip3 install pipenv
➜  ~ ln -s /Users/anzhihe/Library/Python/3.8/bin/pipenv /usr/local/bin/pipenv

➜  ~ pipenv
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.
  --man                           Display manpage.
  --support                       Output diagnostic information for use in
                                  GitHub issues.
  --site-packages / --no-site-packages
                                  Enable site-packages for the virtualenv.
                                  [env var: PIPENV_SITE_PACKAGES]
  --python TEXT                   Specify which version of Python virtualenv
                                  should use.
  --clear                         Clears caches (pipenv, pip).  [env var:
                                  PIPENV_CLEAR]
  -q, --quiet                     Quiet mode.
  -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 PyUp Safety 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.
  requirements  Generate a requirements.txt from Pipfile.lock.
  run           Spawns a command installed into the virtualenv.
  scripts       Lists scripts in current environment config.
  shell         Spawns a shell within the virtualenv.
  sync          Installs all packages specified in Pipfile.lock.
  uninstall     Uninstalls a provided package and removes it from Pipfile.
  update        Runs lock, then sync.
  upgrade       Resolves provided packages and adds them to Pipfile, or (if no
                packages are given), merges results to Pipfile.lock
  verify        Verify the hash in Pipfile.lock is up-to-date.


2.使用Pipenv

2.1 创建虚拟环境

Python虚拟环境(4)—pipenv使用

➜  MyVirEnvs where python
/Library/Frameworks/Python.framework/Versions/2.7/bin/python
/usr/local/bin/python
➜  MyVirEnvs where python3
/usr/bin/python3
➜  MyVirEnvs export WORKON_HOME="~/MyVirEnvs" # pipenv也使用环境变量WORKON_HOME来指定虚拟环境的路径

➜  MyVirEnvs pipenv --python "/Library/Frameworks/Python.framework/Versions/2.7/bin/python"
Creating a virtualenv for this project...
Pipfile: /Users/anzhihe/Pipfile
Using /Library/Frameworks/Python.framework/Versions/2.7/bin/python (2.7.18) to create virtualenv...
⠴ Creating virtual environment...created virtual environment CPython2.7.18.final.0-64 in 316ms
  creator CPython2macOsArmFramework(dest=/Users/anzhihe/MyVirEnvs/anzhihe-HGmV8VVA, clear=False, no_vcs_ignore=False, global=False)
  seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/Users/anzhihe/Library/Application Support/virtualenv)
    added seed packages: pip==20.3.4, setuptools==44.1.1, wheel==0.37.1
  activators BashActivator,CShellActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator

   Successfully created virtual environment!
Virtualenv location: /Users/anzhihe/MyVirEnvs/anzhihe-HGmV8VVA


2.2 激活虚拟环境

Python虚拟环境(4)—pipenv使用

➜  anzhihe-HGmV8VVA pipenv shell
Launching subshell in virtual environment...
 . /Users/anzhihe/MyVirEnvs/anzhihe-HGmV8VVA/bin/activate
➜  ~  . /Users/anzhihe/MyVirEnvs/anzhihe-HGmV8VVA/bin/activate
(anzhihe) ➜  ~ where python
/Users/anzhihe/.local/share/virtualenvs/anzhihe-HGmV8VVA/bin/python
/Library/Frameworks/Python.framework/Versions/2.7/bin/python
/usr/local/bin/python
(anzhihe) ➜  ~ where pip
/Users/anzhihe/.local/share/virtualenvs/anzhihe-HGmV8VVA/bin/pip
/Library/Frameworks/Python.framework/Versions/2.7/bin/pip
/usr/local/bin/pip
(anzhihe) ➜  ~ pip --version
pip 20.3.4 from /Users/anzhihe/.local/share/virtualenvs/anzhihe-HGmV8VVA/lib/python2.7/site-packages/pip (python 2.7)


2.3 在虚拟环境中进行操作

Python虚拟环境(4)—pipenv使用

➜  MyVirEnvs pipenv --python /usr/bin/python3
Creating a virtualenv for this project...
Pipfile: /Users/anzhihe/Pipfile
Using /usr/bin/python3 (3.8.9) to create virtualenv...
⠋ Creating virtual environment...created virtual environment CPython3.8.9.final.0-64 in 741ms
  creator CPython3macOsFramework(dest=/Users/anzhihe/MyVirEnvs/anzhihe-HGmV8VVA, clear=False, no_vcs_ignore=False, global=False)
  seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/Users/anzhihe/Library/Application Support/virtualenv)
    added seed packages: pip==23.0.1, setuptools==67.4.0, wheel==0.38.4
  activators BashActivator,CShellActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator

  Successfully created virtual environment!
Virtualenv location: /Users/anzhihe/MyVirEnvs/anzhihe-HGmV8VVA
Warning: Your Pipfile requires python_version 2.7.18, but you are using 3.8.9 (/Users/b/M/b/bin/python).
  $ pipenv --rm and rebuilding the virtual environment may resolve the issue.
  $ pipenv check will surely fail.
➜  MyVirEnvs ll
total 0
drwxr-xr-x  8 anzhihe  staff   256B  4  9 01:59 anzhihe-HGmV8VVA
➜  MyVirEnvs cd anzhihe-HGmV8VVA
➜  MyVirEnvs pipenv shell

(anzhihe) ➜  MyVirEnvs pipenv install requests
(anzhihe) ➜  ~ cat Pipfile.lock
        "requests": {
            "hashes": [
                "sha256:64299f4909223da747622c030b781c0d7811e359c37124b4bd368fb8c6518baa",
                "sha256:98b1b2782e3c6c4904938b84c0eb932721069dfdb9134313beff7c83c2df24bf"
            ],
            "index": "pip_conf_index_global",
            "version": "==2.28.2"
        },

(anzhihe) ➜  ~ pipenv --py
/Users/anzhihe/MyVirEnvs/anzhihe-HGmV8VVA/bin/python
(anzhihe) ➜  ~ pipenv --where
/Users/anzhihe
(anzhihe) ➜  ~ pipenv --venv
/Users/anzhihe/MyVirEnvs/anzhihe-HGmV8VVA

(anzhihe) ➜  ~ exit
➜  MyVirEnvs pipenv --rm
Removing virtualenv (/Users/anzhihe/MyVirEnvs/anzhihe-HGmV8VVA)...


2.4 反激活虚拟环境

Python虚拟环境(4)—pipenv使用


3.使用Pipenv时的项目交接

Python虚拟环境(4)—pipenv使用

anzhihe 安志合个人博客,版权所有 丨 如未注明,均为原创 丨 转载请注明转自:https://chegva.com/5700.html | ☆★★每天进步一点点,加油!★★☆ | 

您可能还感兴趣的文章!

2 评论

发表评论

电子邮件地址不会被公开。 必填项已用*标注