Flask, mod_wsgi and Apache: ImportError

I get this in my error logs:

ImportError: no module named flask

It looks like Django + mod_wsgi + apache: ImportError in / No module named djproj.urls , but I tried this solution and it doesnโ€™t seem to work ... I correctly insert the home folder and the parent folder of the application in the system path, but this an error still occurs.

Going to my page still causes 500 pages.

Additional Information: I am using Amazon EC2 free tier, with Apache as httpd. Everything is installed correctly, I'm sure ...

Here are my things (badassery application name and hatemail application home folder name - I will 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 do "if name == 'main': app.run ()".

How can I fix this problem?

+7
source share
2 answers

If you are deploying virtualenv, you need to activate it first. You should update your wsgi file as follows (updating the values โ€‹โ€‹appropriate for 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 are not deploying virtualenv, you probably just need to make sure Flask is installed on your system. If you have already installed setuptools , you can simply:

 easy_install flask 
+13
source

If you have problems importing using wsgi_mod , make sure you are trying to explicitly point to the import files that are causing the problems:

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

then make your import!

+3
source

All Articles