管理应用程序的依赖性

软件包安装教程 涵盖了安装和更新 Python 软件包的基本设置。

然而,即使对你自己的个人项目来说,交互式地运行这些命令也会变得很乏味,而当试图为有多个贡献者的项目自动设置开发环境时,事情就变得更加困难了。

本教程指导你使用 Pipenv 来管理应用程序的依赖性。它将向你展示如何安装和使用必要的工具,并对最佳实践提出有力的建议。

请记住,Python 被用于许多不同的目的,而且准确地说,你想如何管理你的依赖关系可能会根据你决定如何发布你的软件而改变。这里提出的指导意见最直接地适用于网络服务的开发和部署(包括网络应用),但也非常适用于管理任何种类项目的开发和测试环境。

For alternatives, see Other Tools for Application Dependency Management.

安装 Pipenv

Pipenv 是一个 Python 项目的依赖管理器。如果你熟悉 Node.js 的 npm 或 Ruby 的 bundler,它与这些工具的精神相似。虽然 pip 单独使用通常就足够了,但 Pipenv 被推荐用于合作项目,因为它是一个更高级别的工具,为常见的使用情况简化了依赖管理。

使用 pip 来安装Pipenv:

python3 -m pip install --user pipenv
py -m pip install --user pipenv

备注

这将做一个 user installation,以防止破坏任何系统范围的软件包。如果 pipenv 在安装后在你的 shell 中不可用,你需要将 user base 的二进制目录加入你的 PATH。参见 安装到用户站点 获取更多信息。

为你的项目安装软件包

Pipenv 在每个项目的基础上管理依赖性。要安装软件包,请进入你的项目目录(或者在本教程中只是一个空目录)并运行:

cd myproject
pipenv install requests

Pipenv 将安装 Requests 库并在你的项目目录中为你创建一个 PipfilePipfile 用于跟踪你的项目需要哪些依赖,以备你需要重新安装它们,例如当你与他人分享你的项目时。你应该得到类似这样的输出(尽管显示的确切路径会有所不同):

Creating a Pipfile for this project...
Creating a virtualenv for this project...
Using base prefix '/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6'
New python executable in ~/.local/share/virtualenvs/tmp-agwWamBd/bin/python3.6
Also creating executable in ~/.local/share/virtualenvs/tmp-agwWamBd/bin/python
Installing setuptools, pip, wheel...done.

Virtualenv location: ~/.local/share/virtualenvs/tmp-agwWamBd
Installing requests...
Collecting requests
  Using cached requests-2.18.4-py2.py3-none-any.whl
Collecting idna<2.7,>=2.5 (from requests)
  Using cached idna-2.6-py2.py3-none-any.whl
Collecting urllib3<1.23,>=1.21.1 (from requests)
  Using cached urllib3-1.22-py2.py3-none-any.whl
Collecting chardet<3.1.0,>=3.0.2 (from requests)
  Using cached chardet-3.0.4-py2.py3-none-any.whl
Collecting certifi>=2017.4.17 (from requests)
  Using cached certifi-2017.7.27.1-py2.py3-none-any.whl
Installing collected packages: idna, urllib3, chardet, certifi, requests
Successfully installed certifi-2017.7.27.1 chardet-3.0.4 idna-2.6 requests-2.18.4 urllib3-1.22

Adding requests to Pipfile's [packages]...

使用已安装的软件包

现在 Requests 已经安装完毕,你可以创建一个简单的 main.py 文件来使用它:

import requests

response = requests.get('https://httpbin.org/ip')

print('Your IP is {0}'.format(response.json()['origin']))

然后你可以使用 pipenv run 来运行这个脚本。

pipenv run python main.py

你应该得到与此类似的输出:

Your IP is 8.8.8.8

使用 pipenv run 可以确保你安装的软件包对你的脚本可用。也可以用 pipenv shell 催生一个新的 shell,确保所有命令都能访问你安装的软件包。

下一步

恭喜你,你现在知道如何在一个合作的 Python 项目中有效地管理依赖性和开发环境了!。✨ 🍰 ✨

如果你对创建和发布你自己的 Python 软件包感兴趣,请参阅 关于打包和发布软件包的教程

注意,当你的应用程序包括 Python 源代码包的定义时,它们(和它们的依赖关系)可以通过 pipenv install -e <relative-path-to-source-directory> 添加到你的 pipenv 环境中(例如 pipenv install -e .pipenv install -e src)。

用于应用程序依赖性管理的其他工具

If you find this particular approach to managing application dependencies isn’t working well for you or your use case, you may want to explore these other tools and techniques, listed in alphabetical order, to see if one of them is a better fit:

  • hatch for opinionated coverage of even more steps in the project management workflow, such as incrementing versions and creating new skeleton projects from project templates.

  • micropipenv for a lightweight wrapper around pip that supports requirements.txt, Pipenv and Poetry lock files, or converting them to pip-tools compatible output. Designed for containerized Python applications, but not limited to them.

  • PDM for a modern Python package management tool supporting PEP 582 (replacing virtual environments with __pypackages__ directory for package installation) and relying on standards such as PEP 517 and PEP 621.

  • pip-tools for creating a lock file of all dependencies from a list of packages directly used in a project, and ensuring that only those dependencies are installed.

  • Poetry for a tool comparable in scope to Pipenv that focuses more directly on use cases where the project being managed is structured as a distributable Python package with a valid pyproject.toml file. By contrast, Pipenv explicitly avoids making the assumption that the application being worked on will support distribution as a pip-installable Python package.