使用mod_wsgi,apache和sqlalchemy的语法类模板(metaclass = _TemplateMetaclass)无效

时间:2013-04-18 07:59:55

标签: python apache sqlalchemy mod-wsgi bottle

enter image description here我有一个我导入的脚本:

from bottle import route, request
from sqlalchemy import create_engine, MetaData
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from sqlalchemy import Table, Column, Integer
import xml.etree.cElementTree as ET 

@route('/getmember')
def get_member():
    pass
run(host='localhost', port=8080, reloader=True)

我可以使用Web服务器中的瓶子构建成功运行此脚本:$python3.2 getmember.py

这样做我想使用Apache和mod_wsgi运行应用程序并创建一个名为adapter.wsgi的脚本:

import sys, os, bottle
sys.path = ['/var/www/getmember/'] + sys.path
os.chdir(os.path.dirname(__file__))
import getmember
application = bottle.default_app()

在Apache我有:

WSGIDaemonProcess getmember user=www-data group=www-data processes=1 threads=5 python-path=/usr/lib/python3.2/site-packages
WSGIScriptAlias /getmember /var/www/idcheck/adapter.wsgi
<Directory /var/www/getmember>
    WSGIProcessGroup getmember
    WSGIApplicationGroup %{GLOBAL}
    Order deny,allow
    Allow from all
</Directory>

当我使用浏览器localhost/getmember运行此脚本时,我收到HTTP 200错误,Apache错误日志显示:

mod_wsgi (pid=10271): Exception occurred processing WSGI script '/var/www/getmember/adapter.wsgi'.
Traceback (most recent call last):
  File "/var/www/getmember/adapter.wsgi", line 7, in <module>
    import getmember # This loads your application
  File "/var/www/getmember/getmember.py", line 10, in <module>
    from sqlalchemy import create_engine, MetaData
  File "/usr/lib/python3.2/site-packages/SQLAlchemy-0.8.0b2-py3.2.egg/sqlalchemy/__init__.py", line 10, in <module>
    from .sql import (
  File "/usr/lib/python3.2/site-packages/SQLAlchemy-0.8.0b2-py3.2.egg/sqlalchemy/sql/__init__.py", line 7, in <module>
    from .expression import (
  File "/usr/lib/python3.2/site-packages/SQLAlchemy-0.8.0b2-py3.2.egg/sqlalchemy/sql/expression.py", line 34, in <module>
    from .. import util, exc, inspection
  File "/usr/lib/python3.2/site-packages/SQLAlchemy-0.8.0b2-py3.2.egg/sqlalchemy/util/__init__.py", line 7, in <module>
    from .compat import callable, cmp, reduce, defaultdict, py25_dict, \\
  File "/usr/lib/python3.2/site-packages/SQLAlchemy-0.8.0b2-py3.2.egg/sqlalchemy/util/compat.py", line 100, in <module>
    from urllib.parse import parse_qsl
    ImportError: No module named parse

那么为什么它在正常的python调用下运行(意味着所有的库和包都没问题)但是在wsgi&amp; Apache它抛出这个错误?我觉得它无法找到该包的需求路径,但不知道如何添加它。

修改

我运行import sysprint sys.path以获取运行python3.2环境时加载的所有路径。我有多条路径,我在Apache中添加到Python路径:

WSGIDaemonProcess idcheck user=www-data group=www-data processes=1 threads=5 python-path=/usr/local/lib/python3.2/dist-packages/distribute-0.6.34-py3.2.egg:/usr/local/lib/python3.2/dist-packages/logilab_common-0.58.3-py3.2.egg:/usr/local/lib/python3.2/dist-packages/logilab_astng-0.24.1-py3.2.egg:/usr/local/lib/python3.2/dist-packages/SQLAlchemy-0.8.0b2-py3.2.egg:/usr/local/lib/python3.2/dist-packages/psycopg2-2.4.6-py3.2-linux-x86_64.egg:/usr/local/lib/python3.2/dist-packages/pip-1.2.1-py3.2.egg:/usr/local/lib/python3.2/dist-packages/sqlautocode-0.6b1-py3.2.egg:/usr/lib/python3.2:/usr/lib/python3.2/plat-linux2:/usr/lib/python3.2/lib-dynload:/usr/local/lib/python3.2/dist-packages:/usr/lib/python3/dist-packages

有了这个,之前的错误消失了,我得到了一个无效语法的新错误。 SQLAlchemy和mod_wsgi + Apache是​​否有问题?

mod_wsgi (pid=21339): Target WSGI script '/var/www/getmember/adapter.wsgi' cannot be loaded as Python module.
mod_wsgi (pid=21339): Exception occurred processing WSGI script '/var/www/getmember/adapter.wsgi'.
Traceback (most recent call last):
  File "/var/www/getmember/adapter.wsgi", line 6, in <module>
    import getmember # This loads your application
  File "/var/www/getmember/getmember.py", line 10, in <module>
    from sqlalchemy import create_engine, MetaData
  File "/usr/local/lib/python3.2/dist-packages/SQLAlchemy-0.8.0b2-py3.2.egg/sqlalchemy/__init__.py", line 7, in <module>
    import inspect as _inspect
  File "/usr/lib/python3.2/inspect.py", line 36, in <module>
    import string
  File "/usr/lib/python3.2/string.py", line 89
    class Template(metaclass=_TemplateMetaclass):
                            ^
SyntaxError: invalid syntax

1 个答案:

答案 0 :(得分:1)

也许Apache正在使用版本2 Python可执行文件?您可以检查apache用户的搜索路径,或者查看WSGI指令WSGIPythonHome / WSGIPythonExecutable。

尝试:

import sys

-snip -

@route('/test', method='GET')
def test():
    return sys.version

或者:

#grep Python /var/log/httpd/error_log