在安装过程中复制配置文件的正确方法?

问题描述 投票:1回答:1

我正在尝试分发我写的mplstyle,以便我可以轻松分享。归结为在安装过程中将文本文件复制到正确的配置方向(任何架构都已知)。我希望能够使用python setup.py installpip install ...安装。目前我似乎没有获得两种方式中的任何一种(见下面的当前方法)。

  • 使用pip install ...安装似乎根本不会调用复制。
  • 使用python setup.py install安装在我的机器上运行良好,但ReadTheDocs给出了以下错误: python setup.py install --force running install error: [Errno 2] No such file or directory: u'/home/docs/.config/matplotlib/stylelib/goose.mplsty

在安装过程中以健壮的方式复制配置文件的正确方法是什么?

Current approach

文件结构

setup.py
goosempl/
| __init__.py
| stylelib/
  | goose.mplstyle
  | ...

setup.py

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

class PostInstallCommand(install):

  def run(self):

    import goosempl
    goosempl.copy_style()

    install.run(self)

setup(
  name              = 'goosempl',
  ...,
  install_requires  = ['matplotlib>=2.0.0'],
  packages          = ['goosempl'],
  cmdclass          = {'install': PostInstallCommand},
  package_data      = {'goosempl/stylelib':['goosempl/stylelib/goose.mplstyle']},
)

goosempl/__init__.py

def copy_style():

  import os
  import matplotlib

  from pkg_resources import resource_string

  files = [
    'stylelib/goose.mplstyle',
  ]

  for fname in files:
    path = os.path.join(matplotlib.get_configdir(),fname)
    text = resource_string(__name__,fname).decode()

    print(path, text)

    open(path,'w').write(text)

上传到PyPi

python setup.py bdist_wheel --universal
twine upload dist/*
python matplotlib setuptools
1个回答
1
投票

首先,根据您提供的项目结构,您没有正确指定package_data。如果goosempl是一个包,stylelib里面有一个包含mplstyle文件的目录(我从你的代码中假设),那么你的package_data配置行应该是:

package_data = {'goosempl': ['stylelib/goose.mplstyle']},

Building and Distributing Packages with Setuptools所述:

package_data参数是一个字典,它从包名称映射到glob模式列表。如果数据文件包含在包的子目录中,则globs可能包含子目录名称。

所以你的包是goosemplstylelib/goose.mplstyle是要包含在goosempl包数据中的文件。

你的第二个问题(No such file or directory)很简单:在copy_style()函数中,你永远不会在写文件之前检查文件的父目录是否存在。你应该能够通过删除目录/home/<user>/.config/matplotlib/stylelib/(或暂时移动它)在本地重现这个。

修复也很简单,实际上有很多。使用您想要创建缺少目录的任何内容。

  • distutils.dir_util.mkpath适用于python2python3for fname in files: path = os.path.join(matplotlib.get_configdir(), fname) distutils.dir_util.mkpath(os.dirname(path))
  • 我首选的是使用pathlib,但它仅在Python 3.4之后可用: for fname in files: path = pathlib.Path(matplotlib.get_configdir(), fname) path.parent.mkdir(parents=True, exist_ok=True)
© www.soinside.com 2019 - 2024. All rights reserved.