

python 依赖管理 pipenv 使用教程
source link: https://blog.sinlov.cn/posts/2019/09/16/python-%E4%BE%9D%E8%B5%96%E7%AE%A1%E7%90%86-pipenv-%E4%BD%BF%E7%94%A8%E6%95%99%E7%A8%8B/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

PipEnv 安装
$ pip install --user pipenv
# 升级 pipenv
$ pip install --user pipenv --upgrade
# 如果不知道如何将 shell 找不到的命令添加到系统环境变量则这样寻找
$ python -m site --user-base
- macOS 安装
# 可以通过 brew 安装
$ brew info pipenv
$ brew install pipenv
- pipenv 自动补全
添加到 .bashrc 或者 .zshrc 中即可
eval "$(pipenv --completion)"
windows 安装 pipenv
$ pip install --user pipenv
windows 需要设置环境变量到用户目录,方法是添加用户环境变量到 %AppData%\Python\Python39\Scripts
注意,不同版本可能不同,比如 python 3.8 为
%AppData%\Python\Python38\Scripts
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 executed by the
shell).
--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.
--three / --two Use Python 3/2 when creating virtualenv.
--clear Clears caches (pipenv, pip, and pip-tools).
[env var: PIPENV_CLEAR]
-v, --verbose Verbose mode.
--pypi-mirror TEXT Specify a PyPI mirror.
--version Show the version and exit.
-h, --help Show this message and exit.
已经存在使用 pipenv 管理的项目通过
# 如果不添加参数 --two 或者 --three ,则通过默认的 python 来安装
$ pipenv install --three
Creating a virtualenv for this project...
# 安装完成后提示
To activate this project's virtualenv, run pipenv shell.
Alternatively, run a command inside the virtualenv with pipenv run.
# 如果安装报错,可以尝试
$ pipenv install --three --skip-lock
# 如果使用了 requirements.txt 管理依赖可以这样初始化
$ pipenv install --three -r requirements.txt
# 使用代理
$ pipenv install --skip-lock --pypi-mirror https://mirrors.aliyun.com/pypi/simple/
pip install 如果目录中没有Pipfile和Pipfile.lock,会自动生成。如果存在,则会自动检查 Pipfile 中的所有依赖
虚拟环境使用
- 注意,使用 pipenv 的一切操作都在工程目录执行
pipenv shell
进入 - 不要在非 pipenv shell 环境下执行操作
# 进入环境
$ pipenv shell
# 退出环境, 或者按 ctrl + D
$ exit
tips: 注意:千万不要使用 deactivate 命令退出
虚拟环境代理
工程目录新建文件 .env
内容为
PIPENV_PYPI_MIRROR=https://mirrors.aliyun.com/pypi/simple/
PIP_DEFAULT_TIMEOUT=300
PIPENV_IGNORE_VIRTUALENVS=-1
在虚拟环境目录种执行
echo PIPENV_PYPI_MIRROR=https://pypi.tuna.tsinghua.edu.cn/simple/ >.env
也可以制作一个快速脚本放在 ~/.zshrc 中,方便生成本地环境来做隔离化的代理管理
# for new local pipenv mirror
function pipenv-mirror-tsinghua() {
echo PIPENV_PYPI_MIRROR=https://pypi.tuna.tsinghua.edu.cn/simple/ > .env
cat .env
}
虚拟环境常用指令
# 锁定依赖
$ pipenv lock
# 安装依赖
$ pipenv sync
# 安装 开发工具依赖
$ pipenv sync --dev
# 如果依赖使用了 带有 dev 或者非发布依赖需要这么安装 否则报错
$ pipenv lock --pre
$ pipenv sync --pre
$ pipenv sync --dev --pre
# 检查当前工程,建议任何时候先使用这个命令防止错误
$ pipenv check
# 显示项目文件所在地
$ pipenv --where
# 显示虚拟环境实际文件路径
$ pipenv --venv
# 显示虚拟环境 python 解释器路径
$ pipenv --py
删除虚拟环境
$ pipenv --rm
注意:需要先进入虚拟环境后使用依赖管理
# Pipfile文件目录下
$ pipenv install requests
这里执行了两步操作:
- 安装到虚拟环境中,更新
Pipfile
里面的依赖版本 - 使用sha256算法更新
Pipfile.lock
文件
tips: 默认情况下会加锁,速度很慢,可以增加参数 –skip-lock 来加快安装,但是会出现版本可能对不上的问题
- 安装开发依赖
$ pipenv install httpie --dev
这种依赖不会出现在最终运行库,可以方便隔离发布和测试代码
# 更新某个依赖
$ pipenv update requests
# 更新全部依赖
$ pipenv update
tips: 如果不写具体依赖更新,会删除全部软件包重新安装到最新
# Pipfile文件目录下
$ pipenv uninstall requests
查看当前所有依赖
$ pipenv graph
httpie==2.3.0
- Pygments [required: >=2.5.2, installed: 2.7.3]
- requests [required: >=2.22.0, installed: 2.25.1]
- certifi [required: >=2017.4.17, installed: 2020.12.5]
- chardet [required: >=3.0.2,<5, installed: 4.0.0]
- idna [required: >=2.5,<3, installed: 2.10]
- urllib3 [required: >=1.21.1,<1.27, installed: 1.26.2]
- requests-toolbelt [required: >=0.9.1, installed: 0.9.1]
- requests [required: >=2.0.1,<3.0.0, installed: 2.25.1]
- certifi [required: >=2017.4.17, installed: 2020.12.5]
- chardet [required: >=3.0.2,<5, installed: 4.0.0]
- idna [required: >=2.5,<3, installed: 2.10]
- urllib3 [required: >=1.21.1,<1.27, installed: 1.26.2]
PySocks==1.7.1
pip list 也行,不过没 graph 清楚
$ pip list
$ pipenv lock
生成 Pipfile.lock 文件
tips: Pipfile.lock 本质是一个 json 文件,用于锁定依赖的,建议上传文件对照依赖
依赖环境管理
pipenv shell 运行时会读取默认工程目录下的 .env
文件作为环境变量文件
ENV_HTTP_PORT=2880
print('env: ENV_HTTP_PORT=%s' % os.getenv('ENV_HTTP_PORT'))
就会输出对应的环境变量
在 Pipfile 中编写脚本
[scripts]
main = "python main.py"
那么就可以看到可以执行的脚本代理
$ pipenv scripts
Command Script
--------- ---------------------------
main python main.py
那么不进入环境,直接执行则为
$ pipenv run main
Loading .env environment variables...
......
Recommend
-
182
Pipenv: Python Development Workflow for Humans [ ~ Dependency Scanning by PyUp.io ~ ] Pipenv is a tool that aims to bring the best of all packaging worlds (bund...
-
129
安全验证 - 知乎系统监测到您的网络环境存在异常,为保证您的正常访问,请点击下方验证按钮进行验证。在您验证完成前,该提示将多次出现
-
8
《Flask Web 开发实战》虚拟环境/依赖/Pipenv 等问题解决方法 1条回复 注:这篇文章的主要受众是《Flask...
-
10
Pipenv:新一代Python项目依赖管理工具 发表回复 UPDATE(2019/8/31):不要用 Pipenv...
-
6
使用 pipenv 代替 virtualenv 管理 python 包第一次接触到 pipenv 是因为看到 @董明伟大神的《使用 pipenv 管理你的项目》,之前可能和大家的选择类似使用 virtualenv 或者 pyenv 来管理 python 的包环境。virtualenv 是针对 python 的包的多版本管理,通过将 p...
-
6
更新于 2022/05/26 | 创建于 2022/05/26 Javascript开发者的Rust教程cargo依赖管理
-
11
Painlessly developing Python on NixOS with pipenvHomePainlessly developing Python on NixOS with pipenv8 March 20...
-
8
Article Pipenv and S2i: A better way to manage Python dependencies in containers ...
-
9
Python Hands-on How to manage Python projects with Pipenv Have your Python projects become a rat’s nest? Pipenv provides a clean and easy way t...
-
3
本文介绍Pipenv的用途以及为什么需要它,然后简单的介绍使用pipenv管理项目依赖的方法。 目录 Pipenv是Python的一个打包工具,解决了使用pip、virtualenv和传统的requirements.txt时常见的一些问题。 除了解决一些常见问...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK