Skip to main content
 首页 » 编程设计

python之配置 setup.py 以从存储库 URL 安装需求

2025年02月15日8oomusou

我正在创建一个模块,需要准备我的 setup.py 文件以满足一些要求。其中一项要求是 PyPI 中已有的一个包的分支,因此我想直接引用我的 GitHub 存储库。

我尝试了两种配置,第一种是:

setup( 
    'name': 'mymodule', 
    # other arguments 
    install_requires=[ 
        'myrequirement',  # The dependency name 
    ], 
    dependency_links=[ 
        'https://github.com/ihhcarus/myrequirement.git#egg=myrequirement',  # This is my repository location 
    ] 
) 

我使用 python setup.py sdist 创建我的模块的本地分发,当我运行 pip install path/to/module/dist/mymodule-0.1.tar.gz 它最终在 PyPI 而不是我的存储库上安装了版本。

另一个配置,我尝试更改需求名称以强制搜索依赖链接,如下所示:

setup( 
    'name': 'mymodule', 
    # other arguments 
    install_requires=[ 
        'myrequirement_alt',  # The dependency name with a suffix 
    ], 
    dependency_links=[ 
        'https://github.com/ihhcarus/myrequirement.git#egg=myrequirement_alt',  # This is my repository location 
    ] 
) 

但是有了这个,我最终只会得到一个错误,即 myrequirement_alt is not found...

所以我想问,在不使用 PyPI 的情况下实现这一目标的正确方法是什么?

请您参考如下方法:

要使依赖关系链接起作用,您需要将包的版本号添加到 https://github.com/ihhcarus/myrequirement.git#egg=myrequirement_alt。 否则它将不知道要安装什么。

例如:

setup( 
    'name': 'mymodule', 
    # other arguments 
    install_requires=[ 
        'myrequirement',  # The dependency name 
    ], 
    dependency_links=[ 
        'https://github.com/ihhcarus/myrequirement.git#egg=myrequirement_alt-1.3'  # Link with version at the end 
    ] 
) 

请注意,我根本不建议使用依赖关系链接,因为它们已被弃用。相反,您应该使用需求文件。