I've produced a python web application with this particular directory structure:

# cd /usr/local/www/myapp


I've set my PYTHONPATH to:


In myapp.wsgi I attempt to complete:

import layout

But I get Internal server error. Why?

This really is my myapp.wsgi (basically take away the import layout line, it really works):

import sys
import wsgiref
import layout    
def application(environ, start_response):
        response_status = '200 OK'
        response_body = 'Hello! '
        response_headers = []
        content_type = ('Content-type', 'text-plain')
        content_length = ('Content-Length', str(len(response_body)))
        start_response(response_status, response_headers)
        return [response_body]

Full error message I get:

Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator, webmaster@example.com and inform them of the time the error occurred, and anything you might have done that may have caused the error.

More information about this error may be available in the server error log.

My virtualhost configuration:

<VirtualHost *:80>

    ServerName localhost
    ServerAlias localhost
    ServerAdmin webmaster@example.com

    DocumentRoot /usr/local/www/myapp/public

    <Directory /usr/local/www/myapp/public>
    Order allow,deny
    Allow from all

    WSGIScriptAlias / /usr/local/www/myapp/myapp.wsgi

    <Directory /usr/local/www/myapp>
    Order allow,deny
    Allow from all


Error from /var/log/httpd-error.log:

[Fri Jan 20 15:31:03 2012] [error] [client] mod_wsgi (pid=1725): Target WSGI script '/usr/local/www/myapp/myapp.wsgi' cannot be loaded as Python module.
[Fri Jan 20 15:31:03 2012] [error] [client] mod_wsgi (pid=1725): Exception occurred processing WSGI script '/usr/local/www/myapp/myapp.wsgi'.
[Fri Jan 20 15:31:03 2012] [error] [client] Traceback (most recent call last):
[Fri Jan 20 15:31:03 2012] [error] [client]   File "/usr/local/www/myapp/myapp.wsgi", line 3, in <module>
[Fri Jan 20 15:31:03 2012] [error] [client]     import layout
[Fri Jan 20 15:31:03 2012] [error] [client] ImportError: No module named layout

Creation of print sys.path:

enter image description here


python /usr/local/www/myapp/myapp.wsgi

Will it load properly?

If so, then most likely you've some atmosphere (in ~/.bashrc or such) that is required for your application. Try::

# to wipe-out extra env
env -i bash
# try again
python /usr/local/www/myapp/myapp.wsgi

Verify you utilize same python inside your spend because the one utilized by apache WSGI.

In case your myapp.wsgi need any other env to load properly, you'll be able to do among:

  • set python path in apache, or
  • occur runtime inside your myapp.wsgi

To occur inside your WSGI code, this is actually the example code.

import os, sys
EXTRA_DIR = os.path.realpath(os.path.join(os.path.dirname(__file__), '..', '..'))
if EXTRA_DIR not in sys.path:

Place in at first of the myapp.wsgi file.

You've __init.__py inside your layout folder, but it ought to be __init__.py. The time is misplaced. I don't know if this sounds like a typo inside your publish or otherwise, but when that is what your file appears like it might cause this issue.

modules directory also requires a init.py file to become understood to be a package.