简体   繁体   English

根据我导入它的源创建具有动态设置的 python 库

[英]Creating python library with dynamic settings based on the source where I import it

I created a python library (pyhton3.7) with the following structure我创建了一个具有以下结构的 python 库(pyhton3.7)

- my_library:
  - module1
    -- file.py
  - module
  - settings.py
- setup.py

The settings.py file has some config constants like API_URL='http://dev...' , some classes location like USER_CLASS=my_library.module1 settings.py 文件有一些配置常量,如API_URL='http://dev...' ,一些类位置如USER_CLASS=my_library.module1

I would like when I import this library into an application to change the settings/py constant var dynamically based on my app settings.当我将此库导入应用程序时,我想根据我的应用程序设置动态更改设置/py 常量 var。

Is there any clean way to do it?有什么干净的方法吗? I saw DynaConf but it doesn't seem to be useful in this case.我看到了 DynaConf,但在这种情况下似乎没有用。

I think it's possible to do that using DynaConf.我认为使用 DynaConf 可以做到这一点。

Take a look at that part of " working variables ".看看“ 工作变量”的那一部分。

The config file can be of the following formats: .toml , .py , .yaml and .json配置文件可以是以下格式: .toml , .py , .yaml.json

And still has support for .env files并且仍然支持.env文件

Consider looking at the lib's CLI.考虑查看 lib 的 CLI。 It makes the job a lot easier: https://dynaconf.readthedocs.io/en/latest/guides/cli.html它使工作变得容易得多:https://dynaconf.readthedocs.io/en/latest/guides/cli.html

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

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