Skip to main content
 首页 » 编程设计

依赖另一个子包的Python命名空间子包安装失败

2024年12月31日9hnrainll

我的目标是创建一个包含两个子包的命名空间包:foo 和 bar(依赖于 foo),并将命名空间包推送到 git 存储库(不将其发布到 PyPI)以便我可以安装任一子包与 repo url 打包。

我正在关注 this tutorial创建命名空间结构:

root/ 
 ├ setup.py 
 └ microlibs/ 
    ├ foo/ 
    │  ├ setup.py 
    │  ├ macrolib/ 
    │     └ foo/ 
    │        ├ __init__.py 
    │        ├ module1.py 
    │        ├ ... 
    │        └ moduleN.py 
    . 
    . 
    . 
    └ bar/ 
       ├ setup.py 
       ├ macrolib/ 
          └ bar/ 
             ├ __init__.py 
             ├ module1.py 
             ├ ... 
             └ moduleN.py 

foo的setup.py文件没有依赖:

foo/setup.py

from setuptools import setup 
microlib_name = 'macrolib.foo' 
setup( 
    name=microlib_name, 
    version="0.1.0", 
    namespace_packages=['macrolib'], 
    packages=[microlib_name], 
    install_requires=[] 
) 

由于bar的依赖包括foo,bar的setup.py在install_requires列表中包括macrolib.bar:

bar/setup.py

from setuptools import setup 
microlib_name = 'macrolib.bar' 
setup( 
    name=microlib_name, 
    version="0.1.0", 
    namespace_packages=['macrolib'], 
    packages=[microlib_name], 
    install_requires=[ 
        'macrolib.foo' 
    ] 
) 

推送到 bitbucket 存储库后,我可以毫无问题地安装 macrolib.foo 存储库的子目录。

$ pip install git+http://path/to/repo.git@<branch name>#"subdirectory=foo&egg=macrolib.foo" 

安装了 macrolib.foo 后,我还可以安装 macrolib.bar 而不会出现存储库子目录的问题。

$ pip install git+http://path/to/repo.git@<branch name>#"subdirectory=bar&egg=macrolib.bar" 

但是,如果我尝试安装 macrolib.bar 而没有先安装 macrolib.foo,则安装失败。

$ pip install git+http://path/to/repo.git@<branch name>#"subdirectory=bar&egg=macrolib.bar" 

错误:

Collecting macrolib.foo (from macrolib.bar==0.1.0) 
  Could not find a version that satisfies the requirement macrolib.foo (from macrolib.bar==0.1.0) (from versions: ) 
No matching distribution found for macrolib.foo (from macrolib.bar==0.1.0) 

我猜这是因为 bar/setup.py 中缺少 dependency_links。所以我尝试了不同的链接 url 组合,但都因相同的错误而失败。

我试过的格式:

dependency_links=['http://path/to/repo.git@<branch name>#"subdirectory=foo&egg=macrolib.foo"'] 
 
dependency_links=['http://path/to/repo.git@<branch name>#subdirectory=foo&egg=macrolib.foo'] 
 
dependency_links=['http://path/to/repo/tarball/<branch name>#"subdirectory=foo&egg=macrolib.foo"'] 
 
dependency_links=['http://path/to/repo/tarball/<branch name>#subdirectory=foo&egg=macrolib.foo'] 
 
dependency_links=['http://path/to/repo/archive/<branch name>.zip#"subdirectory=foo&egg=macrolib.foo"'] 
 
dependency_links=['http://path/to/repo/archive/<branch name>.zip#subdirectory=foo&egg=macrolib.foo'] 

或为上述所有网址添加前缀“git+”。

我的问题是,为了将 macrolib.foo 作为依赖项安装,dependency_links 的正确 url 格式是什么,或者是否有任何其他方法可以使其工作?

请您参考如下方法:

这是正确的格式(添加“git+”和依赖版本):

dependency_links=['git+http://path/to/repo.git@<branch name>#subdirectory=foo&egg=macrolib.foo-0.1.0'] 

你需要请求 pip 来处理它:

pip install --process-dependency-links git+http://path/to/repo.git@<branch name>#"subdirectory=bar&egg=macrolib.bar"