Python 安装时包不包含静态内容

Python Package Not Including Static Content When Installed

我正在编写一个 Python 包来创建架构图。作为该过程的一部分,我需要能够将静态内容(即图像)作为我的包分发的一部分。为此,我按照文档添加了一个包含我所有图标的 MANIFEST.in 文件,并更新了 setup.py 以包含 include_package_data=True 属性。

基于此配置,根据对 运行 [=] 时生成的 SOURCES.txt 文件的检查,发布包时静态内容确实被推送到 PyPi 22=]。包含静态内容的上传时间也明显更长。

问题是,当我 运行 pip3 install architectures 包确实安装时,但静态内容的 none 却安装了。在查看随包下载的 RECORD 文件后,我对此相当有信心,该文件不包含对我的图像(图标)的任何引用。

可以在下面找到相关代码片段。非常感谢任何帮助。

简化文件夹结构

.
├── setup.py
├── MANIFEST.in
├── README.md
├── icons
│   └── azure
│       └── ai
│           ├── cognitive-services.png
│           ├── ...
└── architectures
    ├── core
    │   └── __init__.py
    ├── themes
    │   └── __init__.py
    ├── providers
    │   ├── azure
    │   └── __init__.py
    └── __init__.py

为了完整查看,这里是包的 link 到 source code。还有其他提供程序,例如 aws 和 gcp,但为了简单起见,只包含一个 azure 资源示例。

setup.py

import setuptools

with open("README.md", "r", encoding="utf-8") as fh:
    long_description = fh.read()

setuptools.setup(
    name="architectures",
    version=version,
    author="Justin O'Connor",
    author_email="jsoconno@gmail.com",
    description="Tools for creating architecture as code using Python.",
    long_description=long_description,
    long_description_content_type="text/markdown",
    url="https://github.com/jsoconno/architectures",
    packages=setuptools.find_packages(),
    include_package_data=True,
    classifiers=[
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
    ],
    python_requires='>=3.7',
)

MANIFEST.in

recursive-include icons *

简化了 SOURCES.txt 来自 architectures.egg-info

LICENSE
MANIFEST.in
README.md
setup.py
architectures/__init__.py
architectures.egg-info/PKG-INFO
architectures.egg-info/SOURCES.txt
architectures.egg-info/dependency_links.txt
architectures.egg-info/top_level.txt
architectures/core/__init__.py
architectures/providers/__init__.py
architectures/providers/aws/__init__.py
...
architectures/themes/__init__.py
architectures/themes/settings/__init__.py
icons/aws/analytics/analytics.png
icons/aws/analytics/athena.png
icons/aws/analytics/cloudsearch-search-documents.png
icons/aws/analytics/cloudsearch.png
icons/aws/analytics/data-pipeline.png
...

此文件的其余部分只是对更多图标和更多提供商的引用。如您所见,引用了静态内容。如果 setup.py 中没有 MANIFEST.ininclude_package_data=True,则不包括在内。

pip 安装包后获得的简化 RECORD 文件

architectures-0.2.6.dist-info/INSTALLER,sha256=zuuue4knoyJ-UwPPXg8fezS7VCrXJQrAP7zeNuwvFQg,4
architectures-0.2.6.dist-info/LICENSE,sha256=HkGqWUVTTaMop6P60XETwa3m-6YB6e9dj-7Z1qvVcPc,1072
architectures-0.2.6.dist-info/METADATA,sha256=q1B9AHheDrIEFl9bHsE4SxzhmN87RyK5EHtIDkqDWAk,21202
architectures-0.2.6.dist-info/RECORD,,
architectures-0.2.6.dist-info/REQUESTED,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
architectures-0.2.6.dist-info/WHEEL,sha256=OqRkF0eY5GHssMorFjlbTIq072vpHpF60fIQA6lS9xA,92
architectures-0.2.6.dist-info/top_level.txt,sha256=Ry8P57aco2wTEkX7F8DbF1AEiCIwLuhQk3DSXj5ZTjk,14
architectures/*
architectures/core/*
architectures/providers/*
architectures/themes/*

如果下载了静态内容,我还希望在同一个文件中看到类似以下的内容:

icons/azure/ai/cognitive-services.png,sha256=...

提前感谢您的任何见解。我看过许多 Whosebug 帖子和 Google 搜索,但当您将包发布到 PiPy 时,一切似乎都与包含静态内容有关,而不是当您 运行 pip install package-name.

如果有人感兴趣,这里是我在 Mac:

上测试的一般步骤
  1. 使用 cd desktop
  2. 进入桌面
  3. 创建一个新文件夹,内容类似于 mkdir test
  4. 进入那个文件夹cd test
  5. 创建虚拟环境`python3 -m venv env
  6. 激活环境source env/bin/activate
  7. 安装包pip3 install architectures graphviz
  8. 为图表创建一个文件touch architecture.py
  9. 添加如下内容:
from architectures.core import *
from architectures.themes import *
from architectures.providers.azure.ai import CognitiveServices

theme = DarkMode()

with Graph("Test", theme=theme):
    CognitiveServices()

结果是创建了图表,但没有显示图标。

我可以通过将 __init__.py 文件添加到所有图标 png 文件所在的文件夹来解决此问题。这样做之后,pip install architectures 包含了生成架构图所需的所有静态内容。似乎没有这个,分发将不包含这些文件,因为它们位于不在我的包文件夹下的文件夹中。

如前所述,这只有效,因为我在 setup.py 文件中也有 MANIFEST.in 文件和 include_package_data=True 属性。

如果我决定采取将所有内容都包含在单个 src 文件夹中的方式,这可能是可以避免的,因为在研究如何打包 Python 代码以供分发时经常引用。