【问题标题】:pip install using tarball archive from private gitlab repo using authenticationpip install using tarball archive from private gitlab repo using authentication
【发布时间】:2023-03-19 19:42:01
【问题描述】:

我正在尝试从压缩存档 URL 安装 python 模块,该压缩存档 URL 是 gitlab 私有 repo URL。但它抛出错误:

pip install https://gitlab.com/<myprivate_repo_path>/-/archive/main/private-module.tar.gz

Collecting https://gitlab.com/<myprivate_repo_path>/-/archive/main/private-module.tar.gz
ERROR: Could not install packages due to an OSError: HTTPSConnectionPool(host='gitlab.com', port=443): Max retries exceeded with url: /users/sign_in (Caused by ResponseError('too many 503 error responses'))

虽然从公共仓库的压缩存档安装工作正常:

pip install https://gitlab.com/pycqa/flake8/-/archive/3.7.7/flake8-3.7.7.tar.gz
Collecting https://gitlab.com/pycqa/flake8/-/archive/3.7.7/flake8-3.7.7.tar.gz
  Downloading https://gitlab.com/pycqa/flake8/-/archive/3.7.7/flake8-3.7.7.tar.gz
     | 153 kB 328 kB/s
Collecting entrypoints<0.4.0,>=0.3.0
  Downloading entrypoints-0.3-py2.py3-none-any.whl (11 kB)
Collecting pyflakes<2.2.0,>=2.1.0
  Downloading pyflakes-2.1.1-py2.py3-none-any.whl (59 kB)
     |████████████████████████████████| 59 kB 476 kB/s 
Collecting pycodestyle<2.6.0,>=2.5.0
  Downloading pycodestyle-2.5.0-py2.py3-none-any.whl (51 kB)
     |████████████████████████████████| 51 kB 782 kB/s 
Collecting mccabe<0.7.0,>=0.6.0
  Downloading mccabe-0.6.1-py2.py3-none-any.whl (8.6 kB)
Using legacy 'setup.py install' for flake8, since package 'wheel' is not installed.
Installing collected packages: pyflakes, pycodestyle, mccabe, entrypoints, flake8
    Running setup.py install for flake8 ... done
Successfully installed entrypoints-0.3 flake8-3.7.7 mccabe-0.6.1 pycodestyle-2.5.0 pyflakes-2.1.1

我有什么办法仍然可以通过提供压缩存档 URL 从私人仓库 pip install 吗?

我已经尝试过了:

  1. 通过关注this URL 在 GitLab 中创建了一个令牌
  2. 在 URL 中使用了该令牌:
    pip install https://<user>:<pass>@gitlab.com/<myprivate_repo_path>/-/archive/main/private-module.tar.gz
    

但它导致了同样的错误:

ERROR: Could not install packages due to an OSError: HTTPSConnectionPool(host='gitlab.com', port=443): Max retries exceeded with url: /users/sign_in (Caused by ResponseError('too many 503 error responses'))

注意:

我不是在寻找类似下面的东西, 因为它增加了安装 git 的额外依赖项(尤其是在使用 docker 时)+ 我的要求是从压缩存档中安装模块。

pip install git+https://<user>:<pass>@gitlab.com/<myprivate_repo_path>/private-module.git

【问题讨论】:

    标签: pip gitlab


    【解决方案1】:

    向 GitLab 进行身份验证

    您最初的问题是您无法使用用户名和密码进行身份验证,使用基本身份验证来获取私有存储库的 UI 中显示的获取存档。 GitLab 不支持这样的基本身份验证。

    相反,您应该使用 GitLab API 并使用 API 令牌进行身份验证。您可以使用repository file archive API 通过 API 获取相同的 tarball 存档,这将支持正确的身份验证。

    以 curl 为例

    curl -o mypackage.tar.gz --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/<project_id>/repository/archive"
    pip install ./mypackage.tar.gz
    rm ./mypackage.tar.gz
    

    使用 GitLab PyPI 存储库

    PyPI repositories 也是 GitLab 在所有层级中支持的功能。您可以利用该功能,只需将regular authentication methods for PyPI 与您的令牌一起使用,即可从私有索引中提取包。

    避免 Docker 中不需要的依赖项

    它增加了安装 git 的额外依赖(尤其是在使用 docker 时)

    作为附加说明,因为您提到了 docker...如果您使用的是 docker,您可以轻松地使用多阶段构建来避免构建映像中的依赖问题。

    FROM python:3.9-slim as build
    
    # install build-time dependencies
    RUN apt update && apt install -y git
    RUN pip install --upgrade wheel pip
    
    # fetch and build dependencies to wheels
    ARG USERNAME
    ARG PASSWORD
    WORKDIR /opt/build
    RUN pip wheel -w wheelhouse "git+https://${USERNAME}:${PASSWORD}@gitlab.com/path/mypackage/repo.git"
    
    FROM python:3.9-slim as final
    # copy pre-built packages from build stage
    COPY --from=build /opt/build/wheelhouse /wheelhouse
    # install the package from wheels
    RUN pip install --no-index --find-links /wheelhouse mypackage
    

    这样,您只在第一阶段安装 git,但它不会出现在最终构建的映像中。可以做更多的工作来优化这个多阶段构建,但这只是对如何避免您描述的依赖问题的简要说明。

    【讨论】:

    • 我猜你是对的。 pip 中缺少传递 auth 标头以下载软件包功能。我之前可能没有提到它,但我想将存档 URL 放在 requirements.txt 中。当我使用 CURL 时我无法实现。例如,如果 URL 来自公共存储库,只需在 requirement.txt 中添加这一行即可安装模块:https://gitlab.com/pycqa/flake8/-/archive/3.7.7/flake8-3.7.7.tar.gz
    • @SauravKumar 在标题上,是的,它是 not available in pip 但如果您正在寻找纯 Python 解决方案,您仍然可以使用 urllib.request 使用适当的标题下载压缩包,然后只需在下载的文件上调用pip(类似于使用 curl 的答案中的步骤)。您甚至可以围绕pip 进行包装以使其无缝。关于requirements.txt 的问题——pip 支持环境变量替换以及安装需求文件中指定的本地包(和tarball)。
    • PyPI repositories 也是 GitLab 在所有层级中受支持的功能...您可以利用该功能,只需对私有索引使用常规身份验证方法。所以该选项也可用。
    • 是的,gitlab 支持包注册,这可以通过在requirements.txt 中添加类似这样的行来实现:--index-url https://&lt;user&gt;:&lt;token&gt;@gitlab.com/api/v4/projects/&lt;project_id&gt;/packages/pypi/simple。但它要求所有开发者都按照 gitlab 的说明在本地进行设置,他们正在推送到 gitlab 包注册。对我来说似乎有点极端。我感觉 GitLab 可能允许通过在 URL 路径(如https://&lt;user&gt;:&lt;token&gt;@gitlab.com/modulearchivepath)或查询参数?token=&lt;token&gt; 中传递身份验证来下载文件,但不确定如何。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-16
    • 2022-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-02
    相关资源
    最近更新 更多