PIP:仅安装依赖项

发布于 2024-08-23 03:51:14 字数 521 浏览 12 评论 0原文

我有一个脚本,它创建一个 virtualenv,在其中安装 distributepip,然后选择性地克隆一个 git 存储库。

现在我已经安装了我将要处理的项目。但它的依赖项没有安装。如何让 pip 安装所有依赖项,就像我发出 pip install MyApp 一样?

编辑:显然我的问题与这个

不太确定,但是 pip install -e . 似乎可以做我想做的事情,而没有太多额外的东西。不过,我希望我的代码没有从 site-packages 链接。

I have a script that creates a virtualenv, installs distribute and pip in it and then optionally clones a git repo.

Now I have the project I will be working on, installed. But its dependencies are not installed. How can I make pip install all the dependencies as if I have issued a pip install MyApp?

EDIT: Appareantly my question is a duplicate of this one.

Not exactly sure but pip install -e . seems to do what I want without too many extra stuff lying around. I'd prefer if my code wasn't linked from site-packages though.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

你是暖光i 2024-08-30 03:51:14

如果您的依赖项在 setup.py 文件中定义,您可以首先使用以下方法将它们转储到外部文件:

python setup.py egg_info

这将在 YOUR_PROJECT.egg-info/requires.txt< 中列出您的所有依赖项/代码> 文件。然后您可以使用 pip 安装它们:

pip install -r *.egg-info/requires.txt

删除您刚刚创建的内容:

rm -rf *.egg-info/

以节省一些时间复制粘贴:

python setup.py egg_info
pip install -r *.egg-info/requires.txt
rm -rf *.egg-info/

If your dependencies are defined in the setup.py file, you can first dump them to an external file using:

python setup.py egg_info

This will list all your dependencies in YOUR_PROJECT.egg-info/requires.txt file. Then you can install them using pip:

pip install -r *.egg-info/requires.txt

to delete what you just created:

rm -rf *.egg-info/

to save some time copy pasting:

python setup.py egg_info
pip install -r *.egg-info/requires.txt
rm -rf *.egg-info/
谁人与我共长歌 2024-08-30 03:51:14

在我的包根目录中发出 pip install -e . 安装依赖项。

In my package root issuing pip install -e . installs dependencies.

把人绕傻吧 2024-08-30 03:51:14

您可以使用 pip-tools 创建一个 requirements.txt仅包含您的包的依赖项:

$ pip-compile -o requirements.txt setup.py

请注意,上述命令仅在您还没有 requirements.txt 文件时才有效。如果您碰巧已经有一个,请将其删除。

然后,您可以使用生成的 requirements.txt 运行 pip 来安装依赖项:

$ pip install -r requirements.txt

好处 1:

requirements.txt 将包含指示相关依赖项源自何处的注释。

奖励 2:

如果您的 setup.py 中有一个用于可选依赖项的 extras_require 部分,如下所示:

    ...
    extras_require={
        "development": [
            "wheel",
            "debugpy",
            "pytest",
        ],
    },
    ...

您可以创建 requirements.txt 通过使用以下方式包含可选的依赖项:

$ pip-compile -o requirements.txt --extra development setup.py

You can use pip-tools to create a requirements.txt that only contains the dependencies of your package:

$ pip-compile -o requirements.txt setup.py

Note that the command above only works if you do not already have a requirements.txt file. If you happen to have one already, just delete it.

Using the generated requirements.txt you can then run pip to install the dependencies:

$ pip install -r requirements.txt

Bonus 1:

The requirements.txt will include comments that indicate where the regarding dependency originates from.

Bonus 2:

If you have have an extras_require section for optional dependencies in your setup.py that looks e.g. like this:

    ...
    extras_require={
        "development": [
            "wheel",
            "debugpy",
            "pytest",
        ],
    },
    ...

You can create the requirements.txt including the optional dependencies by using:

$ pip-compile -o requirements.txt --extra development setup.py
许你一世情深 2024-08-30 03:51:14

要安装项目的依赖项(即 install_requires + extra_requires),您必须使用 setuptools egg-info 提取依赖项,然后安装过滤后的列表组合的依赖关系:

python setup.py egg_info
pip install `grep -v '^\[' *.egg-info/requires.txt`

To install your project's dependencies (i.e. install_requires + extra_requires) you have to extract your dependencies using setuptools egg-info and then install the filtered list of the combined dependencies:

python setup.py egg_info
pip install `grep -v '^\[' *.egg-info/requires.txt`
一念一轮回 2024-08-30 03:51:14

您应该使用 pip 要求文件。

本质上,将所有要求放在文件中的每一行中,然后使用命令将其传递给 pip

pip install -r requirements.txt

更重要的是,如果您有标准环境,pip 实际上可以使用以下命令从现有安装中转储这样的文件:

pip freeze

您可以将将因此生成的文件直接添加到 pip 要求中,并从部署脚本中调用前面的命令。

很酷,不是吗? :)

You should use the pip requirements file.

Essentially, place all your requirements, one in each line in a file and pass that to pip using the command

pip install -r requirements.txt

What more, if you have a standard environment, pip can actually dump such a file from existing installs using the command:

pip freeze

You can put the file thus generated directly into the pip requirements, and call the previous command from your deployment script.

Pretty cool, isnt it? :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文