为人类服务的Python开发工作流工具¶
Pipenv 的目标是把所有包管理工具(bundler, composer, npm, cargo, yarn等)中的最佳实践带到Python的世界。 Windows在这里是第一等公民。
它能为你的项目自动创建、管理虚拟环境,同时,当你安装、卸载包时,它能自动在你的 Pipfile
中添加、删除记录。它还会为你生成一个相当重要的 Pipfile.lock
文件,用来产生一致性的构建。
Pipenv的主要目的是为应用的开发者和使用者能容易地设置好一个工作环境。关于库与应用的区别以及 setup.py
与 Pipfile
在指定依赖上的使用,请参考 ☤ Pipfile vs setup.py 。

Pipenv要解决的问题,是多方面的:
- 你无需再分开使用
pip
和virtualenv
了,他们是一起工作的。 - 管理一个
requirements.txt` `文件 `可能会有很多问题 <https://www.kennethreitz.org/essays/a-better-pip-workflow>`_ ,所以Pipenv使用 ``Pipfile
与Pipfile.lock
来将抽象依赖声明与最近的已验证的依赖版本分离。 - 在各处都尽可能使用哈希值。自动暴露安全缺陷。
- 为了尽量避免 过期组件的安全漏洞 ,强烈鼓励使用依赖的最新版本。
- 让你看到你的依赖图 (
$ pipenv graph
) - 通过
.env
文件让开发工作流水化。
你可以在你的浏览器中快速预览Pipenv
今天就安装Pipenv吧!¶
如果你在用MacOS,你可以使用Homebrew方便地安装Pipenv。你也可以在Linux系统上的Linuxbrew中使用相同的命令
$ brew install pipenv
或者,如果你用的是Fedora 28
$ sudo dnf install pipenv
如果你的系统不在上述列表中,参考 ☤ 安装Pipenv 一章中的安装指引。
✨🍰✨
用户推荐语¶
- David Gang—
- 这个包管理工具真的太好用了。我有生第一次知道了我安装了哪些依赖和哪些过渡依赖。再加上依赖的安装是确定性的,使得这个包管理工具成为人们的优先选择,就像cargo 。
- Justin Myles Holmes—
- 终于出现了Pipenv这样的良好的抽象层,它占据你的大脑,而不仅仅是文件系统。
☤ Pipenv的特性¶
- 支持真正的 一致性构建 ,只需简单的指定 你想要什么。
- 从锁定的依赖中生成文件哈希值用来做校验。
- 在
pyenv
可用时,自动安装需要的Python版本。 - 通过搜索
Pipfile
,自动地递归查找项目根目录。 - 如果
Pipfile
不存在,自动为你生成一个。 - 在标准位置自动创建一个虚拟环境。
- Automatically adds/removes packages to a
Pipfile
when they are installed or uninstalled. - 若探测到
.env
文件存在,自动加载它。
主要的命令有 install
, uninstall
,和用来生成 Pipfile.lock
的 lock
命令。这些命令可以取代 $ pip install
, 以及人工管理虚拟环境(要激活一个虚拟环境,可以运行 $ pipenv shell
)。
基本概念¶
- 如果虚拟环境不存在,会被自动创建。
install
命令后面如果不带参数,则安装[packages]
下面所有的包。- 使用
$ pipenv --three
来指定用Python 3初始化虚拟环境。 - 使用
$ pipenv --two
来指定用Python 2初始化虚拟环境。 - 否则,和
virtualenv
一样,使用默认的Python版本。
其他命令¶
graph
命令可以展示当前安装的包的依赖关系树。shell
命令会启动一个激活了虚拟环境的shell,在此shell中可以使用exist
退出。run
命令可以在虚拟环境中运行一段给定的命令,并转发其他命令参数(比如:$ pipenv run python
或者$ pipenv run pip freeze
)。check
命令可以检查安全漏洞,以及当前环境是否满足PEP 508标签的要求。
进一步文档指南。¶
- Pipenv的基本使用
- Advanced Usage of Pipenv
- ☤ Caveats
- ☤ Specifying Package Indexes
- ☤ Using a PyPI Mirror
- ☤ Injecting credentials into Pipfiles via environment variables
- ☤ Specifying Basically Anything
- ☤ Using pipenv for Deployments
- ☤ Pipenv and Other Python Distributions
- ☤ Generating a
requirements.txt
- ☤ Detection of Security Vulnerabilities
- ☤ Community Integrations
- ☤ Open a Module in Your Editor
- ☤ Automatic Python Installation
- ☤ Automatic Loading of
.env
- ☤ Custom Script Shortcuts
- ☤ Support for Environment Variables
- ☤ Configuration With Environment Variables
- ☤ Custom Virtual Environment Location
- ☤ Testing Projects
- ☤ Shell Completion
- ☤ Working with Platform-Provided Python Components
- ☤ Pipfile vs setup.py
- ☤ Changing Pipenv’s Cache Location
- ☤ Changing Default Python Versions
- Pipenv CLI Reference
- Frequently Encountered Pipenv Problems
- ☤ Your dependencies could not be resolved
- ☤ No module named <module name>
- ☤ My pyenv-installed Python is not found
- ☤ Pipenv does not respect pyenv’s global and local Python versions
- ☤ ValueError: unknown locale: UTF-8
- ☤ /bin/pip: No such file or directory
- ☤
shell
does not show the virtualenv’s name in prompt - ☤ Pipenv does not respect dependencies in setup.py
- ☤ Using
pipenv run
in Supervisor program - ☤ An exception is raised during
Locking dependencies…