如何使用python软件包分发字体?

我创建了一个名为clearplot的程序包,该程序包围绕着matplotlib.我还创建了一个不错的字体,希望随包一起分发.我查阅了《 Python打包用户指南》中的this section,并确定我应该使用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存在两个问题:

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

问题2让我非常恼火,但是从那以后,我发现绝对路径很糟糕,因为它们不能与virtualenv一起使用.因此,我现在愿意改变自己的方法,但是我该怎么办?

我唯一的想法是先将字体作为package_data分发,然后再使用os模块将字体移动到正确的位置.那是洁净方法?

解决方法:

感谢@benjaoming的回答和this blog post,这是我想出的:

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 Control Installation


下一篇:安装selenium教程