'Flask app routes not importing, all pages show 404. Passenger WSGI on DreamHost
I've got a Python Flask app that's working perfectly in my local environment, but when I try to get it to run on a DreamHost server all my pages turn up 404 errors.
Here's my __init__.py file:
from flask import Flask
import os
package_dir = os.path.dirname(os.path.abspath(__file__))
templates = os.path.join(package_dir, "templates")
app = Flask('name', template_folder=templates)
# some other stuff
And here's my __main__.py file, in the same directory (the folder is called c499):
from c499 import app, frontend
FLASK_PORT = 8081
if __name__ == "__main__":
app.run(debug=True,port=FLASK_PORT,host='0.0.0.0')
frontend.py is where I defined all my app routes. The actual file is pretty long because I have a lot of pages, but here's a small snippet of it:
from flask import render_template, session, redirect # and other imports
from c499 import app
@app.route('/login', methods=['GET'])
def login_get():
# if the user is logged in already, redirect to home page
if 'logged_in' in session:
return redirect('/', code=303)
# if a message was passed to this function, display that as message
passed_message = request.args.get('message')
if passed_message == None:
passed_message = 'Sign in to continue'
return render_template('login.html', message=passed_message)
So, as I said above trying to access any of my pages on the server turns up a 404 error. It isn't recognizing any of the app routes in frontend.py.
Here's the weird part though - I know there isn't a problem with my Python or Flask installation because the site is capable of rendering my pages properly, but only when the app route is defined in __init__.py. For example if I copy and paste that whole @app.route('/login', methods=['GET']) function into the end of my __init__.py file then my site correctly renders my login page at 'site.address.com/login'. And everything about it works perfectly, just like in my local environment. But of course only that page will work - all the others return 404 errors.
So as far as I can tell, the issue is that my app only recognizes functions inside of __init__.py and can't import ones from my other Python files. Is there any way for me to fix this that doesn't involve copying the contents of all my other files into __init__.py?
Here's my project structure:
site.address.com/
├─ public/
│ ├─ c499/
│ │ ├─ __init__.py
│ │ ├─ __main__.py
│ │ ├─ frontend.py
│ │ ├─ (multiple other .py modules)
│ │ ├─ templates/
│ │ │ ├─ login.html
│ │ │ ├─ index.html
│ │ │ ├─ (other html pages)
├─ passenger_wsgi.py
├─ tmp/
│ ├─ restart.txt
And also my passenger_wsgi.py file:
import sys, os
INTERP = "/usr/bin/python3"
if sys.executable != INTERP:
os.execl(INTERP, INTERP, *sys.argv)
sys.path.append(os.getcwd())
sys.path.append(os.getcwd()+'/public/c499')
from public.c499 import app as application
For reference I was mostly following this guide: https://www.brettsbeta.com/blog/2020/07/flask-on-dreamhost-shared-website-hosting/
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
