告诉pip安装需求fi中列出的包的依赖项

2024-04-30 05:04:55 发布

您现在位置:Python中文网/ 问答频道 /正文

开发Django web应用程序时,我有一个需要在virtualenv中安装的包列表。说:

Django==1.3.1
--extra-index-url=http://dist.pinaxproject.com/dev/
Pinax==0.9b1.dev10
git+git://github.com/pinax/pinax-theme-bootstrap.git@cff4f5bbe9f87f0c67ee9ada9aa8ae82978f9890
# and other packages

最初,我在开发过程中一个接一个地手动安装它们。这安装了所需的依赖项,最后在部署应用程序之前使用了pip freeze

问题是,当我升级了一些包时,一些依赖项不再被使用,也不再是必需的,但它们仍然由pip freeze列出。

现在,我想用这种方式建立一个新的virtualenv:

  • 将所需的包(不包括它们的依赖项)放入需求文件中,
    就像manual-requirements.txt
  • 安装它们及其依赖项
    pip install -r manual-requirement.txt(☆问题,这不安装依赖项)
  • 冻结完整的virtualenv
    pip freeze -r manual-requirements.txt > full-requirements.txt
    然后部署。

有什么方法可以做到这一点,而不需要在新的virtualenv中手动重新安装包来获取它们的依赖项?这很容易出错,我希望自动化从不再需要的旧依赖项中清除virtualenv的过程。

编辑:实际上,即使the documentation告诉我们这些文件是平面的,pip也不会安装需求文件中未显式列出的依赖项。我错了我希望安装的依赖项。我将让这个问题为任何人怀疑pip没有安装所有依赖项。


Tags: pip文件djangogittxtcom应用程序virtualenv
1条回答
网友
1楼 · 发布于 2024-04-30 05:04:55

考虑到您对这个问题的评论(您说为单个包执行安装可以按预期工作),我建议在您的需求文件上循环。在bash中:

#!/bin/sh
while read p; do
  pip install $p
done < requirements.pip

啊!

网友
2楼 · 发布于 2024-04-30 05:04:55

简单地说,使用:

pip install -r requirement.txt

它可以安装需求文件中列出的所有内容。

网友
3楼 · 发布于 2024-04-30 05:04:55

Any way to do this without manually re-installing the packages in a new virtualenv to get their dependencies ? This would be error-prone and I'd like to automate the process of cleaning the virtualenv from no-longer-needed old dependencies.

这就是pip tools包的用途(fromhttps://github.com/jazzband/pip-tools):

安装

$ pip install --upgrade pip  # pip-tools needs pip==6.1 or higher (!)
$ pip install pip-tools

pip编译的示例用法

假设您有一个烧瓶项目,并希望将其固定以供生产。将以下行写入文件:

# requirements.in
Flask

现在,运行pip compile requirements.in:

$ pip-compile requirements.in
#
# This file is autogenerated by pip-compile
# Make changes in requirements.in, then run this to update:
#
#    pip-compile requirements.in
#
flask==0.10.1
itsdangerous==0.24        # via flask
jinja2==2.7.3             # via flask
markupsafe==0.23          # via jinja2
werkzeug==0.10.4          # via flask

它将生成您的requirements.txt,并固定所有烧瓶依赖项(和所有底层依赖项)。将此文件置于版本控制之下,并定期重新运行pip-compile以更新包。

pip sync的示例用法

现在您已经有了一个requirements.txt,您可以使用pip-sync来更新您的虚拟环境,以准确反映其中的内容。注意:这将安装/升级/卸载与requirements.txt内容匹配所需的所有内容。

$ pip-sync
Uninstalling flake8-2.4.1:
  Successfully uninstalled flake8-2.4.1
Collecting click==4.1
  Downloading click-4.1-py2.py3-none-any.whl (62kB)
    100% |████████████████████████████████| 65kB 1.8MB/s
  Found existing installation: click 4.0
    Uninstalling click-4.0:
      Successfully uninstalled click-4.0
Successfully installed click-4.1

相关问题 更多 >