Flask, mod_wsgi, and Apache: ImportError - python

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?

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

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!

Related

ModuleNotFoundError when making a Flask website using Apache2

I am trying to run a Flask website using Apache2 on Ubuntu 22.04, but I'm getting an error when I try to go to the website. I get a 500 Internal Server Error. I posted the error logs down below. If I run the __init__.py file directly with python3 __init__.py, it works. However with systemctl start apache2, it gives the error.
The file structure:
Flaskapp/Flaskapp/
__init__.py
static
templates
main.py
flaskapp.wsgi
Error:
[Thu Dec 01 15:51:15.649651 2022] [wsgi:error] [pid 1261:tid 139847272486464] [client 145.28.163.210:50244] mod_wsgi (pid=1261): Failed to exec Python script file '/var/www/FlaskApp/flaskapp.wsgi'.
[Thu Dec 01 15:51:15.649689 2022] [wsgi:error] [pid 1261:tid 139847272486464] [client 145.28.163.210:50244] mod_wsgi (pid=1261): Exception occurred processing WSGI script '/var/www/FlaskApp/flaskapp.wsgi'.
[Thu Dec 01 15:51:15.649818 2022] [wsgi:error] [pid 1261:tid 139847272486464] [client 145.28.163.210:50244] Traceback (most recent call last):
[Thu Dec 01 15:51:15.649833 2022] [wsgi:error] [pid 1261:tid 139847272486464] [client 145.28.163.210:50244] File "/var/www/FlaskApp/flaskapp.wsgi", line 7, in <module>
[Thu Dec 01 15:51:15.649835 2022] [wsgi:error] [pid 1261:tid 139847272486464] [client 145.28.163.210:50244] from FlaskApp import app as application
[Thu Dec 01 15:51:15.649839 2022] [wsgi:error] [pid 1261:tid 139847272486464] [client 145.28.163.210:50244] File "/var/www/FlaskApp/FlaskApp/__init__.py", line 8, in <module>
[Thu Dec 01 15:51:15.649841 2022] [wsgi:error] [pid 1261:tid 139847272486464] [client 145.28.163.210:50244] import main
[Thu Dec 01 15:51:15.649850 2022] [wsgi:error] [pid 1261:tid 139847272486464] [client 145.28.163.210:50244] ModuleNotFoundError: No module named 'main'
The imports in __init__.py:
from flask import Flask, session, render_template, redirect, request, make_response
import os
import jinja2
import main
import ai
import api
import settings
import database
import time
import base64
The wsgi file:
#!/usr/bin/python
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/FlaskApp/")
from FlaskApp import app as application
application.secret_key = 'something super SUPER secret'
The FlaskApp.conf file:
<VirtualHost *:80>
ServerName vps-29.sgrt.nl
ServerAdmin youemail#email.com
WSGIScriptAlias / /var/www/FlaskApp/flaskapp.wsgi
<Directory /var/www/FlaskApp/FlaskApp/>
Order allow,deny
Allow from all
</Directory>
Alias /static /var/www/FlaskApp/FlaskApp/static
<Directory /var/www/FlaskApp/FlaskApp/static/>
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Can someone tell me what I'm doing wrong? I tried changing the file names of main.py and the __init__.py files. Which did nothing. I checked the python versions, and they are both Python3.10.
I have no clue what I'm doing wrong, so someone, please help.

Pygal import failure in apache

I'm trying to run a simple Flask web app that displays a pygal graph. When I run it with the python development server, the application runs fine. However, when I try to run it in apache2, I get the following error:
[Thu Dec 10 19:55:22.745469 2020] [wsgi:error] [pid 17741] [client ::1:34366] mod_wsgi (pid=17741): Failed to exec Python script file '/var/www/FlaskApp/FlaskApp.wsgi'.
[Thu Dec 10 19:55:22.745521 2020] [wsgi:error] [pid 17741] [client ::1:34366] mod_wsgi (pid=17741): Exception occurred processing WSGI script '/var/www/FlaskApp/FlaskApp.wsgi'.
[Thu Dec 10 19:55:22.745553 2020] [wsgi:error] [pid 17741] [client ::1:34366] Traceback (most recent call last):
[Thu Dec 10 19:55:22.745586 2020] [wsgi:error] [pid 17741] [client ::1:34366] File "/var/www/FlaskApp/FlaskApp.wsgi", line 8, in <module>
[Thu Dec 10 19:55:22.745650 2020] [wsgi:error] [pid 17741] [client ::1:34366] from FlaskApp import app as application
[Thu Dec 10 19:55:22.745665 2020] [wsgi:error] [pid 17741] [client ::1:34366] File "/var/www/FlaskApp/FlaskApp/__init__.py", line 2, in <module>
[Thu Dec 10 19:55:22.745707 2020] [wsgi:error] [pid 17741] [client ::1:34366] import pygal
[Thu Dec 10 19:55:22.745723 2020] [wsgi:error] [pid 17741] [client ::1:34366] File "/var/www/FlaskApp/FlaskApp/venv/lib/python3.8/site-packages/pygal/__init__.py", line 28, in <module>
[Thu Dec 10 19:55:22.745786 2020] [wsgi:error] [pid 17741] [client ::1:34366] import pkg_resources
[Thu Dec 10 19:55:22.745838 2020] [wsgi:error] [pid 17741] [client ::1:34366] File "/var/www/FlaskApp/FlaskApp/venv/lib/python3.8/site-packages/pkg_resources/__init__.py", line 1365
[Thu Dec 10 19:55:22.745850 2020] [wsgi:error] [pid 17741] [client ::1:34366] raise SyntaxError(e) from e
[Thu Dec 10 19:55:22.745857 2020] [wsgi:error] [pid 17741] [client ::1:34366] ^
[Thu Dec 10 19:55:22.745864 2020] [wsgi:error] [pid 17741] [client ::1:34366] SyntaxError: invalid syntax
Here is the code that I'm running
# -*- coding: utf-8 -*-
import pygal
from flask import Flask, Response
app = Flask(__name__)
#app.route('/')
def index():
""" render svg on html """
return """
<html>
<body>
<h1>hello pygal</h1>
<figure>
<embed type="image/svg+xml" src="/graph/" />
</figure>
</body>
</html>'
"""
#app.route('/graph/')
def graph():
""" render svg graph """
bar_chart = pygal.Bar()
bar_chart.add('Fibonacci', [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55])
return Response(response=bar_chart.render(), content_type='image/svg+xml')
if __name__ == '__main__':
app.run()
(Note that this code is just a sample copied from github)
Here is my apache site configuration:
<VirtualHost *:443>
ServerName flaskapp.localhost
ServerAdmin me#email.com
WSGIScriptAlias / /var/www/FlaskApp/FlaskApp.wsgi
<Directory /var/www/FlaskApp/FlaskApp/>
Order allow,deny
Allow from all
</Directory>
Alias /static /var/www/FlaskApp/FlaskApp/static
<Directory /var/www/FlaskApp/FlaskApp/static/>
Order allow,deny
Allow from all
</Directory>
DocumentRoot /var/www/FlaskApp
SSLEngine on
SSLCertificateFile /etc/ssl/certs/apache-selfsigned.crt
SSLCertificateKeyFile /etc/ssl/private/apache-selfsigned.key
ErrorLog ${APACHE_LOG_DIR}/FlaskApp-error.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/FlaskApp-access.log combined
AddDefaultCharset utf-8
</VirtualHost>
Here is my wsgi file:
python_home = '/var/www/FlaskApp/FlaskApp/venv'
activate_this = python_home + '/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/FlaskApp/")
sys.path.insert(0,"/var/www/FlaskApp/FlaskApp/venv/lib/python3.8/site-packages/")
from FlaskApp import app as application
application.secret_key = 'secret key stuff'
Here are some details about my environment:
Ubuntu 20.04.1 LTS
Apache/2.4.41 (Ubuntu)
Python 3.8.5 (installed in a virtual environment)
Any thoughts would be greatly appreciated!
It appears that the python interpreter is compiled into the mod_wsgi.so file. An old version, that included python2, was being pointed to by Apache. To fix it I did the following:
Removed the old version of mod_wsgi by doing sudo apt-get remove libapache2-mod-wsgi
modified /etc/apache2/mods-available/mod_wsgi.load to point to the library (.so) file in my virtual environment. The contents of mod_wsgi looked like this:
LoadModule wsgi_module /var/www/FlaskApp/FlaskApp/venv/lib/python3.8/site-packages/mod_wsgi/server/mod_wsgi-py38.cpython-38-x86_64-linux-gnu.so
I suspect that modifying it here points all Apache virtual hosts to this python virtual environment, which isn't really ideal. It would be better if each Apache virtual host pointed to its own python environment.
Modified my wsgi file to work with the new version of python. Here are the contents:
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/FlaskApp/")
sys.path.insert(0,"/var/www/FlaskApp/FlaskApp/venv/lib/python3.8/site-packages/")
from FlaskApp import app as application
application.secret_key = 'secret key stuff'
Changed the local imports in my __init__.py file to be preceeded by a dot(.)
Following these changes the site is now working as expected.

Running Flask application from virtual environment

I have a simple flask app that i want to be accessible from internet.
OS is debian7, and unfortunately it can't be changed.
What I did:
- downloaded and installed Python3.4 into /usr/local/opt/python3.4.3
- installed wsgi lib for py3
- installed virtualenv and created new env in my flask project folder
- installed flask together with some other packages that I need within this env
- created VirtualHost as follows:
cat /etc/apache2/sites-available/Monitor
Listen 8080
<VirtualHost *:8080>
ServerName {ip address}
ServerAdmin {my email}
WSGIScriptAlias / /var/www/Monitor/monitor.wsgi
<Directory /var/www/Monitor/Monitor/>
Order allow,deny
Allow from all
</Directory>
Alias /static /var/www/Monitor/Monitor/static
<Directory /var/www/Monitor/Monitor/static/>
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
now when i try to access the app from the internet I see these errors in error.log
[Sat Nov 14 22:16:04 2015] [error] /usr/local/bin/python3
[Sat Nov 14 22:16:04 2015] [error] [client ip] mod_wsgi (pid=13450): Target WSGI script '/var/www/Monitor/monitor.wsgi' cannot be loaded as Python module.
[Sat Nov 14 22:16:04 2015] [error] [client ip] mod_wsgi (pid=13450): Exception occurred processing WSGI script '/var/www/Monitor/monitor.wsgi'.
[Sat Nov 14 22:16:04 2015] [error] [client ip] Traceback (most recent call last):
[Sat Nov 14 22:16:04 2015] [error] [client ip] File "/var/www/Monitor/monitor.wsgi", line 21, in <module>
[Sat Nov 14 22:16:04 2015] [error] [client ip] from Monitor import app as application
[Sat Nov 14 22:16:04 2015] [error] [client ip] File "/var/www/Monitor/Monitor/__init__.py", line 4, in <module>
[Sat Nov 14 22:16:04 2015] [error] [client ip] from flask import Flask, render_template, url_for
[Sat Nov 14 22:16:04 2015] [error] [client ip] File "/var/www/Monitor/Monitor/venv/lib/python3.4/site-packages/flask/__init__.py", line 17, in <module>
[Sat Nov 14 22:16:04 2015] [error] [client ip] from werkzeug.exceptions import abort
[Sat Nov 14 22:16:04 2015] [error] [client ip] File "/var/www/Monitor/Monitor/venv/lib/python3.4/site-packages/werkzeug/__init__.py", line 152, in <module>
[Sat Nov 14 22:16:04 2015] [error] [client ip] __import__('werkzeug.exceptions')
[Sat Nov 14 22:16:04 2015] [error] [client ip] File "/var/www/Monitor/Monitor/venv/lib/python3.4/site-packages/werkzeug/exceptions.py", line 113
[Sat Nov 14 22:16:04 2015] [error] [client ip] return u'<p>%s</p>' % escape(self.description)
[Sat Nov 14 22:16:04 2015] [error] [client ip] ^
[Sat Nov 14 22:16:04 2015] [error] [client ip] SyntaxError: invalid syntax
From what I could find in the internet, such errors appear when trying to start flask app with python version <= 3.3, which is not my case I think (how do i check?)
here is my wsgi that starts the app
#!/usr/bin/env python3
import os
import sys
import logging
logging.basicConfig(stream=sys.stderr)
PROJECT_DIR = '/var/www/Monitor/'
sys.path.insert(0, PROJECT_DIR)
def execfile(filename):
globals = dict(__file__ = filename)
exec(open(filename).read(), globals)
activate_this = '/var/www/Monitor/Monitor/venv/bin/activate_this.py'
execfile(activate_this)
from Monitor import app as application
Any help would be very much appreciated. Any additional information will be supplied if necessary.
Default Python3 on Debian 7 is 3.2.3, libapache2-mod-wsgi is in version 3.3.
You Apache run Your app with default Python because mod-wsgi has to be compiled against one and only one python version (in this case python 3.3).
mod-wsgi 3.3
If You want to run only one app (or many apps) with Python 3.4, try to recompile mod-wsgi with python 3.4
mod-wsgi 4.1.x or newer
This newer version of mod_wsgi provides a way of installing mod_wsgi against multiple Python versions and running up an Apache instance for each using a provided script. The script takes over all the setup of the Apache configuration so you do not need to worry about it.
https://pypi.python.org/pypi/mod_wsgi
old answer
To change Python version, set in Apache conf:
WSGIPythonHome /usr/local/opt/python3.4.3/
WSGIPythonPath /var/www/Monitor:/var/www/Monitor/Monitor/venv/lib/python3.4/site-packages:/usr/local/opt/python3.4.3/lib/site-packages
for details read https://code.google.com/p/modwsgi/wiki/ConfigurationDirectives#WSGIPythonHome

mod_wsgi Apache error with django app

I have a django (1.7) app that works pretty well on a Centos7 server. Now i moving to production server RHEL 6 and still have some issues.
The problem is the mod_wsgi.
The server config:
RHEL6
Apache 2.2
Django 1.7
Python 2.6
the apache error log
[Fri Feb 20 11:18:39 2015] [error] [client 127.0.0.1] mod_wsgi (pid=12827): Target WSGI script '/home/evtdb/FLWeb/FLWeb/wsgi.py' cannot be loaded as Python module.
[Fri Feb 20 11:18:39 2015] [error] [client 127.0.0.1] mod_wsgi (pid=12827): Exception occurred processing WSGI script '/home/evtdb/FLWeb/FLWeb/wsgi.py'.
[Fri Feb 20 11:18:39 2015] [error] [client 127.0.0.1] Traceback (most recent call last):
[Fri Feb 20 11:18:39 2015] [error] [client 127.0.0.1] File "/home/evtdb/FLWeb/FLWeb/wsgi.py", line 21, in <module>
[Fri Feb 20 11:18:39 2015] [error] [client 127.0.0.1] from django.core.wsgi import get_wsgi_application
[Fri Feb 20 11:18:39 2015] [error] [client 127.0.0.1] ImportError: No module named django.core.wsgi
[Fri Feb 20 11:18:39 2015] [error] [client 127.0.0.1] mod_wsgi (pid=12827): Target WSGI script '/home/evtdb/FLWeb/FLWeb/wsgi.py' cannot be loaded as Python module.
[Fri Feb 20 11:18:39 2015] [error] [client 127.0.0.1] mod_wsgi (pid=12827): Exception occurred processing WSGI script '/home/evtdb/FLWeb/FLWeb/wsgi.py'.
[Fri Feb 20 11:18:39 2015] [error] [client 127.0.0.1] Traceback (most recent call last):
[Fri Feb 20 11:18:39 2015] [error] [client 127.0.0.1] File "/home/evtdb/FLWeb/FLWeb/wsgi.py", line 21, in <module>
[Fri Feb 20 11:18:39 2015] [error] [client 127.0.0.1] from django.core.wsgi import get_wsgi_application
[Fri Feb 20 11:18:39 2015] [error] [client 127.0.0.1] ImportError: No module named django.core.wsgi
the rest of the important files
flweb.conf
WSGIScriptAlias / /home/evtdb/FLWeb/FLWeb/wsgi.py
WSGIDaemonProcess localhost python-path=/home/evtdb/FLWeb:/usr/lib/python2.6/site-packages
WSGIProcessGroup localhost
<VirtualHost 127.0.0.1:8080>
<Directory /home/evtdb/FLWeb/FLWeb>
<Files wsgi.py>
Order allow,deny
Allow from all
</Files>
</Directory>
</VirtualHost>
wsgi.py
"""
WSGI config for FLWeb project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/1.7/howto/deployment/wsgi/
"""
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "FLWeb.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
ldd modules/mod_wsgi.so
linux-vdso.so.1 => (0x00007fff6afff000)
libpython2.6.so.1.0 => /usr/lib64/libpython2.6.so.1.0 (0x00007fd69fc02000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x00007fd69f9e5000)
libdl.so.2 => /lib64/libdl.so.2 (0x00007fd69f7e0000)
libutil.so.1 => /lib64/libutil.so.1 (0x00007fd69f5dd000)
libm.so.6 => /lib64/libm.so.6 (0x00007fd69f359000)
libc.so.6 => /lib64/libc.so.6 (0x00007fd69efc4000)
/lib64/ld-linux-x86-64.so.2 (0x000000330aa00000)
I thought that a problem could be that mod_wsgi seems compiled with lib64, so I changed the python path on the flweb.conf to lib64 but it didn't worked
EDIT:
I realize the server has python 2.7 in /user/local/lib and that is the python version where django is installed. I changed the path in the conf file, but now I guess I need to rebuild the mod_wsgi
EDIT2:
Trying to rebuild mod_wsgi, have this problem
/usr/bin/ld: /usr/local/lib/libpython2.7.a(abstract.o): relocation R_X86_64_32 against `.rodata.str1.8' can not be used when making a shared object; recompile with -fPIC
according to https://code.google.com/p/modwsgi/wiki/InstallationIssues is a conflict between python that was compiled to 32 bit and mod_wsgi to 64

Import errors with WSGI, Flask, and Apache

Totally new to python-based web apps, so I'm somewhat lost. Here is my apache error:
[Wed May 08 22:41:47 2013] [error] [client 64.56.91.45] mod_wsgi (pid=23704): Target WSGI script '/home/http/public/hello/hello.wsgi' cannot be loaded as Python module.
[Wed May 08 22:41:47 2013] [error] [client 64.56.91.45] mod_wsgi (pid=23704): Exception occurred processing WSGI script '/home/http/public/hello/hello.wsgi'.
[Wed May 08 22:41:47 2013] [error] [client 64.56.91.45] Traceback (most recent call last):
[Wed May 08 22:41:47 2013] [error] [client 64.56.91.45] File "/home/http/public/hello/hello.wsgi", line 3, in <module>
[Wed May 08 22:41:47 2013] [error] [client 64.56.91.45] from hello import app as application
[Wed May 08 22:41:47 2013] [error] [client 64.56.91.45] File "/home/http/public/hello/hello.py", line 1, in <module>
[Wed May 08 22:41:47 2013] [error] [client 64.56.91.45] from flask import Flask
[Wed May 08 22:41:47 2013] [error] [client 64.56.91.45] ImportError: No module named 'flask'
[Wed May 08 22:41:47 2013] [error] [client 64.56.91.45] File does not exist: /home/http/public/favicon.ico
Apparently, it cannot find the flask module. I've looked this up, and it seems that most people get it to work by appending the project directory to the path, like so: (hello.wsgi)
import sys
sys.path.insert(0, "/home/http/public/hello")
from hello import app as application
And here is hello.py:
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello_world():
return 'Hello World!'
if __name__ == '__main__':
app.run(host='0.0.0.0')
Works great when ran as python hello.py, however, throws a 500 error when browsing to host/hello. Here's the Apache configuration:
WSGIDaemonProcess hello user=http group=http threads=5
WSGIScriptAlias /hello "/home/http/public/hello/hello.wsgi"
<Directory /home/http/public/hello/>
WSGIProcessGroup hello
WSGIApplicationGroup %{GLOBAL}
Order deny,allow
Allow from all
</Directory>
I'm somewhat lost.
The mod_wsgi module is built against a specific Python version and can only work with that version. You cannot force it to use a different version. Thus if you have multiple versions installed, you need to make sure that you are installing your packages and running your code in development against the same version.
You can determine which Python version/installation mod_wsgi is using by running the test:
http://code.google.com/p/modwsgi/wiki/CheckingYourInstallation#Python_Installation_In_Use

Categories