Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load module into apache + mod-wsgi

I am using Apache + mod-wsgi.

In my httpd.conf, I am having the following additional lines at the end of file.

LoadModule wsgi_module modules/mod_wsgi-win32-ap22py27-3.3.so
WSGIScriptAlias / "C:/Projects/Folder/web/"
<Directory "C:/Projects/Folder/web">
AllowOverride None
Options None
Order deny,allow
Allow from all
</Directory>

When I execute the following index.py scripts in Windows through http://localhost/script/index.py

def application(environ, start_response):
    status = '200 OK' 
    output = 'Hello World!'
    response_headers = [('Content-type', 'text/plain'),
                        ('Content-Length', str(len(output)))]
    start_response(status, response_headers)
    return [output]

Works pretty fine.

However, when I add import utils at the first line of index.py, I get

ImportError: No module named utils

utils.py is same directory as index.py

Is there any additional configuration I need to set?

I try suggestion given by @dan_waterworth

import sys, os
sys.path.append(os.path.dirname(__file__))

I get no more error by importing my own module. However, when I import module which is being installed through easy_install, error happens.

   File "C:/Projects/Folder/web/script\\connection.py", line 1, in <module>
     import psycopg2
   File "build\\bdist.win32\\egg\\psycopg2\\__init__.py", line 65, in <module>
     from psycopg2 import tz
 ImportError: cannot import name tz

import psycopg2 executed no problem, if this script is being executed as standalone application.

like image 285
Cheok Yan Cheng Avatar asked Nov 12 '10 07:11

Cheok Yan Cheng


1 Answers

I find that I have to add a few lines to append the python path. Something like:

import sys, os
sys.path.append(os.path.dirname(__file__))

import utils

for the second part, just add additional lines for your import directories. ie:

sys.path.append([enter path here])

to find your import directories, type into an interactive python prompt:

import sys
print sys.path
like image 63
dan_waterworth Avatar answered Sep 24 '22 20:09

dan_waterworth