使用 pipenv 代替 virtualenv 管理 Python 包

2018-05-03 11:18:40 +08:00
 wsgzao

前言

第一次接触到 pipenv 是因为看到 @董明伟大神的《使用 pipenv 管理你的项目》,之前可能和大家的选择类似使用 virtualenv 或者 pyenv 来管理 python 的包环境。virtualenv 是针对 python 的包的多版本管理,通过将 python 包安装到一个模块来作为 python 的包虚拟环境,通过切换目录来实现不同包环境间的切换。pyenv 是针对 python 版本的管理,通过修改环境变量的方式实现;虽然我自己对 pipenv 的掌握程度还不深,但是我自己能感受到更加简单而清晰的 python 包管理方式,并且 pipenv 还是 Python 官方正式推荐的 python 包管理工具。原文如下:

Pipenv — the officially recommended Python packaging tool from Python.org, free (as in freedom).

Pipenv 官方推荐的 Python 包管理工具

更新历史

2017 年 04 月 25 日 - 初稿

阅读原文 - https://wsgzao.github.io/post/pipenv/

扩展阅读

Pipenv - https://docs.pipenv.org/ Pipenv & 虚拟环境 - http://pythonguidecn.readthedocs.io/zh/latest/dev/virtualenvs.html


推荐阅读

使用 pipenv 管理你的项目 @董伟明 http://www.dongwm.com/archives/%E4%BD%BF%E7%94%A8pipenv%E7%AE%A1%E7%90%86%E4%BD%A0%E7%9A%84%E9%A1%B9%E7%9B%AE/

[ python 基础系列 ] - pipenv 试用过程分享 http://pylixm.cc/posts/2018-01-13-python-pipenv.html

Pipenv 官方简介

Pipenv: Python Development Workflow for Humans

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.

It automatically creates and manages a virtualenv for your projects, as well as adds/removes packages from your Pipfile as you install/uninstall packages. It also generates the ever – important Pipfile.lock, which is used to produce deterministic builds.

The problems that Pipenv seeks to solve are multi-faceted:

You no longer need to use pip and virtualenv separately. They work together. Managing a requirements.txt file can be problematic, so Pipenv uses the upcoming Pipfile and Pipfile.lock instead, which is superior for basic use cases. Hashes are used everywhere, always. Security. Automatically expose security vulnerabilities. Give you insight into your dependency graph (e.g. $ pipenv graph). Streamline development workflow by loading .env files.

Pipenv 安装和使用

我的使用深度不高,就以目前我实际使用 pipenv 的方式为例

# pip 离线下载
# pip install --download DIR -r requirements.txt
mkdir pipenv
pip install -d ~/pipenv/ pipenv

# pip 离线安装 pipenv
pip install --no-index --find-links=pipenv/ pipenv

# 使用 pipenv 创建虚拟环境
mkdir win_ansible
cd win_ansible
pipenv shell
pip install --no-index --find-links=pip-ansible-2.4.3.0/ -r requirements.txt

# 升级 ansible 版本
pip install --no-index --find-links=pip-ansible-2.5.0/ -r requirements.txt -U

# 退出虚拟环境
exit

# 对不同开发用户自动创建 python 虚拟环境
vim ~/.bash_profile
pipenv shell

# 虚拟环境会在当前用户家目录自动创建
test101@JQ/root#su - wangao
Spawning environment shell (/bin/bash). Use 'exit' to leave.
test101@JQ/home/wangao$. /home/wangao/.local/share/virtualenvs/wangao-iOSX51hl/bin/activate

# 沿用 pip 创建 requirements.txt ,该方法相对 Pipfile 来说不是最佳
(wangao-iOSX51hl) test101@JQ/home/wangao/git/ansible$cat requirements.txt 
--index-url=http://172.31.96.201:8081/simple/
--trusted-host=172.31.96.201
ansible
ansible-cmdb
pywinrm

# 通过 gitlab 同步控制 python 包环境
git checkout develop
git pull origin develop
pip install -r requirements.txt -U

推荐参考的文章

Python 2.6 升级至 Python 2.7 的实践心得 - https://wsgzao.github.io/post/python-2-6-update-to-2-7/ 使用 pypiserver 快速搭建内网离线 pypi 仓库实践 - https://wsgzao.github.io/post/pypiserver/ RHEL7/CentOS7 在线和离线安装 GitLab 配置使用实践 - https://wsgzao.github.io/post/gitlab/

6860 次点击
所在节点    Python
33 条回复
sleshep
2018-05-03 16:29:40 +08:00
pyenv 一个就够了,配好了什么版本都可以用
vimiix
2018-05-03 17:18:09 +08:00
lfzyx
2018-05-03 17:19:42 +08:00
Python 官方正式推荐的 python 包管理工具是 python3 -m venv

python3 -m venv

python3 -m venv

python3 -m venv

python3 -m venv

python3 -m venv
wqzjk393
2018-05-03 17:20:27 +08:00
为啥我一直感觉 pipenv 很卡… install 一个包要半天才能反应过来。后来一怒之下虚拟环境都不用了反正用的包就那几个…
chroming
2018-05-03 17:54:42 +08:00
@wqzjk393 #24 确实卡
chroming
2018-05-03 17:56:07 +08:00
pipenv 相比 pyenv 优势是支持 win
filetype
2018-05-03 17:56:52 +08:00
pyenv 不是很好吗。
kingcos
2018-05-03 17:57:50 +08:00
@wsgzao Conda 还装这么多版本的吗…
tulongtou
2018-05-03 18:58:53 +08:00
看了这使用方式,完全没感觉哪里好。venv 是 python3 自带的包,直接 python3 -m venv 就完事了
lightening
2018-05-03 19:22:36 +08:00
因为 pyenv 根本就不能 lock 包啊……
核心思想就是“我想要的包的版本”和“最后一次安装能运行的包的版本”要分开存放。
Pipfile 是可以手动修改的,用来描述“我想要什么包的版本”。

https://github.com/pypa/pipfile
lightening
2018-05-03 19:33:40 +08:00
@wsgzao 其实用 requirements.txt ,就失去了 pipenv 最主要的意义了。

你说 requirements.txt 到底是手动编辑好还是让 pip 自动生成好…… 自动生成的话,你的 virtualenv 里面所有的包都会写进去,包括那些只是你个人使用而不是项目的一部分的包。

手写的话,版本怎么写呢?写大致的版本范围把,如果远程仓库的包更新了,别人(或者放到服务器上)用你的 requirements 安装不一定和你的版本相同,所以你机器上能用不保证别人机器上能用;写精确版本把,下次升级包时烦死。

所以 Python 社区终于学了 Ruby 的 Bundler,把 pipfile 和 pipfile.lock 分开。pipfile 手写或者让 pipenv 生成,只写大致要求的版本号就好。pipfile.lock 由 pipenv 生成,记录每个包以及他们依赖的包的精确版本。这样如果跑到服务器上或别人电脑上跑 pipenv install,就会根据 pipfile.lock 安装和你自己上次测试好用的 100% 一样版本的包,确保不会出问题。
wsgzao
2018-05-04 09:10:38 +08:00
@lightening
谢谢您的经验,我会积极推动大家一起改进
@kingcos
主要内部 aop 自动化运维平台一期使用 python 2.7 + django 1.x 这样的稳定版搭建,现在想重构很困难,因为不熟悉 python 3.6 + django 2.0 的差异到底有多大,所以想先尝试开发 demo 出来理解下,就选择了 Anaconda 2 + 3 并在这基础上再独立创建 conda 虚拟 env,也许也不是简单有效的方案,再摸索下吧
vimiix
2018-05-04 16:07:02 +08:00
@wqzjk393
@chroming
卡是因为 pip 源使用的是默认的源,我一般会修改 Pipfile 中的 source 字段,替换成清华的源,就好多了。Lock 也很快了。

[[source]]

url = "https://pypi.tuna.tsinghua.edu.cn/simple"
verify_ssl = true
name = "pypi"

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/451681

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX