繁体   English   中英

如何使用 python -m 构建将 fonts 与我的 python package 一起分发?

[英]How do I distribute fonts with my python package using python -m build?

问题陈述

我的 package 依赖 matplotlib 使用目标设备上可能未安装的特定字体。 在构建 package之后,我正在尝试将 ttf 字体文件从源分发安装到 matplotlib fonts/ttf目录。

随着setuptools慢慢删除其 CLI 的一部分( python setup.py sdist/bdist_wheel )和使用build的可能性( python -m build ),我决定用后者构建我的 package。

我试过的

我尝试从过时的https://stackoverflow.com/a/34304823/8797886修改setup.py 我的问题与这篇文章的不同之处在于我想使用python -m build

setup.py

import warnings
from functools import partial

from setuptools import setup
from setuptools.command.install import install

def _post_install():
    # Try to install custom fonts
    try:
        import os
        import shutil

        import matplotlib as mpl

        import mypkg

        # Find where matplotlib stores its True Type fonts
        mpl_data_dir = os.path.dirname(mpl.matplotlib_fname())
        mpl_ttf_dir = os.path.join(mpl_data_dir, "fonts", "ttf")

        # Copy the font files to matplotlib's True Type font directory
        # (I originally tried to move the font files instead of copy them,
        # but it did not seem to work, so I gave up.)
        cp_ttf_dir = os.path.join(os.path.dirname(mypkg.__file__), "ttf")
        for file_name in os.listdir(cp_ttf_dir):
            if file_name[-4:] == ".ttf":
                old_path = os.path.join(cp_ttf_dir, file_name)
                new_path = os.path.join(mpl_ttf_dir, file_name)
                shutil.move(old_path, new_path)
                print("moving" + old_path + " -> " + new_path)

        # Try to delete matplotlib's fontList cache
        mpl_cache_dir = mpl.get_cachedir()
        mpl_cache_dir_ls = os.listdir(mpl_cache_dir)
        if "fontList.cache" in mpl_cache_dir_ls:
            fontList_path = os.path.join(mpl_cache_dir, "fontList.cache")
            os.remove(fontList_path)
            print("Deleted the matplotlib fontList.cache")
    except:
        warnings.warn(
            "An issue occured while installing the custom fonts for mypkg."
        )
        raise

# Set up the machinery to install custom fonts.  Subclass the setup tools install
# class in order to run custom commands during installation.
class MoveTTF(install):
    def run(self):
        """
        Performs the usual install process and then copies the True Type fonts
        that come with shithappens into matplotlib's True Type font directory,
        and deletes the matplotlib fontList.cache.
        """
        # Perform the usual install process
        install.run(self)
        self.execute(_post_install, (), "Moving Custom Font to matplotlib.")


setup_movettf = partial(
    setup,
    package_data={"": ["ttf/*.ttf"]},
    cmdclass={"install": MoveTTF},
)

setup_movettf()

我使用了partial setup ,因为我希望build能够填充pyproject.toml中的所有其他内容。

pyproject.toml


[build-system]
requires = ["setuptools>=61.0", "matplotlib"]
build-backend = "setuptools.build_meta"

[project]
name = "mypkg"
etc...

[tool.setuptools.packages.find]
where = ["src"]

[tool.setuptools.package-data]
mypkg = ["ttf/*.ttf"]

我得到什么

当我使用python -m build时,我得到

ModuleNotFoundError: No module named 'mypkg'

当我使用python setup.py sdistpython setup.py bdist_wheel时,轮子已成功构建。 使用pip install dist/mypkg-[].whl安装 wheel 时,安装成功并且 matplotlib ttf 库填充了自定义 fonts。

我期待什么

我希望python -m build能够在安装后导入mypkg ,就像 setuptools cli 那样。

您不能使用 Python 打包工具执行此操作。 除此之外,您还需要 go。 一些特定于平台的打包工具。 想想 Debian/Ubuntu 上的.deb / apt或 Windows 上的完整可执行安装程序(或.msi ,如果它仍然存在)。

这是针对“安装时”的操作...

...但您不需要在“安装时”执行此操作,您的应用程序或库的代码可以包含在运行时执行此 fonts 安装的函数。

另一方面,如果您和您的用户只使用setup.py脚本,根本不使用pip (或任何其他现代安装程序),那么它可能仍然有效,因为显然您可以在setup.py程序中放入任何您想要的代码setup.py没有限制。

这些可能有更多细节:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM