python - mod_wsgi、apache 和 sqlalchemy 的语法类模板(元类=_TemplateMetaclass)无效

标签 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)

我可以使用网络服务器中的 bottle 构建成功运行此脚本:$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 和 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

最佳答案

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

尝试:

import sys

-剪辑-

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

或者:

#grep Python /var/log/httpd/error_log

关于python - mod_wsgi、apache 和 sqlalchemy 的语法类模板(元类=_TemplateMetaclass)无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16077514/

相关文章:

flask - '没有找到应用程序。要么在 View 函数内工作,要么推送'RuntimeError : No application found

python - 在 Python3 中使用 "not in"比使用 "in"快吗?

python - 按列表中元素的出现次数对列表进行排序

apache http 到 https 将您重定向了太多次

regex - .htaccess 根据条件和参数重定向

python - 如何将 Amazon Redshift 连接到 python

python - 如何在 SQLAlchemy Core 中将列名作为参数传递?

python - 尝试将 .dem 文件转换为 .grid 文件时,Python 出现 DLL 加载失败错误

Python:openpyxl 将字体更改为粗体

apache - 在 apache 代理后面使用 Liferay 时完成 struts 操作后忽略重定向