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?
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!"
I've been able to move "around" this issue, by:
- Not having
from appname import app
anywhere else other than inrun.py
- 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