【问题标题】:PythonAnywhere and Flask app keep returning error codePythonAnywhere 和 Flask 应用程序不断返回错误代码
【发布时间】:2020-10-03 04:27:25
【问题描述】:

尝试在 PythonAnywhere 上托管我的烧瓶应用程序 (run.py)。让我的 virtualenv 设置和我的所有模块都通过 pip 导入。烧瓶应用程序存储在此位置:

/home/goldsilvermonitor/GSM/run.py

设置我的 WSGI 文件,它一直给我错误:

TypeError: 'module' object is not callable

我的烧瓶文件如下所示:(run.py)

    from flask import Flask, flash, redirect, render_template, request, session, abort, url_for
app = Flask(__name__)

# ./Home Script:
@app.route("/")
@app.route("/index")
def index():
    return render_template('index.html')

# ./Disclaimer Page:
@app.route("/disclaimer")
def disclaimer():
    return render_template('disclaimer.html')

# ./data.xml:
app.route("/dataxml")
def dataxml():
    return render_template('data.xml')

# ./404 Page
@app.errorhandler(404)
def page_not_found(e):
    # 404 status set explicitly
    return render_template('404.html'), 404

# FLask Debug Script:s
if __name__ == "__main__":
    app.run(host="0.0.0.0", port='5000', debug=True)

我的 WSGI 文件如下所示:

        # +++++++++++ FLASK +++++++++++
# Flask works like any other WSGI-compatible framework, we just need
# to import the application.  Often Flask apps are called "app" so we
# may need to rename it during the import:
#
#
import sys
#
## The "/home/goldsilvermonitor" below specifies your home
## directory -- the rest should be the directory you uploaded your Flask
## code to underneath the home directory.  So if you just ran
## "git clone git@github.com/myusername/myproject.git"
## ...or uploaded files to the directory "myproject", then you should
## specify "/home/goldsilvermonitor/myproject"
path = '/home/goldsilvermonitor/GSM'
if path not in sys.path:
    sys.path.append(path)
#
import run as application  # noqa
#
# NB -- many Flask guides suggest you use a file called run.py; that's
# not necessary on PythonAnywhere.  And you should make sure your code
# does *not* invoke the flask development server with app.run(), as it
# will prevent your wsgi file from working.

我不知道是什么导致了这个错误。已尝试重新上传文件,重做 WSGI 配置。但无济于事。如果有人可以帮助我,那就太好了!我还应该在上线之前从烧瓶文件中删除 debug=true 吗?

【问题讨论】:

    标签: python hosting wsgi pythonanywhere


    【解决方案1】:

    您正在尝试导入一个模块(文件run.py),然后将其用作应用程序;应用程序是该文件中的 app 对象,因此在 WSGI 文件中您应该替换它:

    import run as application  # noqa
    

    ...用这个:

    from run import app as application  # noqa
    

    【讨论】:

      猜你喜欢
      • 2015-12-23
      • 1970-01-01
      • 2012-06-12
      • 1970-01-01
      • 1970-01-01
      • 2018-12-08
      • 1970-01-01
      • 1970-01-01
      • 2021-07-27
      相关资源
      最近更新 更多