使用 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/

6840 次点击
所在节点    Python
33 条回复
greatx
2018-05-03 11:23:42 +08:00
正在使用
lrh3321
2018-05-03 11:38:38 +08:00
正在用+1
pynix
2018-05-03 11:40:44 +08:00
bundler
Sylv
2018-05-03 11:47:58 +08:00
例子里 pipenv 的虚拟环境下该改用 pipenv install 来安装包吧?否则还用 pip install 的话,不会把依赖写入 Pipfile。
SuperMild
2018-05-03 11:52:28 +08:00
感觉 python 社区一直很有活力,一个包管理不好用就再做一个,还不好又造一个,网站框架也是很多,而且各有特色,这种活力让人安心
Rocka
2018-05-03 12:02:45 +08:00
@SuperMild 怎么看着像高级黑 …
yoohwzy
2018-05-03 12:21:03 +08:00
@Sylv #4 正解, 楼主的用法是错误的, 或者说是不合适的
wsgzao
2018-05-03 13:58:23 +08:00
@Sylv @yoohwzy
谢谢提醒,主要是团队内部目前使用 virtualenvwrapper,然后刚刚在转 pipenv,两者还没有确认到底怎么融合,我这边考虑到 requirements 里面的配置所以必须沿用这个语法
julyclyde
2018-05-03 14:00:07 +08:00
@yoohwzy lz 还停留在刚刚认识到一点点东西就很欣喜的阶段
再过几年就好了
wsgzao
2018-05-03 14:05:06 +08:00
@julyclyde #9 不用这么黑我吧,上次 python 升级 2.7 的问题你也没告诉我答案,本来我分享信息也是基于学习交流的目的,有写的不对的修改或者讨论为什么
julyclyde
2018-05-03 14:07:00 +08:00
@wsgzao 上回我已经指出了你的文章的不足在于还没明确说明到底解决了什么问题, 就在解决问题了。只是你不认为这么写有啥错而已
kingcos
2018-05-03 14:07:22 +08:00
现在喜欢 Conda。。
wsgzao
2018-05-03 14:15:14 +08:00
@julyclyde 我之前回复是先道歉没有找到为什么,然后我去 python 官网看了一遍 Install 或者 Google 相关信息确实没有找到有地方解释 why,如果无法从侧面了解我想只有看源码是怎么编译执行。

@kingcos
我现在 Win10 上是使用 Anaconda 2 和 3 两个版本,Linux 下使用 anaconda 是否也有奇效呢?
julyclyde
2018-05-03 14:20:05 +08:00
@wsgzao 我上次跟你说的是,你的文章里写的“因为 Python 2.7.13 以后版本会自动完善 yum 配置,所以不必参考以前的网上文章去修改其他地方”既没有正确的说明(你以为的) python 和 yum 的关系,也没说“以前网上文章”哪些内容是不再需要的
wsgzao
2018-05-03 14:21:05 +08:00
@julyclyde #14 我错了
wsgzao
2018-05-03 14:36:03 +08:00
@julyclyde #14 已修改原文
> Python 2.7.13 以后版本可以正常执行编译安装,不必参考网上文章去修改其他地方(很抱歉我不知道 Python2.7.x 为什么不需要像 Python3 那样调整)

``` bash
# 针对 Python3 调整安装目录
./configure --prefix=/usr/local/python3.6
make && make install
# 先修改老的连接,执行
mv /usr/bin/python /usr/bin/python2.6
# 再建立新连接
ln -s /usr/local/python3.6/bin/python3.6 /usr/bin/python
# 解决升级后 YUM 无法使用
vim /usr/bin/yum

将#!/usr/bin/python 修改为 #!/usr/bin/python2.6

# 测试是否修复
yum repolist

```
sidewalk
2018-05-03 14:37:16 +08:00
pipenv install 好像会自动先搜索项目中的 requirements.txt 进行安装
julyclyde
2018-05-03 14:43:47 +08:00
@wsgzao
yoohwzy
2018-05-03 15:02:57 +08:00
@wsgzao #8 这么说吧, 你引用的那个视频里就展示了生成 `requirements.txt` 的命令, pipenv 也是可以自动搜索 `requirements.txt` 文件的. 具体可见 https://docs.pipenv.org/basics/#importing-from-requirements-txthttps://docs.pipenv.org/advanced/#generating-a-requirements-txt
wsgzao
2018-05-03 15:09:00 +08:00
@yoohwzy #19 谢谢答复

我的 requirements.txt 内头部带有这两句话,目前大家是通过 gitlab 同步数据的,所以暂时不能动
--index-url=http://172.31.96.201:8081/simple/
--trusted-host=172.31.96.201

等其他人熟悉的差不多了就可以统一调整了,说实话当大家习惯了一种做事方式后真的很难推动一群人再二次改变
[[source]]
url = "https://pypi.python.org/simple"
verify_ssl = true

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

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

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

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

© 2021 V2EX