【问题标题】:Write setup.py using git repo in install_requires to detect already installed在 install_requires 中使用 git repo 编写 setup.py 以检测是否已安装
【发布时间】:2021-10-26 17:46:33
【问题描述】:

我有一个带有 git+ssh 依赖的包,像这样:

setup(
    name="setuprequires",
    version="0.1",
    packages=["setuprequires"],
    install_requires=[
        r"hello-world @ git+ssh://git@github.com/hello/hello-world.git#egg=hello-world"
    ],  
)

(我正在关注关于 git repo 依赖项的最新答案 here。)

但是,如果hello-world 已经安装在我当前的环境中,我想跳过安装它。然而,如果我运行pip install -e . 两次,第二次仍然会克隆hello-world。相反,如果我刚刚使用install_requires=[r"hello-world"],它会正确地检测到这个要求已经得到满足。

在我看来git+ssh 不能很好地检测已经满足的需求。我认为拥有#egg=hello-world 可以解决这个问题。我错过了什么吗?

【问题讨论】:

    标签: python git setuptools setup.py


    【解决方案1】:

    要检测存储库是最新的(或使用了某个提交),您需要先克隆它,因此即使它试图从缓存中获取它,您仍然首先需要拥有它克隆。

    在第一次克隆之后,它应该将它与现有的克隆存储库进行比较。

    由于您没有指定任何提交,因此我猜它仍在拉动。见Git._should_fetch()类方法。

    相反,尝试指定您要使用的提交/标记as mentioned in the docs,它应该开始缓存:

    git+https://git.example.com/MyProject.git@<hash>#egg=MyProject
    git+https://git.example.com/MyProject.git@<tag>#egg=MyProject
    git+ssh://git.example.com/MyProject.git@<hash>#egg=MyProject
    git+ssh://git.example.com/MyProject.git@<tag>#egg=MyProject
    

    编辑:

    我注意到,如果你使用分支,缓存就会被破坏。它不是refs/...,所以它不是显式的远程分支拉取,也不是散列。然而,对于提交哈希,它可以完美地工作并将创建的轮子存储在--cache-dir 目标中,然后在第二次执行pip install 命令时选择该目标。 (bug created)

    示例:

    from setuptools import setup
    
    setup(
        name="testing-git",
        install_requires=[
            # cached properly
            "requests @ git+ssh://git@github.com/psf/requests.git@cd4762d5a3b56d8933d1d9c1dff365fc5db4c768#egg=requests-3.0"
            # not cached
            # "requests @ git+ssh://git@github.com/psf/requests.git@3.0#egg=requests-3.0"
        ]
    )
    

    【讨论】:

      猜你喜欢
      • 2020-06-10
      • 2011-08-30
      • 2021-05-14
      • 2019-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-06
      • 2018-04-05
      相关资源
      最近更新 更多