Flask程序可以在Spyder中运行/调试吗?

时间:2019-04-11 10:50:42

标签: python flask anaconda spyder

我正在尝试在网上找到的简单的Flask教程。这是代码:

from flask import Flask, jsonify
from flask_cors import CORS

# configuration
DEBUG = True

# instantiate the app
app = Flask(__name__)
app.config.from_object(__name__)

# enable CORS
CORS(app)

# sanity check route
@app.route('/ping', methods=['GET'])
def ping_pong():
    return jsonify('pong!')

if __name__ == '__main__':
    app.run()

Spyder中,如果我运行文件,则可以正常工作。但是,我希望能够在选择语句时进行调试和执行。当我选择全部内容并执行(通过交互式控制台)时,出现以下错误。有办法使这项工作吗?

Serving Flask app "__main__" (lazy loading)
 Environment: production
   WARNING: Do not use the development server in a production environment.
   Use a production WSGI server instead.
 Debug mode: on
 Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
 Restarting with stat
/opt/local/anaconda3/bin/python: can't find '__main__' module in ''
An exception has occurred, use %tb to see the full traceback.

2 个答案:

答案 0 :(得分:2)

出于运行目的,而不是app.run()使用app.run(debug = False)

答案 1 :(得分:0)

您可以在场景下尝试此代码

从werkzeug.debug导入DebuggedApplication

def create_app():     #将您在Flask应用程序工厂中所做的任何事情插入。

if app.debug:
    app.wsgi_app = DebuggedApplication(app.wsgi_app, evalex=True)

return app

这将使您的应用程序调试

有关示例的完整源代码,请检查以下链接 https://github.com/nickjj/build-a-saas-app-with-flask