简体   繁体   English

Python 3.X package 分发和配置文件

[英]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不仅安装而且在一些可编辑、可预测的地方填充默认配置文件

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM