Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask, mod_wsgi, and Apache: ImportError

I'm getting this in my error logs:

ImportError: No module named flask

It looks exactly like in Django + mod_wsgi + apache: ImportError at / No module named djproj.urls, but I tried that solution, and it doesn't seem to work... I am correctly inserting the app home and parent folder into the system path, but this error is still coming up.

Navigating to my page still brings up the 500 page.

More information: I'm using Amazon EC2 free tier, with Apache in the form of httpd. Everything is installed correctly, I'm pretty sure...

Here's my stuff (app name badassery and app home folder name hatemail - I change my mind a lot):

Error log

[Sun Nov 11 07:14:45 2012] [error] [client 18.189.71.148] mod_wsgi (pid=28143): Target WSGI script '/home/ec2-user/hatemail/badassery.wsgi' cannot be loaded as Python module.
[Sun Nov 11 07:14:45 2012] [error] [client 18.189.71.148] mod_wsgi (pid=28143): Exception occurred processing WSGI script '/home/ec2-user/hatemail/badassery.wsgi'.
[Sun Nov 11 07:14:45 2012] [error] [client 18.189.71.148] Traceback (most recent call last):
[Sun Nov 11 07:14:45 2012] [error] [client 18.189.71.148]   File "/home/ec2-user/hatemail/badassery.wsgi", line 6, in <module>
[Sun Nov 11 07:14:45 2012] [error] [client 18.189.71.148]     from badassery import app as application
[Sun Nov 11 07:14:45 2012] [error] [client 18.189.71.148]   File "/home/ec2-user/hatemail/badassery.py", line 6, in <module>
[Sun Nov 11 07:14:45 2012] [error] [client 18.189.71.148]     from flask import Flask, request, session, url_for, redirect, render_template, abort, g, flash, _app_ctx_stack
[Sun Nov 11 07:14:45 2012] [error] [client 18.189.71.148] ImportError: No module named flask
[Sun Nov 11 07:14:45 2012] [error] [client 18.189.71.148] mod_wsgi (pid=28143): Target WSGI script '/home/ec2-user/hatemail/badassery.wsgi' cannot be loaded as Python module.
[Sun Nov 11 07:14:45 2012] [error] [client 18.189.71.148] mod_wsgi (pid=28143): Exception occurred processing WSGI script '/home/ec2-user/hatemail/badassery.wsgi'.
[Sun Nov 11 07:14:45 2012] [error] [client 18.189.71.148] Traceback (most recent call last):
[Sun Nov 11 07:14:45 2012] [error] [client 18.189.71.148]   File "/home/ec2-user/hatemail/badassery.wsgi", line 6, in <module>
[Sun Nov 11 07:14:45 2012] [error] [client 18.189.71.148]     from badassery import app as application
[Sun Nov 11 07:14:45 2012] [error] [client 18.189.71.148]   File "/home/ec2-user/hatemail/badassery.py", line 6, in <module>
[Sun Nov 11 07:14:45 2012] [error] [client 18.189.71.148]     from flask import Flask, request, session, url_for, redirect, render_template, abort, g, flash, _app_ctx_stack
[Sun Nov 11 07:14:45 2012] [error] [client 18.189.71.148] ImportError: No module named flask

badassery.wsgi

import sys

sys.path.insert(0,'/home/ec2-user/hatemail')
sys.path.insert(1,'/home/ec2-user')

from badassery import app as application

Additions to the httpd.conf file

WSGISocketPrefix /var/run/wsgi

<VirtualHost *:80>
        ServerName 54.243.61.41
        DocumentRoot "/home/ec2-user/hatemail"

        WSGIDaemonProcess badassery user=apache group=apache processes=1 threads=5
        WSGIScriptAlias / /home/ec2-user/hatemail/badassery.wsgi
        WSGIScriptReloading On

        <Directory /home/ec2-user/hatemail>
                WSGIProcessGroup badassery
                WSGIApplicationGroup %{GLOBAL}
                Order deny,allow
                Allow from all
        </Directory>
</VirtualHost>

Directory structure

~
    hatemail
        badassery.py
        badassery.wsgi
        Procfile
        README
        requirements.txt
        schema.sql
        static/
        templates/
        venv/bin/activate

Yes, I am doing "if name == 'main': app.run()".

How can I fix this problem?

like image 247
Oliver Avatar asked Nov 11 '12 07:11

Oliver


2 Answers

If you're deploying a virtualenv, you'll have to make sure to activate it first. You should update your wsgi file like this (updating the values to match your environment):

activate_this = '/path/to/virtualenv/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this)

import sys
sys.path.insert(0, '/path/to/flask/appname')
sys.path.insert(0,'/home/ec2-user/hatemail')
sys.path.insert(1,'/home/ec2-user')

from badassery import app as application

If you aren't deploying a virtualenv, you probably just need to make sure that Flask is installed on your system. If you've already installed setuptools you can just:

easy_install flask
like image 167
apiguy Avatar answered Sep 27 '22 19:09

apiguy


If you have any problems with importing when using wsgi_mod, make sure you try explicitly pointing to the import files that are causing problems:

sys.path.append('/home/foo/www/Forms')

then do your import!

like image 34
user2099484 Avatar answered Sep 27 '22 20:09

user2099484