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

独自空忆成欢 提交于 2020-05-09 06:59:06

问题


I am trying to install a private dependency (not something that Python could find on PyPI).

I have added to the file setup.py this (as explained here: 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>'
      ]

On that official documentation they don't really explain in details what's the format of that URL, however using a <COMMIT_ID after the @ sounds reasonable (as it's done in a variety of other languages and dependency management tools).

When executing the command python setup.py install then I see in the logs/output this:

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

but then I don't see that package being actually installed as I can see from the logs/output for other dependencies.

I know there is a valid GitLab access token setup for my git command, because I've run this:

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

and I can see it when checking the git config with:

git config --list | grep gitlab
url.https://<ACCESS_TOKEN_NAME>:<ACCESS_TOKEN_VALUE>@gitlab.com.insteadof=https://gitlab.com
  • Is Python using the git command when running setup.py?
  • How can I specify a private GitLab dependency inside a Python setup.py file? It should be based on a commit ID rather than a package version
  • What's wrong with the above?
  • I have also the feeling this may be running differently when using pip install and targeting setup.py instead of running python setup.py install, is there a unique way of making this work with both flavors of Python installations? I am asking this because when fiddling with dependency_links I was trying various things like git+ssh instead of https and other variations, all of them failing to install that private repository with various log/outputs saying that repository was not found.

Edit

I've avoided dependency_links because it seems deprecated, so I used the solution proposed in the answer as:

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

However when executing python setup.py install --record installed_files.txt, then the installation fails with this message:

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>')

So I tried to use pip install . assuming there is a setup.py file in the current directory, this worked:

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

This solution seems to work only when using pip install . in a directory containing setup.py. This does not work with python setup.py install --record installed_files.txt.


回答1:


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 were declared obsolete and finally removed in pip 19.0. The replacement for it is install_requires with special syntax (supported since pip 19.1):

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

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

This requires pip install including pip install . and doesn't work with python setup.py install.



来源:https://stackoverflow.com/questions/59109081/python-setup-py-with-private-repository-on-gitlab-as-dependency-links-based-on-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!