【问题标题】:Python setup.py with private repository on GitLab as dependency_links based on commit IDPython setup.py 与 GitLab 上的私有存储库作为基于提交 ID 的依赖链接
【发布时间】:2020-10-06 09:34:08
【问题描述】:

我正在尝试安装一个私有依赖项(不是 Python 在 PyPI 上可以找到的)。

我已经在文件setup.py 中添加了这个(如这里解释:https://python-packaging.readthedocs.io/en/latest/dependencies.html#packages-not-on-pypi):

dependency_links = [
        'https://gitlab.com/<PRIVATE_ORG>/<PRIVATE_REPO>.git@<COMMIT_ID>'
      ]

在官方文档中,他们并没有真正详细解释该 URL 的格式,但是在 @ 之后使用 &lt;COMMIT_ID 听起来很合理(因为它在各种其他语言和依赖项管理工具中完成) .

执行命令python setup.py install 时,我在日志/输出中看到:

Reading https://gitlab.com/<PRIVATE_ORG>/<PRIVATE_REPO>.git@<COMMIT_ID>

但是我没有看到实际安装的包,正如我从其他依赖项的日志/输出中看到的那样。

我知道我的git 命令有一个有效的 GitLab 访问令牌设置,因为我已经运行了这个:

git config \
      --global \
      url."https://<ACCESS_TOKEN_NAME>:<ACCESS_TOKEN_VALUE>@gitlab.com".insteadOf \
      "https://gitlab.com"

我可以在检查 git 配置时看到它:

git config --list | grep gitlab
url.https://<ACCESS_TOKEN_NAME>:<ACCESS_TOKEN_VALUE>@gitlab.com.insteadof=https://gitlab.com
  • Python 在运行setup.py 时是否使用git 命令?
  • 如何在 Python setup.py 文件中指定私有 GitLab 依赖项? 应该基于提交 ID 而不是包版本
  • 上面有什么问题?
  • 我也感觉当使用pip install 和定位setup.py 而不是运行python setup.py install 时运行可能会有所不同,是否有一种独特的方法可以使这两种Python 安装方式一起工作?我问这个是因为在摆弄dependency_links 时,我尝试了各种类似git+ssh 而不是https 和其他变体的东西,所有这些都未能安装带有各种日志/输出的私有存储库,表明没有找到存储库。

编辑

我已经避免使用dependency_links,因为它似乎已被弃用,所以我使用了答案中提出的解决方案:

install_requires=[
    ...
    "mylibraryname @ git+https://<ACCESS_TOKEN_NAME>:<ACCESS_TOKEN_VALUE>@gitlab.com/<PRIVATE_ORG>/<PRIVATE_REPO>.git@<COMMIT_ID>",
    ...
],

但是在执行python setup.py install --record installed_files.txt 时,安装失败并显示以下消息:

Searching for mylibraryname@ git+https://<ACCESS_TOKEN_NAME>:<ACCESS_TOKEN_VALUE>@gitlab.com/<PRIVATE_ORG>/<PRIVATE_REPO>.git@<COMMIT_ID>
Reading https://pypi.org/simple/mylibraryname/
Couldn't find index page for 'mylibraryname' (maybe misspelled?)
Scanning index of all packages (this may take a while)
Reading https://pypi.org/simple/
No local packages or working download links found for mylibraryname@ git+https://<ACCESS_TOKEN_NAME>:<ACCESS_TOKEN_VALUE>@gitlab.com/<PRIVATE_ORG>/<PRIVATE_REPO>.git@<COMMIT_ID>
error: Could not find suitable distribution for Requirement.parse('mylibraryname@ git+https://<ACCESS_TOKEN_NAME>:<ACCESS_TOKEN_VALUE>@gitlab.com/<PRIVATE_ORG>/<PRIVATE_REPO>.git@<COMMIT_ID>')

所以我尝试使用pip install . 假设当前目录中有一个setup.py 文件,这有效:

Collecting mylibraryname@ git+https://<ACCESS_TOKEN_NAME>:<ACCESS_TOKEN_VALUE>@gitlab.com/<PRIVATE_ORG>/<PRIVATE_REPO>.git@<COMMIT_ID> from git+https://<ACCESS_TOKEN_NAME>:****@gitlab.com/<PRIVATE_ORG>/<PRIVATE_REPO>.git@<COMMIT_ID> (from <MY_LIBRARY_WITH_SETUP_PY>==<MY_LIBRARY_VERSION>)
  Cloning https://<ACCESS_TOKEN_NAME>:****@gitlab.com/<PRIVATE_ORG>/<PRIVATE_REPO>.git (to revision <COMMIT_ID>) to /tmp/pip-install-bakazwe2/mylibraryname
  Running command git clone -q https://<ACCESS_TOKEN_NAME>:sYzRKNsYAnv5GtS6zLZj@gitlab.com/<PRIVATE_ORG>/<PRIVATE_REPO>.git /tmp/pip-install-bakazwe2/mylibraryname

此解决方案似乎只有在包含setup.py 的目录中使用pip install . 时才有效。这不适用于python setup.py install --record installed_files.txt。

【问题讨论】:

    标签: python git dependencies gitlab setup.py


    【解决方案1】:

    https://python-packaging.readthedocs.io/ 已经过时了。它的来源是 2016 年 12 月 29 日的last updated,其中大部分内容自 2012 年以来没有更新。自那时以来,Python 打包格局发生了显着变化。新文档位于https://packaging.python.org/

    dependency_links 被宣布为过时,最后在pip 19.0 中宣布removed。它的替换是install_requires,具有特殊语法(自pip 19.1 起支持):

    install_requires=[
        'package_name @ git+https://gitlab.com/<PRIVATE_ORG>/<PRIVATE_REPO>.git@<COMMIT_ID>'
    ]
    

    见https://pip.readthedocs.io/en/stable/reference/pip_install/#requirement-specifiers和https://www.python.org/dev/peps/pep-0440/#direct-references

    这需要pip install,包括pip install .,不适用于python setup.py install。

    【讨论】:

    • 谢谢,仅在使用pip install . 时有效,我想接受这个答案,您能否请提及pip install . 和python setup.py install --record installed_files.txt 之间的警告,也许是为什么它在第二次失败时失败。再次感谢
    • 由于某种原因,当我将提交 ID 更新为新值时,pip 会显示 Requirement already satisfied。我必须先手动 pip uninstall -y package_name 以确保 pip 然后提取具有预期提交 ID 的版本。
    • 如果要指定master分支提交怎么办?
    • @LukAron 请更具体一点——你想指定master 分支(每次pip install 使用不同的提交)还是只有一个提交(与分支无关)?
    • @phd pip install 运行时主分支指向的提交
    【解决方案2】:

    我已经阅读了多个答案,但只有这一个对我有用(使用 pip 20.2.3 和 Gitlab Pypi 功能):

    pip3 install --extra-index-url https://__token__:my_personal_token@gitlab.com/api/v4/projects/347/packages/pypi/simple .
    

    我的setup.py 看起来像:

    from setuptools import setup
    
    setup(name='whatever_production_scripts',
          version='0.0.1',
          description='Whatever production scripts',
          url='https://gitlab.com/user/whatever',
          author='Me Myself',
          author_email='user@whatever.com',
          license='All rights reserved',
          scripts=[
              'whatever_production_scripts/production/insomnia.py',
              'whatever_production_scripts/production/rdsmaintenance.py',
              'whatever_production_scripts/production/changeinstancetype.py',
          ],
          packages=[
              'whatever_production_scripts',
              'whatever_production_scripts.production',
          ],
          classifiers=[
              "Development Status :: 3 - Alpha",
              "Intended Audience :: System Administrators",
              "Operating System :: POSIX :: Linux",
              "Topic :: Internet",
              "Topic :: System :: Systems Administration",
              "Programming Language :: Python :: 3 :: Only"
          ],
          install_requires=[
              'privatepackage1>=0.1',
              'publicpackage1>=7',
              'publicpackage2>=2'
          ],
          zip_safe=False)
    

    【讨论】:

      猜你喜欢
      • 2019-02-20
      • 1970-01-01
      • 1970-01-01
      • 2017-04-26
      • 1970-01-01
      • 2016-03-30
      • 2019-06-21
      • 2021-12-07
      • 1970-01-01
      相关资源
      最近更新 更多