繁体   English   中英

如何使用我的python包分发字体?

[英]How do I distribute fonts with my python package?

我创建了一个名为clearplot的包,它包裹了matplotlib。 我还创建了一个很好的字体,我想用我的包分发。 我查阅了Python包装用户指南的这一部分 ,并确定我应该使用data_files关键字。 我选择了data_files而不是package_data因为我需要在我的包之外的matplotlib目录中安装该字体。

这是我在setup.py文件中的第一个有缺陷的尝试:

from distutils.core import setup
import os, sys
import matplotlib as mpl

#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')

setup(
    ...(edited for brevity)...
    install_requires = ['matplotlib >= 1.4.0, !=1.4.3', 'numpy >= 1.6'],
    data_files = [
        (mpl_ttf_dir, ['./font_files/TeXGyreHeros-txfonts/TeXGyreHerosTXfonts-Regular.ttf']),
        (mpl_ttf_dir, ['./font_files/TeXGyreHeros-txfonts/TeXGyreHerosTXfonts-Italic.ttf'])]
)

#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)

这个setup.py有两个问题:

  1. 我尝试在setup()有机会安装它之前导入matplotlib。 这是一个显而易见的booboo,但在运行setup()之前我需要知道mpl_ttf_dir
  2. 如前所述这里 ,车轮分布不支持绝对路径data_files 我不认为这会是一个问题,因为我以为我会使用sdist发行版。 (sdists确实允许绝对路径。)然后我发现pip 7.0(及更高版本)将所有包转换为wheel分布,即使分发最初是作为sdist创建的。

我对#2问题感到非常恼火,但从那时起,我发现绝对路径很糟糕,因为它们不适用于virtualenv。 因此,我现在愿意改变我的方法,但我该怎么办?

我唯一的想法是首先将字体分发为package_data ,然后使用os模块将字体移动到正确的位置。 这是犹太教法吗?

感谢@ benjaoming的回答和这篇博文 ,这是我想出的:

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

#Set up the machinery to install custom fonts.  Subclass the setup tools install 
#class in order to run custom commands during installation.  
class move_ttf(install):
    def run(self):
        """
        Performs the usual install process and then copies the True Type fonts 
        that come with clearplot into matplotlib's True Type font directory, 
        and deletes the matplotlib fontList.cache 
        """
        #Perform the usual install process
        install.run(self)
        #Try to install custom fonts
        try:
            import os, shutil
            import matplotlib as mpl
            import clearplot as cp

            #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(cp.__file__), 'true_type_fonts')
            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.copyfile(old_path, new_path)
                    print "Copying " + 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("WARNING: An issue occured while installing the custom fonts for clearplot.")

setup(...
    #Specify the dependencies and versions
    install_requires = ['matplotlib >= 1.4.0, !=1.4.3', 'numpy >= 1.6'],
    #Specify any non-python files to be distributed with the package
    package_data = {'' : ['color_maps/*.csv', 'true_type_fonts/*.ttf']},
    #Specify the custom install class
    cmdclass={'install' : move_ttf}
)

这解决了问题#1(它在导入之前安装matplotlib)和问题#2(它适用于轮子)。

我唯一的想法是首先将字体分发为package_data,然后使用os模块将字体移动到正确的位置。 这是犹太教法吗?

我会考虑做到这一点。 我知道你的软件包可能不是virtualenvs的明显候选者,但考虑到python软件包可能只安装在用户可写的位置。 因此,在第一次运行程序并检测到正确位置时复制字体可能会提示您以比可能更好的方式执行操作setup.py,例如:通过密码提示提升权限以备不时之需,请求如果您未能检测到它,则会显示其他位置,如果您覆盖现有系统文件等,则会提示您。

我曾经尝试过争论Python包应该能够在/etc放置东西,但我意识到与为目标操作系统创建适当的本机包相比,它的好处很小,例如Debian的debian包或Windows的.exe安装程序。

最重要的是,wheel和setuptools不是整个操作系统的包管理器,而是一些本地site-packages/

我希望这个答案为你提供足够的背景来避免data_files 最后一个很好的理由:让它在distutils,setuptools和wheel上运行是不行的。

暂无
暂无

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

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