How to solve import errors while trying to deploy Flask using WSGI on Apache2


I am having an issue deploying a flask app on apache2 using wsgi. I have posted the error logs and config files below. I have tried moving things around, renaming them, etc, but all give me an internal server error. Not sure why I'm getting the import error. Any input / suggestions are appreciated, thanks!

Here is my apache error.log

[Sun Sep 12 20:47:59 2010] [error] [client] mod_wsgi (pid=9753): Target WSGI script '/sites/flaskfirst/' cannot be loaded as Python module.
[Sun Sep 12 20:47:59 2010] [error] [client] mod_wsgi (pid=9753): Exception occurred processing WSGI script '/sites/flaskfirst/'.
[Sun Sep 12 20:47:59 2010] [error] [client] Traceback (most recent call last):
[Sun Sep 12 20:47:59 2010] [error] [client]   File "/sites/flaskfirst/", line 1, in <module>
[Sun Sep 12 20:47:59 2010] [error] [client]     from app import app as application
[Sun Sep 12 20:47:59 2010] [error] [client] ImportError: No module named app

# This is
from app import app as application

# This is
from flask import Flask, render_template
import settings

app = Flask(__name__)
app.debug = settings.DEBUG

from views.homepage import *
from import *
from views.submit import *
from views.feed import *

if __name__ == "__main__":

Here is the basics of the directory tree, to give you an idea.


Here is the apache virtualhost file

<VirtualHost *:80>
        DocumentRoot /sites/flaskfirst

        # WSGI Settings
        WSGIScriptAlias / /sites/flaskfirst/
        WSGIDaemonProcess flaskfirst user=sreustle group=general processes=1 threads=10
        WSGIProcessGroup flaskfirst

        # Static Directories
        Alias /static /sites/flaskfirst/static/
        <Location "/static">
                SetHandler None

9/12/2010 9:02:06 PM

Accepted Answer

Thanks to zarf and damjan on at #pocoo, they were able to help me get this fixed. The problem was the PythonPath was not correct. We fixed this by using the following

import sys
sys.path.insert(0, "/sites/flaskfirst")

from app import app
application = app
9/13/2010 7:50:26 PM

I used your solution to get it working but it kept duplicating the path in sys.path (you can write it out to see if it happens to you) so I made a little modification:

import sys
flaskfirst = "/sites/flaskfirst"
if not flaskfirst in sys.path:
    sys.path.insert(0, flaskfirst)

from app import app
application = app

That way it's only included once

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow