让pip与git和GitHub仓库一起使用

8 投票
3 回答
7121 浏览
提问于 2025-04-17 15:00

我正在写一个Python应用程序,它依赖于另一个托管在GitHub上的应用程序(这个应用从来没有在pypi上发布),这是出于开发的需要。

我们称它们为:

  • 正在编写的应用:AppA
  • 在GitHub上的应用:AppB

在AppA中,setup.py的内容是这样的:

# coding=utf-8
import sys
try:
    from setuptools import setup, find_packages
except ImportError:
    import distribute_setup
    distribute_setup.use_setuptools()
    from setuptools import setup, find_packages

setup(
    ...
    install_requires=[
        # other requirements that install correctly
        'app_b==0.1.1'
    ],
    dependency_links=[
        'git+https://github.com/user/app_b.git@0.1.1#egg=app_b-0.1.1'
    ]
)

现在,AppA在每次代码推送时都通过Jenkins CI进行构建,但我遇到了一个错误,导致构建失败,错误信息如下:

error: Download error for git+https://github.com/user/app_b.git@0.1.1: unknown url type: git+https

有趣的是,这个问题只在Jenkins上出现,在我的电脑上运行得很好。我尝试了GitHub提供的其他SSH网址,但那些根本没有被考虑下载。

现在,AppA被包含在一个同样由Jenkins构建的项目的依赖文件中,所以通过pip install AppApip install AppB手动安装依赖是不行的,因为这些依赖会自动通过requirements.txt文件安装。

有没有办法让pip和GitHub网址一起工作呢?

任何帮助都将非常感激 :)

提前谢谢你们!

3 个回答

0

我在2019年也遇到过类似的问题,但原因不同。现在,dependency_links 在pip中已经不再支持了(我测试过pip版本大于等于20.0.0)。对于我的情况,我是通过使用install_requirements并定义一个直接引用来解决这个问题的(可以参考pip手册中的direct reference)。

...
install_requirements = [
    <dependencyname> @ git+<url of dependency repository>@<branchname or tag>
]

我在网上创建了一个公开的示例仓库,叫做thepackage,你可以在这里找到更多细节:https://gitlab.rhrk.uni-kl.de/scheliga/thepackage

2

来自 pip 文档 -

pip currently supports cloning over git, git+http and git+ssh:

git+git://git.myproject.org/MyProject#egg=MyProject
git+http://git.myproject.org/MyProject#egg=MyProject
git+ssh://git.myproject.org/MyProject#egg=MyProject

试着把 git+https 换成 git+git

12

问题不在于 pip,而是在于 setuptools。负责调用 setup() 的是 setuptools 这个包(也叫 setuptools 或 distribute 项目)。

无论是 setuptools 还是 distribute 都不理解那种网址,它们只懂得处理 tarball(压缩包)或 zip 文件。

试着指向 GitHub 的下载网址——通常是一个 zip 文件。

你的 dependency_links 可能看起来像这样:

dependency_links=[
    'https://github.com/user/app_b/archive/0.1.1.zip#egg=app_b-0.1.1'
]

想了解更多信息,可以查看 http://peak.telecommunity.com/DevCenter/setuptools#dependencies-that-aren-t-in-pypi

撰写回答