Issue
I am trying to setup config classes for my project but i cant seem to figure out how to import them properly.
Project structure:
-project
-instance
-config.py
-service
-__init__.py
config.py:
class DefaultConfig(object):
DEBUG = True
class ProductionConfig(DefaultConfig):
DEBUG = False
init.py:
from flask import Flask
def create_app():
app = Flask(__name__, instance_relative_config=True)
app.config.from_object('config.ProductionConfig')
return app
To run the app i did:
set FLASK_APP=service
set FLASK_ENV=production
FLASK run
While standing in the project
folder
That gives me this error:
ImportStringError: import_string() failed for 'config'. Possible reasons are:
- missing __init__.py in a package;
- package or module path not included in sys.path;
- duplicated package or module name taking precedence in sys.path;
- missing module, class, function or variable;
Debugged import:
- 'config' not found.
Original exception:
ModuleNotFoundError: No module named 'config'
My guess is that the path is not right. I followed this example, where they talk about using inheritance for configuration and instance folders. It made alot of sense when i read it but i must have managed to missinterpret an integral part.
EDIT: Now it works. Instead of just
app.config.from_object('config.DefaultConfig')
i had to do
app.config.from_object('instance.config.DefaultConfig')
I think that is really confusing because setting the instance_relative_config
to True like i did, should according to the docs set the path for the config files relative to the instance directory. Now i am accessing them relative to the project root...
Solution
After import, only the class needs to be given as a parameter.
from project.instance.config import ProductionConfig
app.config.from_object(ProductionConfig)
Answered By - Milovan Tomašević
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.