ast*_*nlu 5 python packaging matplotlib
我想分发一个自定义的matplotlib样式表,但是现在我能想到的唯一方法是将它上传到Gist或其他一些网站并告诉我的用户手动将其下载到某个配置目录.
有没有办法分发样式表,就好像它是一个Python包,或者作为模块的一部分?容易的事pip install mpl_fancy.
在阅读了 @aloctavodia 的链接并查阅了Massmutual/mmviz-python GitHub 存储库之后,这就是我的想法。
setup.py
from setuptools import setup
from setuptools.command.install import install
import os
import shutil
import atexit
import matplotlib
def install_mplstyle():
stylefile = "mystyle.mplstyle"
mpl_stylelib_dir = os.path.join(matplotlib.get_configdir() ,"stylelib")
if not os.path.exists(mpl_stylelib_dir):
os.makedirs(mpl_stylelib_dir)
print("Installing style into", mpl_stylelib_dir)
shutil.copy(
os.path.join(os.path.dirname(__file__), stylefile),
os.path.join(mpl_stylelib_dir, stylefile))
class PostInstallMoveFile(install):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
atexit.register(install_mplstyle)
setup(
name='my-style',
version='0.1.0',
py_modules=['my_style'],
install_requires=[
'matplotlib',
],
cmdclass={
'install': PostInstallMoveFile,
}
)
Run Code Online (Sandbox Code Playgroud)
我只是放入my_style.py了一个基本的示例脚本。现在,我的用户可以安装这种风格pip install git+https://github.com/me/my-style!