Str*_*tch 5 python distutils matplotlib setuptools
我创建了一个名为clearplot的包,它环绕着 matplotlib。我还创建了一个不错的字体,我想与我的包一起分发。我查阅了 Python Packaging User guide 的这一部分,并决定我应该使用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)
Run Code Online (Sandbox Code Playgroud)
这有两个问题setup.py
:
setup()
有机会安装它之前导入 matplotlib 。这是一个明显的嘘声,但我需要mpl_ttf_dir
在我跑之前知道在哪里setup()
。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}
)
Run Code Online (Sandbox Code Playgroud)
这解决了问题#1(它在导入 matplotlib 之前安装它)和问题#2(它与轮子一起使用)。
归档时间: |
|
查看次数: |
1567 次 |
最近记录: |