问题
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