[英]Python 3.X package distribution and config files
My goal is to create Python package from my project that copies config file into user home directory during pip install.我的目标是从我的项目中创建 Python package,在 pip 安装期间将配置文件复制到用户主目录中。
Example: pip install my_project
示例:
pip install my_project
Result:结果:
1. my_project installed in site-packages
2. file system.yaml copied into ~/system.yaml
Structure:结构:
my_project/
src/ - all code
config/ - configuration files
Python 3.8 Python 3.8
Packaging tool: setuptools + wheels打包工具:setuptools+wheels
Configuration file: my_project/config/system.yaml配置文件:my_project/config/system.yaml
Q: What is a pythonic way to solve this issue?问:解决这个问题的pythonic方法是什么?
PS: I know how to do it on runtime but I need it during "pip install" time PS:我知道如何在运行时执行此操作,但在“pip install”期间我需要它
Thanks谢谢
I have the same problem.我也有同样的问题。 I am thinking about these possibilities as it looks there is no direct way.
我正在考虑这些可能性,因为看起来没有直接的方法。 Install is not configuration but I guess you need something similar to 'apt install'.
安装不是配置,但我想您需要类似于“apt install”的东西。 Not only install but also populate default config files in some editable, predictable place
不仅安装而且在一些可编辑、可预测的地方填充默认配置文件
first add simple example file using data-files=[] directove首先使用 data-files=[] 指令添加简单的示例文件
write simple code at the end of setup.py file在 setup.py 文件末尾写简单的代码
or write something more sophisticated to extend setup.py functionality或编写更复杂的东西来扩展 setup.py 功能
https://docs.python.org/3/distutils/setupscript.html#installing-additional-files https://docs.python.org/3/distutils/setupscript.html#installing-additional-files
https://jichu4n.com/posts/how-to-add-custom-build-steps-and-commands-to-setuppy/ https://jichu4n.com/posts/how-to-add-custom-build-steps-and-commands-to-setuppy/
I am facing the same problem right now.我现在面临同样的问题。 My turnaround is to do create a
config.py
that contains something like the following:我的转变是创建一个包含以下内容的
config.py
:
import pathlib
try:
with open(pathlib.Path.home().joinpath(".mypackage/config.toml"), mode="rb") as fp:
data = tomllib.load(fp)
MY_CONSTANT = data["my_constant"]
ANOTHER_CONST = data["another_constant"]
except FileNotFoundError:
MY_CONSTANT = 0.0
ANOTHER_CONST = "potato"
and then explaining in the documentation how to add a custom config file.然后在文档中解释如何添加自定义配置文件。 Hope it helps!
希望能帮助到你!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.