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 director开发者_开发百科y 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.
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
The other answers focus on getting the script itself to mangle its own PYTHONPATH. Another approach is to figure out the right Apache settings that will setup a workable path for Python and WSGI apps.
I have these in my conf file:
PassEnv PYTHONPATH
WSGIPythonHome C:/Python/Python26
WSGIPythonPath C:/Python/Python26;C:/myproject/PyLib
If you don't want to pass your environment's PYTHONPATH, I think you can use:
SetEnv PYTHONPATH C:/your/paths/go/here;C:/and/here
I suggest you give these a shot.
sys.path
and sys.modules
to check whether the directory is actually added as a module directory. If not sys.path.append
it.
精彩评论