简体   繁体   English

如何在git中组织python项目?

[英]How to organize a python project in git?

I have a python project A with module name config which include a config.yaml file. 我有一个Python项目A,模块名称为config,其中包含config.yaml文件。 This project is used as a template for others to use and update the config.yaml based on their need. 此项目用作模板,供其他用户根据需要使用和更新config.yaml。 I want these users to fork my git project and make their updates on their fork and be able to get updates to the template when one available. 我希望这些用户分叉我的git项目,并在其分叉上进行更新,并能够在模板可用时获得模板的更新。 But, I don't want to overwrite the config file as they pull the changes from the master project to their fork. 但是,我不想覆盖配置文件,因为他们将更改从主项目拉到其分支。 How to solve this? 如何解决呢?

My_app
  MyConfigModule
     __init__.py
     config_helper.py
     config.yaml
  myAppModule
    __init__.py
    myModule.py

First of all, separate code and configuration. 首先,单独的代码和配置。 The whole code should reside in My_app and not be touched by users, but only developers, whereas the configuration can sit in the root directory. 整个代码应驻留在My_app ,并且只有开发人员才能被用户触摸,而配置可以位于根目录中。

You have multiple options how you want to separate default and actual configuration: 您有多种选择方式想要分别区分默认配置和实际配置:

Have a default configuration and allow the user to set up their local configuration . 具有默认配置,并允许用户设置其本地配置 The user has to create a config.yaml file themselves. 用户必须自己创建一个config.yaml文件。 For instance, bash (and many other programs) reads its configuration from /etc/bash.bashrc and then gets updated from ~/.bashrc . 例如,bash(和许多其他程序)从/etc/bash.bashrc读取其配置,然后从~/.bashrc进行更新。 The downside of this approach is that you need to implement a configuration merging mechanism. 这种方法的缺点是您需要实现配置合并机制。 This is a good idea if some part of the configuration is fairly static/complex (say, program settings or transformation rules), but another part often needs to be overwritten (user name / email address). 如果配置的某些部分相当静态/复杂(例如,程序设置或转换规则),但是另一部分通常需要覆盖(用户名/电子邮件地址),则这是一个好主意。

Require the user to copy config.default.yaml to config.yaml . 要求用户config.default.yaml复制到config.yaml On program installation or running when config.yaml is not present, you can either throw an error with an explanation of how to copy, silently fall back to the default configuration, or copy the default configuration to config.yaml and then continue on. 在不存在config.yaml的情况下进行程序安装或运行时,您可以引发错误并说明如何复制,可以静默回退到默认配置,或者将默认配置复制到config.yaml然后继续。 You can put a lot of documentation into the default configuration, but the downside will be a long config file. 您可以在默认配置中放入很多文档,但缺点是配置文件太长。 For instance, php's default configuration is 2000 lines long. 例如, php的默认配置是2000行长。

Prompt the user for the most important (or all) configuration options and write a small configuration file, like debian's debconf. 提示用户提供最重要的(或全部)配置选项,并编写一个小的配置文件,例如debian的debconf。 This is a good idea if your program has an interactive UI. 如果您的程序具有交互式UI,则这是一个好主意。


You can mix these options, for instance prompt the user for the most important options and rely on the default configuration, or copy over a long default configuration with slight modifications after prompting the user. 您可以混合使用这些选项,例如,提示用户提供最重要的选项并依赖默认配置,或者在提示用户后通过稍作修改复制长的默认配置。

In all of these cases, you want to include config.yaml in your gitignore and not check one in. The default configuration file should be placed at the top level of your repository if you expect the user to copy it manually. 在所有这些情况下,您要包括config.yamlgitignore ,而不是二选一中,默认的配置文件应该在你的资料库的顶层,如果你希望用户手动复制它被放置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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