【问题标题】:Can I tell pip to ignore requirements installed via `setup.py develop`?我可以告诉 pip 忽略通过 setup.py develop 安装的要求吗?
【发布时间】:2014-06-06 21:58:36
【问题描述】:

我正在开发一个 Python 库,我已将其安装在本地 virtualenv 中进行测试。我用 pip 安装了几个依赖项。当我这样做时

$ pip freeze > requirements.txt

它像这样添加我当前的项目:

-e git+git@github.com:path/to/my/project@somehash#egg=lib-master

我必须手动删除 - 我的项目实际上并不依赖于自身。是否可以向 pip 传递一个参数,上面写着“嘿,忽略这个/这些类型的包?”

【问题讨论】:

    标签: python pip virtualenv


    【解决方案1】:

    这是一个错误的问题。您不应该尝试根据您当前安装的内容来建立要求。您的项目应指定其要求,并根据此信息(和要求的要求)计算最终的要求集。请注意,应指定开发要求,以便可以根据需要单独安装 - 请参阅 Setuptools “development” RequirementsHow to customize a requirements.txt for multiple environments?
    不幸的是 pip 无法计算这个yet。您可以使用pip-tools 计算此值,它将结果写入 requirements.txt 文件。有关如何使用 pip-tools 的完整示例,请参阅我的 answer

    【讨论】:

    • 我确实知道很多 now :) - 我在 setup.py 中指定了要求,而对于开发我只使用 pipenv 它同时具有 --dev 和正常安装选项。
    【解决方案2】:

    最简单的解决方案是将pip freeze 的结果通过-v (反向匹配)传递给grep

    pip freeze | grep -v 'project_name' > requirements.txt
    

    演示:

    $ mkvirtualenv test
    New python executable in test/bin/python
    Installing Setuptools...done.
    Installing Pip...done.
    (test)$ pip freeze
    wsgiref==0.1.2
    (test)$ pip install requests
    Downloading/unpacking requests
      Downloading requests-2.2.1.tar.gz (421kB): 421kB downloaded
      Running setup.py egg_info for package requests
    
    Installing collected packages: requests
      Running setup.py install for requests
    
    Successfully installed requests
    Cleaning up...
    (test)$ pip freeze
    requests==2.2.1
    wsgiref==0.1.2
    (test)$ pip freeze | grep -v 'requests'
    wsgiref==0.1.2
    (test)$ pip freeze | grep -v 'requests' > requirements.txt
    (test)$ cat requirements.txt 
    wsgiref==0.1.2
    

    另见:Negative matching using grep (match lines that do not contain foo)

    希望对您有所帮助。

    【讨论】:

    • 不错!我喜欢它——唯一的“缺点”是它在 Windows 上确实需要 *nix 或 unxutils,但我希望这将是我得到的最佳答案。
    • @WayneWerner 是的,我很确定pip 不支持您的用例。它实际上“在某种程度上”类似于我最近问过的herepip 根本不是 yum,简单的包管理器。
    猜你喜欢
    • 2022-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-05
    • 2015-08-04
    • 2013-12-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多