Config.from_object not working in Flask with Python 3

I have the following flask code, __init __ file. py:

from flask import Flask
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from flask_sqlalchemy_session import flask_scoped_session

from . import configmodule

app = Flask(__name__)

engine = create_engine(configmodule.DevelopmentConfig.SQLALCHEMY_DATABASE_URI)  # <--- THIS WORKS

session_factory = sessionmaker(bind=engine)
session = flask_scoped_session(session_factory, app)

app.config.from_object('configmodule.DevelopmentConfig')  # <--- THIS FAILS IN Python 3

                                 ...

The configmodule.py file is in the same directory as __init__.py above.

After starting with python 3.5.2, I get:

werkzeug.utils.ImportStringError: import_string() failed for 'configmodule.DevelopmentConfig'. 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:

- 'configmodule' not found.

This error is for the last line in the code snippet:

app.config.from_object('configmodule.DevelopmentConfig')  # <--- THIS FAILS IN Python 3

I had no problem running it with Python 2. Any idea how to make it work with Python 3? Thank.

+4
source share
1 answer

Python 3 has dropped support for implicit relative imports. You need to use absolute import

app.config.from_object('packagename.configmodule.DevelopmentConfig')

Explicit relative imports are not supported from_object.

The section contains the PEP 8 import section .

: .

+5

Source: https://habr.com/ru/post/1664385/


All Articles