缘起

直接看 Pipenv 官方网站上的原文吧。

Pipenv — the officially recommended Python packaging tool from Python.org, free (as in freedom).
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.

安装

  • pyenv global 3.6.4

  • pip install pipenv

    Successfully installed certifi-2018.1.18 chardet-3.0.4 idna-2.6 pew-1.1.2 pipenv-10.1.0 requests-2.18.4 urllib3-1.22 virtualenv-15.1.0 virtualenv-clone-0.2.6

    从输出上可见,pipenv 还是使用了 virtualenv 的一些包,从而可以猜测之前 virtualenv 的一些用法可能也适用于 pipenv 的虚拟环境中。

配置

  • vi .profile

    1
    2
    3
    4
    # Alex added: pipenv.
    export PIPENV_VENV_IN_PROJECT=True
    # Shell Completion
    eval "$(pipenv --completion)"

    这样就会在当前使用的目录下创建 .venv 目录作为虚拟环境,而不是将虚拟环境创建在 ~/.virtualenvs 目录下。
    参考:Custom Virtual Environment Location

  • 使用 pipenv install 安装时使用代理。

    需要配置相关的环境变量。

    1
    2
    3
    4
    5
    mkdir 20180226_jupyter
    export HTTP_PROXY=http://127.0.0.1:8118
    export HTTPS_PROXY=http://127.0.0.1:8118
    pipenv install jupyter
    pipenv shell

    参考:python - pip install using proxy in a virtual environment - Stack Overflow

Example Pipenv workflow

  • Install from Pipfile:

    $ pipenv install

  • Add a module:

    $ pipenv install <module>

  • Create a Pipfile.lock from the installed versions:

    $ pipenv lock

  • Install from that Pipfile.lock:

    $ pipenv install --ignore-pipfile

使用

  • mkdir 20180224_pipenv

  • pipenv install requests

    默认安装的就是 Python 3 的虚拟环境。

    1
    2
    3
    4
    5
    6
    7
    Creating a virtualenv for this project…
    ⠋Using base prefix '/usr'
    New python executable in /home/alex/.virtualenvs/20180224_pipenv-hDmWU_44/bin/python3
    Also creating executable in /home/alex/.virtualenvs/20180224_pipenv-hDmWU_44/bin/python
    Installing setuptools, pip, wheel...done.

    Virtualenv location: /home/alex/.virtualenvs/20180224_pipenv-hDmWU_44
  • pipenv shell

    Use ‘exit’ to leave.

    • pipenv --where

    /mnt/c586cf79-dfc3-4125-9bb4-63883aa3e28c/20180224_pipenv

  • pipenv --venv

    /home/alex/.virtualenvs/20180224_pipenv-hDmWU_44

  • pipenv --py

    /home/alex/.virtualenvs/20180224_pipenv-hDmWU_44/bin/python

  • pipenv --rm

    Removing virtualenv (/home/alex/.virtualenvs/20180224_pipenv-hDmWU_44)…

  • 更新 Pipenv 和 pip 到最新版本。

    pipenv --update

    注意:在 11.9.0 版本中,已经删除了 pipenv –update 命令

    可以直接使用 $ pip install -U pipenv 来升级 pipenv。
    可以直接使用 $ pip install -U pip 来升级 pip。

  • Example Pipenv Upgrade Workflow

    • Find out what’s changed upstream: $ pipenv update --outdated.

      $ pipenv update --outdated

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      Package 'absl-py' out–of–date: '==0.1.11' installed, '==0.2.0' available.
      Package 'astroid' out–of–date: '==1.6.1' installed, '==1.6.3' available.
      Package 'certifi' out–of–date: '==2018.1.18' installed, '==2018.4.16' available.
      Package 'decorator' out–of–date: '==4.2.1' installed, '==4.3.0' available.
      Package 'grpcio' out–of–date: '==1.10.0' installed, '==1.11.0' available.
      Package 'ipython' out–of–date: '==6.2.1' installed, '==6.3.1' available.
      Package 'ipywidgets' out–of–date: '==7.1.2' installed, '==7.2.1' available.
      Package 'jedi' out–of–date: '==0.11.1' installed, '==0.12.0' available.
      Package 'jupyter-client' out–of–date: '==5.2.2' installed, '==5.2.3' available.
      Package 'lxml' out–of–date: '==4.1.1' installed, '==4.2.1' available.
      Package 'msgpack-python' out–of–date: '==0.5.5' installed, '==0.5.6' available.
      Package 'notebook' out–of–date: '==5.4.0' installed, '==5.4.1' available.
      Package 'numpy' out–of–date: '==1.14.1' installed, '==1.14.2' available.
      Package 'parso' out–of–date: '==0.1.1' installed, '==0.2.0' available.
      Package 'pexpect' out–of–date: '==4.4.0' installed, '==4.5.0' available.
      Package 'protobuf' out–of–date: '==3.5.2' installed, '==3.5.2.post1' available.
      Package 'pylint' out–of–date: '==1.8.2' installed, '==1.8.4' available.
      Package 'pyppeteer' out–of–date: '==0.0.12' installed, '==0.0.17' available.
      Package 'python-dateutil' out–of–date: '==2.6.1' installed, '==2.7.2' available.
      Package 'requests-html' out–of–date: '==0.8.0' installed, '==0.9.0' available.
      Package 'tensorboard' out–of–date: '==1.6.0' installed, '==1.7.0' available.
      Package 'tensorflow' out–of–date: '==1.6.0' installed, '==1.7.0' available.
      Package 'tornado' out–of–date: '==5.0' installed, '==5.0.2' available.
      Package 'widgetsnbextension' out–of–date: '==3.1.4' installed, '==3.2.1' available.
      Package 'you-get' out–of–date: '==0.4.1040' installed, '==0.4.1060' available.
  • Upgrade packages, two options:

    • Want to upgrade everything? Just do $ pipenv update.

    • Want to upgrade packages one-at-a-time? $ pipenv update <pkg> for each outdated package.

      pipenv update absl-py
      pipenv update protobuf

其他

  • 为什么 Pipenv 的作者在文档的开头说:Windows is a first–class citizen, in our world.

    答案在这里

    Kenneth Reitz: latest release works perfectly on windows, and we put exhaustive work into supporting it. what’s funny about that?

    作者真的为 Windows 版本的 Pipenv 做了很多额外的工作。

  • To initialize a Python 3 virtual environment, run $ pipenv --three.

  • Custom Virtual Environment Location

    Pipenv’s underlying pew dependency will automatically honor the WORKON_HOME environment variable, if you have it set — so you can tell pipenv to store your virtual environments wherever you want, e.g.:

    export WORKON_HOME=~/.venvs

    In addition, you can also have Pipenv stick the virtualenv in project/.venv by setting the PIPENV_VENV_IN_PROJECT environment variable.

  • Automatic Python Installation

    If you have pyenv installed and configured, Pipenv will automatically ask you if you want to install a required version of Python if you don’t already have it available.

问题

参考文献

  • Pipenv: Python Dev Workflow for Humans

  • pypa/pipenv: Python Development Workflow for Humans.

  • pipenv/HISTORY.txt at master · pypa/pipenv

    Pipenv 的更新历史。

  • How to use pipenv in your python project - jcutrer.com

    文中的一些命令已经过时了,例如,pipenv update。
    但整篇文章提供的信息仍然是很全面的。

    • Executing code without activating your virtual environment:

      pipenv run python app.py

    • Development only Packages

      pipenv install --dev pytest

      Your project will likely use some development only packages for testing such as nose, pytest, tox, etc. The -dev flag will place these packages in a seperate [dev-packages] section of your pipfile.

  • How to manage your Python projects with Pipenv

    • It brings together Pip, Pipfile and Virtualenv to provide a straightforward and powerful command line tool.

    • If you add the –two or –three flags to that last command above, it will initialise your project to use Python 2 or 3, respectively. Otherwise the default version of Python will be used.

    • pipenv run which python

  • Stop everything! Start using Pipenv! & bryson3Gps

    这篇把来龙去脉讲得太详细了,推荐!

    • The –system flag tells Pipenv to install to the system Python. The –deploy flag will abort the installation if the Pipfile.lock is out of date or the Python version is wrong. Out of date? Pipenv knows if your Pipfile.lock matches up with the Pipfile by comparing a SHA256 hash of the Pipfile that it has saved. If there’s a mismatch then there is an issue and it aborts.