使用额外的Python包索引URL和setup.py

63 投票
8 回答
60728 浏览
提问于 2025-04-18 11:17

有没有办法在使用 setup.py 的时候,添加一个额外的 Python 包索引(就像 pip --extra-index-url pypi.example.org mypackage 这样),让运行 python setup.py install 时也能找到在 pypi.example.org 上托管的包呢?

8 个回答

3

在使用Dockerfile时找到了一个解决方案:

RUN cd flask-mongoengine-0.9.5 && \
    /bin/echo -e [easy_install]\\nindex-url = https://pypi.tuna.tsinghua.edu.cn/simple >> setup.cfg && \
    python setup.py install

这个 /bin/echo -e [easy_install]\\nindex-url = https://pypi.tuna.tsinghua.edu.cn/simple 会出现在文件 setup.cfg 中:

[easy_install]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
4

以下方法对我有效(用于开发,而不是安装):

$ python setup.py develop --index-url https://x.com/n/r/pypi-proxy/simple

这里的 https://x.com/n/r/pypi-proxy/simple 是一个本地的PyPI仓库。

7

我想更新一下这个问题的答案,因为之前的两个答案已经过时了;easy_install这个工具已经被setuptools淘汰了。

https://setuptools.pypa.io/en/latest/deprecated/easy_install.html

Easy Install已经不再使用了。不要再用它了。现在应该使用pip。如果你觉得自己需要Easy Install,请联系PyPA团队(给pip或setuptools提交个问题都可以),告诉他们你的使用情况。

请今后使用pip。你可以选择以下几种方式:

  1. pip命令中添加--index-url参数
  2. pip.conf文件中定义index-url
  3. 设置环境变量PIP_INDEX_URL

https://pip.pypa.io/en/stable/topics/configuration/

17

setuptools 在后台使用 easy_install

它依赖于 setup.cfg 或者 ~/.pydistutils.cfg 这两个文件,具体的说明可以在 这里 找到。

你可以在这两个文件中定义额外的 packages 路径,使用 find_links。你可以用 index_url 来覆盖注册表的地址,但不能提供 extra-index-url。下面的例子是根据文档灵感而来的:

[easy_install]
find_links = http://mypackages.example.com/somedir/
             http://turbogears.org/download/
             http://peak.telecommunity.com/dist/
index-url = https://mypi.example.com
60

如果你是一个软件包的维护者,并且想把你的软件包所依赖的其他软件包放在除了PyPi以外的地方,你可以在你的软件包的 setup.py 文件中使用 dependency_links 这个选项。这样,你就可以明确告诉别人你的软件包在哪里可以找到。

举个例子:

from setuptools import setup

setup(
    name='somepackage',
    install_requires=[
        'somedep'
    ],
    dependency_links=[
        'https://pypi.example.org/pypi/somedep/'
    ]
    # ...
)

如果你自己搭建了一个索引服务器,你需要提供实际下载链接的页面地址,而不是列出所有软件包的页面地址(比如说,应该是 https://pypi.example.org/pypi/somedep/,而不是 https://pypi.example.org/

撰写回答