Flask and guns, several modules: circular import - not all routes are available

My problem is similar to Flask and Gunicorn in Heroku import error and Procfile gunicorn computer_name , but I can not fix it using their solutions.

My Flask application has the following structure:

appname/ run.py Procfile venv/ ... appname/ app.py views.py 

run.py:

 from appname import app app.run(debug=True) 

app.py:

 from flask import Flask app = Flask(__name__) @app.route('/') def home(): return "here" 

PROCFILE:

 web: gunicorn --pythonpath appname app:app 

views.py:

 from appname import app from flask import render_template @app.route('/there') def there(): return "there" 

I used to experience errors when starting foreman start , but those left as soon as I removed import appname.views from app.py

Now foreman start launches the application, and the / route is accessible, but /there is not. How did it happen?

+3
source share
2 answers

Hooray! I managed to get it to work with the code that I really wanted.

Application structure (no change):

 appname/ run.py Procfile venv/ ... appname/ app.py views.py 

run.py (no change):

 from appname import app app.run(debug=True) 

app.py:

 from flask import Flask app = Flask(__name__) import appname.views import appname.anothermodule 

PROCFILE:

 web: gunicorn appname:app 

views.py (no change):

 from appname import app @app.route('/') def home(): return "Hello, awesomeness!" 
+3
source

I managed to move around this problem by clicking

  • Not having from appname import app anywhere else than run.py
  • Therefore, only the definition of routes in app.py

I would prefer to keep my routes with my modules, although I'm not sure what the best Python style is.

+1
source

All Articles