面向人类的Python开发工作流程。
项目描述
Pipenv: 面向人类的Python开发工作流程
这是Thoth对Pipenv的分支,使用时请自行承担风险。有关创建此分支的更多信息,请参阅 此问题。
Pipenv 是一个旨在将所有打包世界的最佳实践(bundler、composer、npm、cargo、yarn 等)带到 Python 世界中的工具。在我们这个领域中,Windows 是一等公民。
它自动为您的项目创建和管理虚拟环境,并在您安装/卸载软件包时自动从您的 Pipfile
中添加/删除软件包。它还生成至关重要的 Pipfile.lock
文件,该文件用于生成确定性的构建。
Pipenv 力求解决的问题是多方面的
- 您不再需要分别使用
pip
和virtualenv
。它们协同工作。 - 管理
requirements.txt
文件 可能会出现问题,因此 Pipenv 使用即将推出的Pipfile
和Pipfile.lock
,这在基本用例中更为优越。 - 总是使用散列,用于安全。自动暴露安全漏洞。
- 让您了解您的依赖图(例如,
$ pipenv graph
)。 - 通过加载
.env
文件来简化开发工作流程。
您可以直接在浏览器中快速尝试 Pipenv
安装
如果您使用 MacOS,您可以使用 Homebrew 轻松安装 Pipenv
$ brew install pipenv
或者,如果您使用 Debian Buster+
$ sudo apt install pipenv
或者,如果您使用 Fedora
$ sudo dnf install pipenv
或者,如果您使用 FreeBSD
# pkg install py36-pipenv
否则,请参阅 文档 获取说明。
✨🍰✨
☤ 用户评价
David Gang---
: 这个包管理器真的很棒。我第一次知道我安装了哪些依赖项,以及它们的传递依赖项。结合安装的确定性,使这个包管理器成为一流的,就像 cargo 一样。
Justin Myles Holmes---
: Pipenv 终于是一个旨在激发思维而不是仅仅文件系统的抽象。
☤ 功能
- 启用真正的 确定性构建,同时可以轻松地指定 您需要的内容。
- 生成并检查锁定依赖项的文件散列。
- 如果可用,自动安装所需的 Python。
- 递归地通过查找
Pipfile
来自动找到您的项目主目录。 - 如果不存在,则自动生成
Pipfile
。 - 在标准位置自动创建虚拟环境。
- 在安装/卸载软件包时自动将软件包添加/删除到
Pipfile
中。 - 如果存在,则自动加载
.env
文件。
主要命令是 install
、uninstall
和 lock
,后者生成 Pipfile.lock
。这些旨在取代 $ pip install
使用以及手动虚拟环境管理(要激活虚拟环境,请运行 $ pipenv shell
)。
基本概念
- 如果不存在,将自动创建虚拟环境。
- 如果没有传递参数给
install
,将安装所有指定的[packages]
软件包。 - 要初始化 Python 3 虚拟环境,请运行
$ pipenv --three
。 - 要初始化 Python 2 虚拟环境,请运行
$ pipenv --two
。 - 否则,虚拟环境默认值将是默认值。
其他命令
shell
将启动一个带有激活虚拟环境的 shell。run
将在虚拟环境中运行给定的命令,并将任何参数传递(例如,$ pipenv run python
)。check
断言当前环境满足 PEP 508 要求。graph
将打印出所有已安装依赖项的精美图。
Shell 完成功能
例如,使用 fish,将以下内容放入您的 ~/.config/fish/completions/pipenv.fish
eval (pipenv --completion)
或者,使用 bash,将以下内容放入您的 .bashrc
或 .bash_profile
eval "$(pipenv --completion)"
现在已启用魔法 shell 完成功能!还有一个 fish 插件,它将自动为您激活子 shell!
Fish 是最好的 shell。您应该使用它。
☤ 用法
$ 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.
--completion Output completion (to be eval'd).
--man Display manpage.
--three / --two Use Python 3/2 when creating virtualenv.
--python TEXT Specify which version of Python virtualenv should use.
--site-packages Enable site-packages for the virtualenv.
--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.
定位项目
$ pipenv --where
/Users/kennethreitz/Library/Mobile Documents/com~apple~CloudDocs/repos/kr/pipenv/test
定位虚拟环境
$ pipenv --venv
/Users/kennethreitz/.local/share/virtualenvs/test-Skyy4vre
定位Python解释器
$ pipenv --py
/Users/kennethreitz/.local/share/virtualenvs/test-Skyy4vre/bin/python
安装包
$ pipenv install
Creating a virtualenv for this project...
...
No package provided, installing all dependencies.
Virtualenv location: /Users/kennethreitz/.local/share/virtualenvs/test-EJkjoYts
Installing dependencies from Pipfile.lock...
...
To activate this project's virtualenv, run the following:
$ pipenv shell
从git安装
您可以使用pipenv从git和其他版本控制系统安装包,使用以下格式的URL
<vcs_type>+<scheme>://<location>/<user_or_organization>/<repository>@<branch_or_tag>#<package_name>
唯一可选的部分是 @<branch_or_tag>
部分。当使用SSH访问git时,您可以使用简写vcs和scheme别名 git+git@<location>:<user_or_organization>/<repository>@<branch_or_tag>#<package_name>
。注意,在解析时这被转换为 git+ssh://git@<location>
。
<vcs_type>
的有效值包括 git
、bzr
、svn
和 hg
。有效值 <scheme>
包括 http
、https
、ssh
和 file
。在特定情况下,您还可以访问其他scheme:svn
可以与 svn
作为scheme结合,而 bzr
可以与 sftp
和 lp
结合。
请注意,强烈建议您以可编辑模式安装任何版本控制的依赖项,使用 pipenv install -e
,以确保每次执行依赖项解析时都可以使用存储库的最新副本,并且它包括所有已知的依赖项。
以下是一个示例用法,它将位于 https://github.com/requests/requests.git
的git存储库,以标签 v2.19.1
作为包名 requests
安装
$ pipenv install -e git+https://github.com/requests/requests.git@v2.19#egg=requests
Creating a Pipfile for this project...
Installing -e git+https://github.com/requests/requests.git@v2.19.1#egg=requests...
[...snipped...]
Adding -e git+https://github.com/requests/requests.git@v2.19.1#egg=requests to Pipfile's [packages]...
[...]
您可以在这里了解更多关于pip的vcs支持实现。
安装开发依赖项
$ pipenv install pytest --dev
Installing pytest...
...
Adding pytest to Pipfile's [dev-packages]...
显示依赖图
$ pipenv graph
requests==2.18.4
- certifi [required: >=2017.4.17, installed: 2017.7.27.1]
- chardet [required: >=3.0.2,<3.1.0, installed: 3.0.4]
- idna [required: >=2.5,<2.7, installed: 2.6]
- urllib3 [required: <1.23,>=1.21.1, installed: 1.22]
生成锁定文件
$ pipenv lock
Assuring all dependencies from Pipfile are installed...
Locking [dev-packages] dependencies...
Locking [packages] dependencies...
Note: your project now has only default [packages] installed.
To install [dev-packages], run: $ pipenv install --dev
安装所有开发依赖项
$ pipenv install --dev
Pipfile found at /Users/kennethreitz/repos/kr/pip2/test/Pipfile. Considering this to be the project home.
Pipfile.lock out of date, updating...
Assuring all dependencies from Pipfile are installed...
Locking [dev-packages] dependencies...
Locking [packages] dependencies...
卸载所有内容
$ pipenv uninstall --all
No package provided, un-installing all dependencies.
Found 25 installed package(s), purging...
...
Environment now purged and fresh!
使用shell
$ pipenv shell
Loading .env environment variables…
Launching subshell in virtual environment. Type 'exit' or 'Ctrl+D' to return.
$ ▯
☤ 文档
文档位于pipenv.org。