2

在此处输入图像描述我有一个导入脚本:

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)

我可以使用网络服务器中的瓶子构建成功运行此脚本:$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 sysandprint sys.path来获取运行 python3.2 环境时加载的所有路径。我有多个路径,我在 Apache 中添加到 Python-path:

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
4

1 回答 1

1

也许 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
于 2013-04-18T08:32:29.773 回答