Flask and gunicorn, multiple modules: circular imports - not all routes accessible

烈酒焚心 提交于 2019-12-07 12:42:35

问题


My problem is similar to Flask and Gunicorn on Heroku import error and Procfile gunicorn custom module name but I can't seem to fix it using their solutions.

My Flask app 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"

Previously, I was experiencing erorrs when running foreman start, but those went away once I removed import appname.views from app.py.

Now, foreman start runs the app and route / is accessible but /there is not. How come?


回答1:


Hooray! I've been able to have it work with the code I really wanted.

app structure (unchanged):

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

run.py (unchanged):

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 (unchanged):

from appname import app

@app.route('/')
def home():
    return "Hello, awesomeness!"



回答2:


I've been able to move "around" this issue, by:

  1. Not having from appname import app anywhere else other than in run.py
  2. Consequently, only defining routes in app.py

I would have preferred to keep my routes with my modules, although I am unsure which is the better Python style.



来源:https://stackoverflow.com/questions/22921187/flask-and-gunicorn-multiple-modules-circular-imports-not-all-routes-accessib

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!