在安装过程中复制配置文件的正确方法?
Proper way to copy configuration files during installation?
我正在尝试分发我写的 mplstyle
以便我可以轻松共享它。它归结为在安装期间将文本文件复制到正确的配置方向(这对任何体系结构都是已知的)。我希望能够使用 python setup.py install
或 pip 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
在安装过程中以稳健的方式复制配置文件的正确方法是什么?
当前方法
文件结构
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/*
首先,根据您提供的项目结构,您没有正确指定 package_data
。如果 goosempl
是一个包并且 stylelib
里面有一个包含 mplstyle
文件的目录(我从你的代码中假设),那么你的 package_data
配置行应该是:
package_data = {'goosempl': ['stylelib/goose.mplstyle']},
如Building and Distributing Packages with Setuptools所述:
The package_data
argument is a dictionary that maps from package names to lists of glob patterns. The globs may include subdirectory names, if the data files are contained in a subdirectory of the package.
因此您的包是 goosempl
,stylelib/goose.mplstyle
是要包含在 goosempl
包数据中的文件。
你的第二个问题(No such file or directory
)很简单:在copy_style()
函数中,你在写入文件之前从不检查文件的父目录是否存在。您应该能够通过删除目录 /home/<user>/.config/matplotlib/stylelib/
(或临时移动它)在本地重现此文件。
修复也很简单,其实有很多。使用任何你想创建丢失的目录。
distutils.dir_util.mkpath
适用于python2
和python3
:
for 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)
我正在尝试分发我写的 mplstyle
以便我可以轻松共享它。它归结为在安装期间将文本文件复制到正确的配置方向(这对任何体系结构都是已知的)。我希望能够使用 python setup.py install
或 pip 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
在安装过程中以稳健的方式复制配置文件的正确方法是什么?
当前方法
文件结构
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/*
首先,根据您提供的项目结构,您没有正确指定 package_data
。如果 goosempl
是一个包并且 stylelib
里面有一个包含 mplstyle
文件的目录(我从你的代码中假设),那么你的 package_data
配置行应该是:
package_data = {'goosempl': ['stylelib/goose.mplstyle']},
如Building and Distributing Packages with Setuptools所述:
The
package_data
argument is a dictionary that maps from package names to lists of glob patterns. The globs may include subdirectory names, if the data files are contained in a subdirectory of the package.
因此您的包是 goosempl
,stylelib/goose.mplstyle
是要包含在 goosempl
包数据中的文件。
你的第二个问题(No such file or directory
)很简单:在copy_style()
函数中,你在写入文件之前从不检查文件的父目录是否存在。您应该能够通过删除目录 /home/<user>/.config/matplotlib/stylelib/
(或临时移动它)在本地重现此文件。
修复也很简单,其实有很多。使用任何你想创建丢失的目录。
distutils.dir_util.mkpath
适用于python2
和python3
:for 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)