如何在托管在Nexus上的私有的PyPI repo中指定依赖项? [英] Python setuptools: How do I specify dependencies in a private PyPI repo, hosted on Nexus?

查看:25
本文介绍了如何在托管在Nexus上的私有的PyPI repo中指定依赖项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在私有的PyPI存储库(托管在Sonatype Nexus存储库管理器,Voss 3.17.0-01上)中的包上生成依赖于包的python轮子。

如果我使用pip,我可以搜索并安装该程序包;我的问题是尝试让setup.py执行相同的操作。从各种命令的输出来看,我认为问题可能是由于存储库提供的包链接中的相对路径造成的。

搜索类似的问题,其中大多数与私有git存储库有关。我能找到的看起来最相关的问题是Equivalent for `--find-links` in `setup.py`。评论正文指出
在setuptools上下文中,dependency_links选项接受...包含直接下载链接的网页的URL

但是,提供链接页面是为了支持报价不再包含该文本(是否被较新版本替换?)

所以这可能就是问题所在--而且我尝试做事情的方式不受支持。如果是这样的话,有没有人能建议一种可行的方法?

或者,这可能是安装工具中的错误,或者我们Nexus的错误配置问题-如果有人可以证实或驳斥这些理论-并再次建议一种可行的方法-我将不胜感激。

以下是我的设置和各种命令的输出,显示了哪些命令有效,哪些命令无效:

  1. 设置环境:

    mkdir depends-fail
    cd depends-fail
    python3 -m venv venv
    source activate venv/bin/activate
    pip install --upgrade pip
    pip install wheel        # So we can use bdist_wheel build option.
    
  2. 确认当前PIP版本:

    (venv) $ pip --version
    pip 21.0.1 from /tmp/depends-fail/venv/lib/python3.6/site-packages/pip (python 3.6)
    
  3. 确认setupTools版本:

    (venv) $ python -c "import setuptools as s; print(s.version.__version__)"
    39.0.1
    
  4. depends-fail目录中,创建这个最小的setup.py文件,它将演示问题:

     from setuptools import setup
     setup(
       name='failed-dependencies',
       install_requires=['data-feed-ping>=0.5'],
       dependency_links=[
         'http://nexus.example.local/nexus/repository/pypi-playground-public/simple/data-feed-ping',
       ]
    )
    
  5. 确认我们的依赖项在我们的专用存储库中可用:

     (venv) $ pip search --trusted-host nexus.example.local 
     >  -i http://nexus.example.local/nexus/repository/pypi-playground-public/pypi 
     >  data-feed-ping
    

    响应确认包在本地可用:

    ⋮
    Starting new HTTP connection (1): nexus.example.local:80
    http://nexus.example.local:80 "POST /nexus/repository/pypi-playground-public/pypi HTTP/1.1" 200 239
    data-feed-ping (0.5)  - Determine response times of data feeds.
    
  6. 以下是我们尝试构建和测试最小程序包(它是触发依赖项下载的测试选项)时发生的情况:

    (venv) $ python setup.py bdist_wheel test
    

    包生成时没有问题-但找不到依赖项:

        running bdist_wheel
        running build
        ⋮
        removing build/bdist.linux-x86_64/wheel
        running test
        Searching for data-feed-ping>=0.5
    (1) Reading http://nexus.example.local/nexus/repository/pypi-playground-public/simple/data-feed-ping
    (2) Downloading http://nexus.example.local/nexus/repository/packages/data-feed-ping/0.5/data_feed_ping-0.5-py3-none-any.whl#md5=e7a9ee0be6cc77165d02e7022c04b336
        error: Can't download http://nexus.example.local/nexus/repository/packages/data-feed-ping/0.5/data_feed_ping-0.5-py3-none-any.whl#md5=e7a9ee0be6cc77165d02e7022c04b336: 
        404 Repository not found
    
  7. (2)中的下载链接和版本散列来自在(1)处读取的索引URL的内容-让我们看看该文件是什么样子:

    (venv) $ curl http://nexus.example.local/nexus/repository/pypi-playground-public/simple/data-feed-ping
    
    <html lang="en">
    <head><title>Links for data-feed-ping</title><meta name="api-version" value="2"/></head>
      <body><h1>Links for data-feed-ping</h1>
        ⋮  <!-- links to previous versions -->
        <a href="../../packages/data-feed-ping/0.5/data_feed_ping-0.5-py3-none-any.whl#md5=e7a9ee0be6cc77165d02e7022c04b336" rel="internal">data_feed_ping-0.5-py3-none-any.whl</a><br/>
      </body>
    </html>
    

索引URL确实提供了到所需版本的数据馈送ping包的链接,并且安装脚本正确地从该链接获取了MD5摘要(请参见脚本输出中的(2))。但是,安装脚本随后会尝试从无效的URL下载该文件。

在我看来,问题似乎是索引链接提供的相对路径。如果我们从索引URL(1)开始:

http://nexus.example.local/nexus/repository/pypi-playground-public/simple/data-feed-ping

并添加下载的相对路径:

../../packages/data-feed-ping/0.5/data_feed_ping-0.5-py3-none-any.whl#md5=e7a9ee0be6cc77165d02e7022c04b336

我得到以下绝对路径:

http://nexus.example.local/nexus/repository/pypi-playground-public/packages/data-feed-ping/0.5/data_feed_ping-0.5-py3-none-any.whl#md5=e7a9ee0be6cc77165d02e7022c04b336

我已确认该绝对路径指向我想要的包:

(venv) $ wget http://nexus.example.local/nexus/repository/pypi-playground-public/packages/data-feed-ping/0.5/data_feed_ping-0.5-py3-none-any.whl#md5=e7a9ee0be6cc77165d02e7022c04b336

--2021-04-01 16:44:18--  http://nexus.example.local/nexus/repository/pypi-playground-public/packages/data-feed-ping/0.5/data_feed_ping-0.5-py3-none-any.whl
Resolving nexus.example.local (nexus.example.local)... 192.168.24.136
Connecting to nexus.example.local (nexus.example.local)|192.168.24.136|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 19066 (19K) [application/zip]
Saving to: ‘data_feed_ping-0.5-py3-none-any.whl’
data_feed_ping-0.5-py3-none-any 100%[==========================================>]  18.62K  --.-KB/s    in 0s
2021-04-01 16:44:18 (76.3 MB/s) - ‘data_feed_ping-0.5-py3-none-any.whl’ saved [19066/19066]

如果我尝试使用pip直接下载包,并指定安装脚本使用的相同索引URL:

,我也没有问题
   (venv) $ pip install --trusted-host nexus.example.local 
   >  --index-url http://nexus.example.local/nexus/repository/pypi-playground-public/simple 
   >  data-feed-ping

   Looking in indexes: http://nexus.example.local/nexus/repository/pypi-playground-public/simple
Collecting data-feed-ping
   Downloading http://nexus.example.local/nexus/repository/pypi-playground-public/packages/data-feed-ping/0.5/data_feed_ping-0.5-py3-none-any.whl (19 kB)
   Requirement already satisfied: requests in ./venv/lib/python3.6/site-packages (from data-feed-ping) (2.25.1)
   ⋮
   Installing collected packages: data-feed-ping
   Successfully installed data-feed-ping-0.5

推荐答案

您应该只使用pip(或任何其他安装程序)来安装Python项目。例如,通过调用python setup.py install进行安装是一种过时的做法(通常不起作用)。因此,您可能应该删除setup.pydependency_links参数,并指示您的用户使用正确的pip标志进行安装,正如您在问题末尾所示:

python -m pip install --trusted-host nexus.example.local 
   >  --index-url http://nexus.example.local/nexus/repository/pypi-playground-public/simple 
   >  data-feed-ping

以同样的方式python setup.py test:也是过时的/不推荐的,您可能会被很好地建议转移到更现代的测试运行程序(我想到的是pytest,但可能还有其他的,可能是Nose)。

这篇关于如何在托管在Nexus上的私有的PyPI repo中指定依赖项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆