我有一個劇本,我導入:無效語法類模板(元類= _TemplateMetaclass)與mod_wsgi的,apache和SQLAlchemy的
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調用下(意思是所有的庫和包都可以),但在Apache下它會拋出這個錯誤?我感覺它無法找到該軟件包需要的路徑,但不知道如何添加它。
編輯
我跑了import sys
和print sys.path
讓所有當我運行python3.2環境已加載的路徑。我有多個路徑,我加入到了Python路徑中的Apache:
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
我想使用Python3.2它,因爲我在apache2.conf此項:'WSGIDaemonProcess idcheck user = www-data group = www-data processes = 1 threads = 5 python-path =/usr/lib/python3.2/site-packages'?另外,在插入該行之前,它找不到「瓶子」包裝。現在它確實,但現在在SQLAlchemy部分失敗了 – lukik