Python setup.py GitLab 上的私有存储库为 dependency_links 基于提交 ID

Python setup.py with private repository on GitLab as dependency_links based on commit ID

我正在尝试安装私有依赖项(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 的格式是什么,但是在 @ 之后使用 <COMMIT_ID 听起来很合理(因为它在各种其他语言和依赖管理工具)。

当执行命令 python setup.py install 然后我在 logs/output 中看到这个:

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

但是我没有看到那个包被实际安装,正如我从 logs/output 看到的其他依赖项一样。

我知道我的 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

编辑

我避免使用 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.

https://python-packaging.readthedocs.io/ is quite old and outdated. Its sources was last updated at Dec 29, 2016 and most parts of it were not updated since 2012. Python packaging landscape changed significantly since that times. The new docs are at https://packaging.python.org/

dependency_links 被宣布为过时,最终 removedpip 19.0 中。它的替代品是具有特殊语法的 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 and https://www.python.org/dev/peps/pep-0440/#direct-references

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

我已经阅读了多个答案,但只有这个对我有用(使用 pip 20.2.3Gitlab 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)