Pylint is yelling at me for putting a from .views import *
at the end of my __init__.py
saying imports should be placed at the top of the module.
If I place it at the top of __init__.py
then Flask can't find my routes (views) so that doesn't work. Page doesn't load, 404 error. Loads fine when routes are imported at the end.
A couple questions:
for reference, in case:
.
├── README.md
├── my_app
│ ├── __init__.py
│ ├── forms.py
│ ├── models.py
│ ├── static
│ ├── templates
│ │ ├── index.html
│ │ └── loggedin.html
│ └── views.py
├── config.py
├── instance
│ └── config.py
├── requirements.txt
└── run.py
example of what's in __init__.py
from flask import Flask, render_template
from authlib.integrations.flask_client import OAuth
app = Flask(__name__, instance_relative_config=True)
app.config.from_object('config')
app.config.from_pyfile('config.py')
oauth = OAuth(app)
APP_CALLBACK_URL = app.config['APP_CALLBACK_URL']
APP_CLIENT_ID = app.config['APP_CLIENT_ID']
APP_CLIENT_SECRET = app.config['APP_CLIENT_SECRET']
APP_DOMAIN = app.config['APP_DOMAIN']
APP_BASE_URL = 'https://' + APP_DOMAIN
my_app = oauth.register(
'MY_APP',
client_id=APP_CLIENT_ID,
client_secret=APP_CLIENT_SECRET,
api_base_url=APP_BASE_URL,
access_token_url=APP_BASE_URL + '/oauth/token',
authorize_url=APP_BASE_URL + '/authorize',
)
from .views import *
Generally speaking, imports should go at the top, but Flask documentation talks about this kind of situations and encourages you to do as you did. Taken from https://flask.palletsprojects.com/en/1.1.x/patterns/packages/:
the Flask application object creation has to be in the
__init__.py
file. That way each module can import it safely and the__name__
variable will resolve to the correct package.all the view functions (the ones with a
route()
decorator on top) have to be imported in the__init__.py
file. Not the object itself, but the module it is in. Import the view module after the application object is created.
By the way, don't do from .views import *
. Do import .views
instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With