Setting Python Path in Windows XAMPP using WSGI
我正在Webfaction上设置实时服务器的开发版本,在本地计算机XP上的虚拟Apache服务器环境中运行Django应用程序(无任何错误运行),在Python 2.6上运行XAMPP Lite,可以从中提交更改通过Git。
XAMPP已启动并在Python上运行正常,并且服务器在加载WSGI模块后完美启动。问题是当我设置Python路径时,它们的一半设置为'nix格式(带有/),而一半设置为Windows(带有反斜杠)。
这是本地计算机Apache错误,显示损坏的python路径:
1 2 3 4 5 6 7 | [Fri Oct 08 14:52:53 2010] [error] [client 127.0.0.1] mod_wsgi (pid=1436): Exception occurred processing WSGI script 'C:/SERVER/Python26/Lib/site-packages/website-cms/webapps/django/dev.wsgi'. [Fri Oct 08 14:52:53 2010] [error] [client 127.0.0.1] Traceback (most recent call last): [Fri Oct 08 14:52:53 2010] [error] [client 127.0.0.1] File"C:/SERVER/Python26/Lib/site-packages/website-cms/webapps/django/lib/python2.5\\django\\core\\handlers\\wsgi.py", line 230, in __call__ [Fri Oct 08 14:52:53 2010] [error] [client 127.0.0.1] self.load_middleware() [Fri Oct 08 14:52:53 2010] [error] [client 127.0.0.1] File"C:/SERVER/Python26/Lib/site-packages/website-cms/webapps/django/lib/python2.5\\django\\core\\handlers\\base.py", line 42, in load_middleware [Fri Oct 08 14:52:53 2010] [error] [client 127.0.0.1] raise exceptions.ImproperlyConfigured('Error importing middleware %s:"%s"' % (mw_module, e)) [Fri Oct 08 14:52:53 2010] [error] [client 127.0.0.1] ImproperlyConfigured: Error importing middleware cms.middleware.multilingual:"No module named cms.middleware.multilingual" |
以及令人讨厌的.wsgi文件内容:
1 2 3 4 5 6 7 8 9 10 11 | import os, sys sys.path.append('C:/SERVER/Python26/') sys.path.append('C:/SERVER/Python26/Lib/site-packages/website-cms/webapps/django') sys.path.append('C:/SERVER/Python26/Lib/site-packages/website-cms/webapps/django/lib/python2.5') from django.core.handlers.wsgi import WSGIHandler #Add the path to Django itself os.environ['DJANGO_SETTINGS_MODULE'] = 'website.settings' application = WSGIHandler() |
Apache httpd.conf是XAMPP(而非虚拟实例)的默认设置,并添加了以下内容以加载wsgi模块
1 | LoadModule wsgi_module modules/mod_wsgi-win32-ap22py26-3.3.so |
&指向wsgi文件:
1 | WSGIScriptAlias / C:/SERVER/Python26/Lib/site-packages/website-cms/webapps/django/dev.wsgi |
我知道XAMPP服务器使用的是Python2.6(我不得不使用TortoiseGIT),生产版本为2.5(由Web主机托管),但这似乎不是罪魁祸首-我仍然希望至少能够设置正确的路径!
欢迎获得有关使用Python玩球的所有建议!
我的电脑具有Python 2.6,因此我将以Python 2.6为目标版本的情况下使用所有配置。
如有必要,请参阅文档-http://code.google.com/p/modwsgi/wiki/InstallationOnWindows
- LoadModule wsgi_module模块/mod_wsgi.so
为了进行测试,我执行了以下步骤。
test.wsgi如下。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | #!/usr/bin/env python """ A simple WSGI test application. Its main purpose is to show that WSGI support works (meaning that the web server and the WSGI adaptor / support module are configured correctly). As a nice plus, it outputs some interesting system / WSGI values as a nice HTML table. The main use of this script will be using the WSGI"application" defined below within your production WSGI environment. You will use some code similar to what you see at the end of this script to use the application from that environment. For the special case of apache2/mod_wsgi, it shoud be possible to directly use this file. If you start this script from the commandline either with python2.5 or with and older python + wsgiref module installed, it will serve the content on http://localhost:8000/ - this is mainly for debugging THIS script. @copyright: 2008 by MoinMoin:ThomasWaldmann @license: Python License, see LICENSE.Python for details. """ import os.path import os import sys try: __file__ except NameError: __file__ = '?' html_template ="""\ <html> <head> WSGI Test Script </head> <body> WSGI test script is working! <table border=1> <tr><th colspan=2>1. System Information</th></tr> <tr><td>Python</td><td>%(python_version)s</td></tr> <tr><td>Python Path</td><td>%(python_path)s</td></tr> <tr><td>Platform</td><td>%(platform)s</td></tr> <tr><td>Absolute path of this script</td><td>%(abs_path)s</td></tr> <tr><td>Filename</td><td>%(filename)s</td></tr> <tr><th colspan=2>2. WSGI Environment</th></tr> %(wsgi_env)s </table> </body> </html> """ row_template =" <tr><td>%s</td><td>%r</td></tr>" def application(environ, start_response): mysite = '/Users/smcho/Desktop/django' if mysite not in sys.path: sys.path.insert(0,'/Users/smcho/Desktop/django') mysite = '/Users/smcho/Desktop/django/mysite' if mysite not in sys.path: sys.path.insert(0,'/Users/smcho/Desktop/django/mysite') """ The WSGI test application""" # emit status / headers status ="200 OK" headers = [('Content-Type', 'text/html'), ] start_response(status, headers) # assemble and return content content = html_template % { 'python_version': sys.version, 'platform': sys.platform, 'abs_path': os.path.abspath('.'), 'filename': __file__, 'python_path': repr(sys.path), 'wsgi_env': ' '.join([row_template % item for item in environ.items()]), } return [content] if __name__ == '__main__': # this runs when script is started directly from commandline try: # create a simple WSGI server and run the application from wsgiref import simple_server print"Running test application - point your browser at http://localhost:8000/ ..." httpd = simple_server.WSGIServer(('', 8000), simple_server.WSGIRequestHandler) httpd.set_app(application) httpd.serve_forever() except ImportError: # wsgiref not installed, just output html to stdout for content in application({}, lambda status, headers: None): print content |
这是代码
1 2 3 4 5 6 7 8 9 10 11 | <Directory"C:/xampp/htdocs/wsgi/scripts"> Options ExecCGI Indexes AddHandler cgi-script .cgi AddHandler wsgi-script .wsgi Order allow,deny Allow from all </Directory> Alias /wsgi/"C:/xampp/htdocs/wsgi/scripts/" <IfModule wsgi_module> WSGIScriptAlias /test"C:/xampp/htdocs/wsgi/scripts/test.wsgi" </IfModule> |
我也遇到了"服务器错误",并在Apache error.log文件中查看:这是由于注释行"它的主要目的是……"引起的空格或换行符