Running Flask on bash: Could not import “app”

旧城冷巷雨未停 提交于 2021-01-05 07:24:39

问题


I made this code on Flask:

from flask import Flask

app = Flask(__name__)

@app.route('/')
def index():
    return "hi"

Even so, fi I try to run it with flask run it shows this message:

* Serving Flask app "app.py"
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
Usage: flask run [OPTIONS]

Error: Could not import "app".

What am I doing wrong?

Edit: App structure:

website/
  | - app.py

回答1:


from flask import Flask

app = Flask(__name__)

@app.route('/')
def index():
    return "hi"


if __name__ == '__main__':
  app.run(host='0.0.0.0', port=30006, debug=True)

Run in the same directory where this code is placed.

python3 file_name.py

You can access it on

http://0.0.0.0:30006/




回答2:


If you have configured a virtualenv for your project, you have to activate the virtualenv with this command

>>>env\scripts\activate

Then make sure you are in the same directory where your app.py is located then use this commands

env>>>Set FLASK_APP=app.py 
env>>>Set FLASK_ENVIRONMENT=development
env>>>flask run

keep cracking **note if you are on linux use 'export' instead of 'set'



来源:https://stackoverflow.com/questions/60356031/running-flask-on-bash-could-not-import-app

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!