Richard Knop
Richard Knop

Reputation: 83745

Cannot import module

I have created a python web app with this directory structure:

# cd /usr/local/www/myapp

modules
    layout
        __init__.py
        layout.py
packages
public
myapp.wsgi

I have set my PYTHONPATH to:

/usr/local/www/myapp/modules:/usr/local/www/myapp/packages

In myapp.wsgi I try to do:

import layout

But I am getting Internal server error. Why?

This is my myapp.wsgi (if I remove the import layout line, it 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)))
        response_headers.append(content_type)
        response_headers.append(content_length)
        start_response(response_status, response_headers)
        return [response_body]

Full error message I am getting:

Internal Server Error

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

Please contact the server administrator, [email protected] 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 [email protected]

    DocumentRoot /usr/local/www/myapp/public

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

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

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

</VirtualHost>

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

[Fri Jan 20 15:31:03 2012] [error] [client 192.168.201.123] 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 192.168.201.123] mod_wsgi (pid=1725): Exception occurred processing WSGI script '/usr/local/www/myapp/myapp.wsgi'.
[Fri Jan 20 15:31:03 2012] [error] [client 192.168.201.123] Traceback (most recent call last):
[Fri Jan 20 15:31:03 2012] [error] [client 192.168.201.123]   File "/usr/local/www/myapp/myapp.wsgi", line 3, in <module>
[Fri Jan 20 15:31:03 2012] [error] [client 192.168.201.123]     import layout
[Fri Jan 20 15:31:03 2012] [error] [client 192.168.201.123] ImportError: No module named layout

Output of print sys.path:

enter image description here

Upvotes: 6

Views: 11495

Answers (5)

SmilingSun
SmilingSun

Reputation: 515

According to http://webpy.org/install#apachemodwsgi

If you get an "ImportError: No module named web" in your apache error.log file, you could try setting the absolute path in code.py before importing web:

import sys, os
abspath = os.path.dirname(__file__)
sys.path.append(abspath)
os.chdir(abspath)
import web

It works for me dramatically.

Upvotes: 0

Gerry
Gerry

Reputation: 11214

I had a similar issue and this solved it:

chmod a+x myapp.wsgi

Upvotes: 0

Meitham
Meitham

Reputation: 9680

modules directory also needs a __init__.py file to be defined as a package.

Upvotes: 0

Michał Šrajer
Michał Šrajer

Reputation: 31182

First try:

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

Does it load correctly?

If yes, then probably you have some environment (in ~/.bashrc or such) which is needed for your app. Try::

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

Verify you use same python in your shell as the one used by apache WSGI.

If your myapp.wsgi need any extra env to load correctly, then you can do one of:

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

To set in in your WSGI code, here is 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:
    sys.path.append(EXTRA_DIR)

Put in in the beginning of your myapp.wsgi file.

Upvotes: 8

aganders3
aganders3

Reputation: 5955

You have __init.__py in your layout folder, but it should be __init__.py. The period is misplaced. I am not sure if this is a typo in your post or not, but if that's what your file looks like it would cause this problem.

Upvotes: 1

Related Questions