【问题标题】:Error deploying Flask Application to Apache using WSGI - ImportError: No module named MySQLdb使用 WSGI 将 Flask 应用程序部署到 Apache 时出错 - ImportError: No module named MySQLdb
【发布时间】:2014-09-04 18:58:24
【问题描述】:

嗯,我已经配置了我的 Apache、MySql 和 Python。安装了 mod_wsgi 并在我的系统上运行了一个示例 .wsgi “Hello World”应用程序。我的 Flask 应用程序也使用 Flask 的网络服务器在我的 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>

我的文件夹结构如下:

__主文件夹

____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)

【问题讨论】:

  • 我在数据库访问部分评论了我的代码,并尝试运行应用程序并且它正在运行,此后 Flask 正在运行。问题出在数据库连接上,在 apache 和 python-mysql 驱动程序之间。
  • virtualenv 激活时pip freeze 的输出是什么?
  • Flask==0.10.1 Jinja2==2.7.3 MarkupSafe==0.23 MySQL-python==1.2.5 Werkzeug==0.9.6 itsdangerous==0.24 wsgiref==0.1.2跨度>

标签: mysql apache flask mod-wsgi


【解决方案1】:

已解决: 如果我错了,任何人都可以纠正我。我相信问题在于,当你安装 mysql-python 时,你会用你的 Python 解释器“连接”你的内部 MySql 文件。所以,当我安装它时,它将我的 MySql 连接到我的 Python。之后我安装了 XAMPP,它有自己的 MySql。一切都发生了冲突,我试图以多种方式解决它,但都没有奏效。我从中学到的两件事是,您必须决定是要使用 MAMP、XAMPP 之类的东西,还是自己安装网络服务器、数据库和驱动程序的基本方式。另一件重要的事情是这三者——网络服务器、数据库和编程语言解释器——必须具有相同的架构(32 位或 64 位)。

最后我决定重新安装所有东西,这是我使用 Mac OS X Mavericks 的过程:

  1. 收拾残局:我删除了 XAMPP(删除之前请注意移动应用程序文件,我几乎丢失了所有代码)
  2. 在link之后卸载了MySQL
  3. 卸载mysql-python:sudo pip卸载mysql-python
  4. 安装 MySql 更新我的符号链接

    sudo rm /usr/lib/libmysqlclient.18.dylib

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

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

    导出 PATH=$PATH:/usr/local/mysql/bin

    sudo pip install mysql-python

  6. 按照网站教程安装 Apache

  7. 安装的 mod_wsgi 设置了 Apache 和我的 Python 解释器的正确路径

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

    之后,我让我的应用程序在 Flask 的网络服务器中运行。并使用了与我的问题代码中相同的配置,一切正常。

【讨论】:

    猜你喜欢
    • 2016-12-23
    • 2016-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-11
    • 2015-11-11
    • 2020-02-01
    • 2018-12-16
    相关资源
    最近更新 更多