See anything wrong with my attempt to get Flask running? (mod_wsgi + virtualenv)


I have a VPS running a fresh install of Ubuntu 10.04 LTS. I'm trying to set up a live application using the Flask microframework, but it's giving me trouble. I took notes while I tried to get it running and here's my play-by-play in an effort to pinpoint exactly where I went wrong.


$ adduser myapp
$ sudo apt-get install python-setuptools
$ sudo easy_install pip
$ sudo pip install virtualenv

-- www/

$ sudo pip install virtualenv

-- www/
-- env/

$ . env/bin/activate
$ easy_install Flask


$ sudo apt-get install apache2
$ sudo apt-get install libapache2-mod-wsgi

Creating WSGI file

$ nano /home/myapp/www/myapp.wsgi

--myapp.wsgi contents:--------------------------
activate_this = '/home/myapp/env/bin/'
execfile(activate_this, dict(__file__=activate_this))
from myapp import app as application

-- www/
     -- myapp.wsgi
-- env/

Configuring Apache

$ nano /etc/apache2/sites-available/ file contents ---------------------
<VirtualHost *:80>

    WSGIDaemonProcess myapp user=myapp group=myapp threads=5 python-path=/home/myapp/env/lib/python2.6/site-packages

    WSGIScriptAlias / /home/myapp/www/myapp.wsgi

    <Directory /home/myapp/www>
        WSGIProcessGroup myapp
        WSGIApplicationGroup %{GLOBAL}
        Order deny,allow
        Allow from all

Enable the virtual host file I just created

$ cd /etc/apache2/sites-enabled
$ ln -s ../sites-available/

Restart Apache

$ /etc/init.d/apache2 restart

Servers me a 500 server error page. Here's the latest error log:

mod_wsgi (pid=3514): Target WSGI script '/home/myapp/www/myapp.wsgi' cannot be loaded as Python module.
mod_wsgi (pid=3514): Exception occurred processing WSGI script '/home/myapp/www/myapp.wsgi'.
Traceback (most recent call last):
File "/home/myapp/www/myapp.wsgi", line 4, in <module>
from myapp import app as application
ImportError: No module named myapp

The errors allude that it's something strikingly obvious, but I'm quite lost.

10/25/2012 5:47:50 PM

Accepted Answer

Obviously, it cannot find your "myapp" package. You should add it to the path in your myapp.wsgi file like this:

import sys
from myapp import app

Also, if myapp module is a package, you should put and empty file into its directory.

10/25/2012 5:48:25 PM

Edit line sys.path.append, it needs to be a string.

import sys

Notice the single quotes.

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