如何将图像添加到 Python 包索引文档中?

d3p*_*3pd 7 python pypi setup.py

我有一个模块存储库,其中包含模块代码、README.md 文件和存储在目录中的 README.md 文件中使用的图像images/(使用相对链接链接到 README.md 中)。为了注册和上传模块到 PyPI,我有文件setup.pyMANIFEST.in. 事情应该如何使得图像包含并出现在 PyPI 在线文档中(就像出现在假设页面https://pypi.python.org/pypi/junkmodule 中一样)?

我目前拥有的MANIFEST.insetup.py(不包括 PyPI 在线文档中的图像)如下:

清单文件

include LICENSE
include README.md
include images/*
include setup.py
include junkmodule.py
Run Code Online (Sandbox Code Playgroud)

设置文件

#!/usr/bin/python
# -*- coding: utf-8 -*-

import os
import setuptools

def main():

    setuptools.setup(
        name             = "junkmodule",
        version          = "0.0.0.1",
        description      = "provides nothing much",
        long_description = Markdown_to_reStructuredText("README.md"),
        url              = "https://github.com/junkuser1/junkmodule",
        author           = "L. Ron. Hubbard",
        author_email     = "lrh@sern.ch",
        license          = "GPLv3",
        py_modules       = ["junkmodule"],
        entry_points     = """
            [console_scripts]
            junkmodule = junkmodule:junkmodule
        """
    )

def read(*paths):
    with open(os.path.join(*paths), "r") as filename:
        return filename.read()

def Markdown_to_reStructuredText(filename):
    try:
        import pypandoc
        return pypandoc.convert(filename, "rst")
    except:
        print("pypandoc not found; long description could be corrupted")
        return read(filename)

if __name__ == "__main__":
    main()
Run Code Online (Sandbox Code Playgroud)

Mar*_*uts 2

一种可能的解决方案是将图像托管在 GitHub 上,然后将图像及其显式 URL 包含在 README 中。

以这个自述文件为例:徽标包含在

![](https://raw.githubusercontent.com/mwouts/jupytext/master/docs/logo.png)
Run Code Online (Sandbox Code Playgroud)

并且该徽标确实出现在pip上。

请注意,您甚至不需要将图像托管在同一个 GitHub 存储库中 - 您只需要一个工作 URL(另一个存储库中的图像,或者 git 也可以)。