首页 文章

使用WSGI将Flask应用程序部署到Apache时出错 - ImportError:没有名为MySQLdb的模块

提问于
浏览
1

好吧,我已经配置了我的Apache,MySql和Python . 安装了mod_wsgi并在我的系统上运行了一个示例.wsgi "Hello World" application . 我的Flask应用程序也运行使用Flask的web服务器在我的virtualenv内部和外部执行 python myServer.py ,它也配置得很好(安装了flask和mysql-python) .

现在我正在尝试部署我的烧瓶应用程序,我从页面中收到以下错误:

Server error!

The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there was an error in a CGI script.

If you think this is a server error, please contact the webmaster.

Error 500

我从Apache转到错误日志,我得到了这个:

[Mon Jul 14 19:27:50.692271 2014] [wsgi:error] [pid 56887] [remote ::1:0] mod_wsgi (pid=56887): Target WSGI script '/Applications/XAMPP/htdocs/nudgeAlong/wsgi/myServer.wsgi' cannot be loaded as Python module.
[Mon Jul 14 19:27:50.692387 2014] [wsgi:error] [pid 56887] [remote ::1:0] mod_wsgi (pid=56887): Exception occurred processing WSGI script '/Applications/XAMPP/htdocs/nudgeAlong/wsgi/myServer.wsgi'.
[Mon Jul 14 19:27:50.692411 2014] [wsgi:error] [pid 56887] [remote ::1:0] Traceback (most recent call last):
[Mon Jul 14 19:27:50.692438 2014] [wsgi:error] [pid 56887] [remote ::1:0]   File "/Applications/XAMPP/htdocs/nudgeAlong/wsgi/myServer.wsgi", line 4, in <module>
[Mon Jul 14 19:27:50.692535 2014] [wsgi:error] [pid 56887] [remote ::1:0]     from myServer import app as application
[Mon Jul 14 19:27:50.692553 2014] [wsgi:error] [pid 56887] [remote ::1:0]   File "/Applications/xampp/xamppfiles/htdocs/nudgeAlong/myServer.py", line 2, in <module>
[Mon Jul 14 19:27:50.692633 2014] [wsgi:error] [pid 56887] [remote ::1:0]     import MySQLdb
[Mon Jul 14 19:27:50.692658 2014] [wsgi:error] [pid 56887] [remote ::1:0] ImportError: No module named MySQLdb

我的Wsgi应用程序在这里:

import sys
activate_this = '/Applications/XAMPP/htdocs/nudgeAlong/venv/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))

sys.path.append('/Applications/xampp/xamppfiles/htdocs/nudgeAlong/')

from myServer import app as application

我的httpd.conf虚拟主机部分是:

<VirtualHost *>
    ServerName MeuServer
    WSGIDaemonProcess myServer threads=5
    WSGIScriptAlias /myapp /Applications/XAMPP/htdocs/nudgeAlong/wsgi/myServer.wsgi

    <Directory /Applications/XAMPP/htdocs/nudgeAlong/wsgi>
      WSGIProcessGroup myServer
      WSGIApplicationGroup %{GLOBAL}
      Require all granted
    </Directory>
</VirtualHost>

我的文件夹结构如下:

__mainfolder

____myServer.py

____ WSGI /

________ myServer.wsgi

____ VENV /

希望有人可以提供帮助关于这种情况的奇怪之处在于错误发生在我的程序的第2行 import MySQLdb 行,但在此之前我导入了Flask并且没有发生错误,所以我可能从我的系统中获取库...

要完成,我的Flask应用程序如果需要:

from flask import Flask, jsonify, render_template, request, make_response, request, current_app
import MySQLdb
from datetime import timedelta
app = Flask(__name__)
db_owen = MySQLdb.connect("localhost","root","","climote_trial")


@app.route('/get_json')
@crossdomain(origin='*')
def get_json():
    results = []
    c = db_owen.cursor()
    c.execute("select date,message_log from trialresults")
    jsonresult = c.fetchall()
    for x in jsonresult:
        dic = {'col1':str(x[0]),'col2':x[1] }
        results.append(dic)

    return jsonify({ 'results':results });

@app.route('/')
def index():
    return render_template('number_1.html')


if __name__ == '__main__':
    app.run(debug= True, port=53000)

1 回答

  • 1

    SOLVED :如果我拥有自己的MySql,任何人都会纠正我 . 一切都发生了冲突,我试图以多种方式解决它,但都没有奏效 . 我从中学到的两件事是你必须决定是否要使用像MAMP,XAMPP这样的东西,或者自己安装网络服务器,数据库和驱动程序的基本方法 . 另一件重要的事情是那三个 - 网络服务器,数据库和编程语言解释器 - 必须具有相同的架构(32或64位) .

    最后我决定重新安装一切,这是我使用Mac OS X Mavericks的程序:

    • Cleaning the mess :我删除了XAMPP(在删除之前小心移动应用程序文件,我几乎丢失了所有代码)

    • 在此之后卸载了MySQL link

    • 卸载mysql-python: sudo pip uninstall mysql-python

    • 安装MySql翻新我的符号链接

    sudo rm /usr/lib/libmysqlclient.18.dylib sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib

    • 已安装mysql-python(注意设置正确的路径)

    export PATH = $ PATH:/ usr / local / mysql / bin sudo pip install mysql-python

    • 安装了Apache的网站教程

    • 安装mod_wsgi设置Apache和Python解释器的正确路径

    ./configure --with-apxs = / path / to / apache / bin / apxs \ --with-python = / path / to / bin / python

    之后,我在Flask的网络服务器上完成了我的应用程序 . 并使用了我的问题的代码中的相同配置,一切正常 .

相关问题