V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
wsgzao
V2EX  ›  Python

使用 pipenv 代替 virtualenv 管理 Python 包

  •  
  •   wsgzao ·
    wsgzao · 2018-05-03 11:18:40 +08:00 · 6791 次点击
    这是一个创建于 2157 天前的主题,其中的信息可能已经有所发展或是发生改变。

    前言

    第一次接触到 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/

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

    @kingcos
    我现在 Win10 上是使用 Anaconda 2 和 3 两个版本,Linux 下使用 anaconda 是否也有奇效呢?
    julyclyde
        14
    julyclyde  
       2018-05-03 14:20:05 +08:00
    @wsgzao 我上次跟你说的是,你的文章里写的“因为 Python 2.7.13 以后版本会自动完善 yum 配置,所以不必参考以前的网上文章去修改其他地方”既没有正确的说明(你以为的) python 和 yum 的关系,也没说“以前网上文章”哪些内容是不再需要的
    wsgzao
        15
    wsgzao  
    OP
       2018-05-03 14:21:05 +08:00
    @julyclyde #14 我错了
    wsgzao
        16
    wsgzao  
    OP
       2018-05-03 14:36:03 +08:00   ❤️ 1
    @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
        17
    sidewalk  
       2018-05-03 14:37:16 +08:00 via iPhone
    pipenv install 好像会自动先搜索项目中的 requirements.txt 进行安装
    julyclyde
        18
    julyclyde  
       2018-05-03 14:43:47 +08:00
    @wsgzao
    yoohwzy
        19
    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
        20
    wsgzao  
    OP
       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
    sleshep
        21
    sleshep  
       2018-05-03 16:29:40 +08:00   ❤️ 1
    pyenv 一个就够了,配好了什么版本都可以用
    lfzyx
        23
    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
        24
    wqzjk393  
       2018-05-03 17:20:27 +08:00 via iPhone
    为啥我一直感觉 pipenv 很卡… install 一个包要半天才能反应过来。后来一怒之下虚拟环境都不用了反正用的包就那几个…
    chroming
        25
    chroming  
       2018-05-03 17:54:42 +08:00
    @wqzjk393 #24 确实卡
    chroming
        26
    chroming  
       2018-05-03 17:56:07 +08:00
    pipenv 相比 pyenv 优势是支持 win
    filetype
        27
    filetype  
       2018-05-03 17:56:52 +08:00
    pyenv 不是很好吗。
    kingcos
        28
    kingcos  
       2018-05-03 17:57:50 +08:00 via iPhone
    @wsgzao Conda 还装这么多版本的吗…
    tulongtou
        29
    tulongtou  
       2018-05-03 18:58:53 +08:00
    看了这使用方式,完全没感觉哪里好。venv 是 python3 自带的包,直接 python3 -m venv 就完事了
    lightening
        30
    lightening  
       2018-05-03 19:22:36 +08:00
    因为 pyenv 根本就不能 lock 包啊……
    核心思想就是“我想要的包的版本”和“最后一次安装能运行的包的版本”要分开存放。
    Pipfile 是可以手动修改的,用来描述“我想要什么包的版本”。

    https://github.com/pypa/pipfile
    lightening
        31
    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
        32
    wsgzao  
    OP
       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
        33
    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"
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5411 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 06:47 · PVG 14:47 · LAX 23:47 · JFK 02:47
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.