'How to load config file in python from an external module
I have a config module (myConfig.py) present in a library for which i created a standard distribution package using setuptools.
--configPackage
|
| ---- myConfig.py
| ---- __init__.py
myConfig.py is a key value pair like this:
MY_NAME = 'myname'
MY_AGE = '99'
Now I have another python project where I import this config module like this
import configPackage.myConfig as customConfig
If this config file was native to my python project and had not come from an external project then I would have done something like this:
app = Flask(__name__)
app.config.from_object('app.configPackage.config')
where config is actually config.py file under configPackage.
and any key value pair in config.py could then be accessed as
myName = app.config['MY_NAME']
My problem is that I am not able to load the external config file in the above mentioned way for a native config file. What I have tried is this which doesn't work:
import configPackage.myConfig as customConfig
app = Flask(__name__)
app.config.from_object(customConfig)
myName = app.config['MY_NAME']
I get the following error
model_name = app.config['model_name'] KeyError: 'model_name'
Which means that it is not able to load the config file from external module correctly. Can anyone tell me the right way to accomplish this ?
Solution 1:[1]
Try this,
in your __init__.py
of configPackage import myConfig as shown below
import configPackage.myConfig as myConfig
And in your other app try
app.config.from_object('configPackage.myConfig')
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | gipsy |