使用mod_wsgi运行Flask应用程序时出现SQLAlchemy ImportError

时间:2013-10-22 08:03:46

标签: python apache sqlalchemy flask mod-wsgi

我收到了这个错误。任何想法?谢谢!

PS。我也尝试以多种方式降级/升级Flask-SQLAlchemy和SQLAlchemy而没有运气。并尝试在python shell(激活的virtualenv)中导入SQLAlchemy行,并且它可以正确运行而不会出错。

这是我的pip冻结:

Flask==0.10.1
Flask-SQLAlchemy==1.0
Jinja2==2.7.1
MarkupSafe==0.18
MySQL-python==1.2.4
SQLAlchemy==0.9.0dev
Werkzeug==0.9.4
itsdangerous==0.23
wsgiref==0.1.2

Apache error_log:

[Tue Oct 22 12:08:18 2013] [error] [client 10.1.100.54] Traceback (most recent call last):
[Tue Oct 22 12:08:18 2013] [error] [client 10.1.100.54]   File "/var/www/html/hello/hello.wsgi", line 11, in <module>
[Tue Oct 22 12:08:18 2013] [error] [client 10.1.100.54]     from hello import app as application
[Tue Oct 22 12:08:18 2013] [error] [client 10.1.100.54]   File "/var/www/html/hello/hello.py", line 8, in <module>
[Tue Oct 22 12:08:18 2013] [error] [client 10.1.100.54]     from flask.ext.sqlalchemy import SQLAlchemy
[Tue Oct 22 12:08:18 2013] [error] [client 10.1.100.54]   File "/var/www/html/hello/venv/lib/python2.7/site-packages/flask/exthook.py", line 81, in load_module
[Tue Oct 22 12:08:18 2013] [error] [client 10.1.100.54]     reraise(exc_type, exc_value, tb.tb_next)
[Tue Oct 22 12:08:18 2013] [error] [client 10.1.100.54]   File "/var/www/html/hello/venv/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py", line 24, in <module>
[Tue Oct 22 12:08:18 2013] [error] [client 10.1.100.54]     from sqlalchemy import orm, event
[Tue Oct 22 12:08:18 2013] [error] [client 10.1.100.54] ImportError: cannot import name event

hello.wsgi:

activate_this = '/var/www/html/hello/venv/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))
import sys, site
site.addsitedir('/var/www/html/hello/venv/lib/python2.7/site-packages')
sys.path.insert(0, '/var/www/html/hello')
from hello import app as application

hello.py:

from flask import Flask
import time, os
from flask.ext.sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://test:11111@10.1.17.197'
db = SQLAlchemy(app)
...

并为apache虚拟主机文件配置:

WSGISocketPrefix run/wsgi
WSGIPythonPath /var/www/html/hello:/var/www/html/hello/venv/lib/python2.7/site-packages

<VirtualHost *>
   ServerName example.com

   WSGIDaemonProcess yourapplication user=apache group=root threads=5
   WSGIScriptAlias /hello /var/www/html/hello/hello.wsgi

   <Directory /var/www/html/hello>
       WSGIProcessGroup yourapplication
       WSGIApplicationGroup %{GLOBAL}
       Order deny,allow
       Allow from all
   </Directory>
</VirtualHost>

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,使用以下代码解决了这个问题:

ALLDIRS = ['/var/www/MarkerDB/']

import sys 
import site 

# Remember original sys.path.
prev_sys_path = list(sys.path) 

# Add each new site-packages directory.
for directory in ALLDIRS:
  site.addsitedir(directory)

# Reorder sys.path so new directories at the front.
new_sys_path = [] 
for item in list(sys.path): 
    if item not in prev_sys_path: 
        new_sys_path.append(item) 
        sys.path.remove(item) 
sys.path[:0] = new_sys_pat

来源:http://code.google.com/p/modwsgi/wiki/VirtualEnvironments#Application_Environments