Question

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?

Was it helpful?

Solution

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!"

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top