Reputation: 21
I am trying to run flask, however, whenever I type in [flask run] it gives me an error: Could not import webapp
. For reference, I am using Visual Studio Code and am running the following code:
from flask import Flask
app = Flask(__name__)
@app.route("/")
def index():
return "Hello, world!"
I get the following error message:
* Serving Flask app "webapp"
* 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 "webapp".
Upvotes: 0
Views: 6494
Reputation: 61
Make sure when you run the app, check your working directory/base folder.
(base) D:\Pyl\GitHubRepo\FlaskApp\FlaskApp>flask run
The same folder (D:\Pyl\GitHubRepo\FlaskApp\FlaskApp)
should contain app.py file
app.py
file is the script file you want to run.
If it does not work: do this set FLASK_APP=app
(In windows)
here 'app' is your script file name without py extension.
Also, if you change script name, you need to reset the FLASK_APP variable.
Upvotes: 0
Reputation: 4217
Forget the flask command line launcher and do as this answer says:
Add these lines to your python script:
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/
For you, it looks like you named your file webapp.py
so you'd run:
python webapp.py
Upvotes: 0
Reputation: 284
The error is because the documentation is incorrect in the VS Code tutorial for Flask.
FLASK_APP
to hello_app.webapp
in the tutorial:{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Flask",
"type": "python",
"request": "launch",
"module": "flask",
"env": {
"FLASK_APP": "hello_app.webapp"
},
"args": [
"run",
"--no-debugger",
"--no-reload"
],
"jinja": true
}
]
}
export set FLASK_APP=hello_app.webapp
instead of export set FLASK_APP=webapp
as mentioned in the tutorial.python3 -m flask run
Upvotes: 0
Reputation: 11228
this error is because you set FLASK_APP
environmental variable and your script name is different.
I suggest you to:
change your script name (the file where you have to write given code )to webapp.py
set environment variable FLASK_APP
to your script name, ie
in windows set FLASK_APP=<script-name>
or in ubuntu(Linux) export FLASK_APP=<script-name>
Upvotes: 0
Reputation: 21
If you’re setting the FLASK_ENV variable using a .flaskenv or simply using the shell, make sure you don’t have any spelling errors. It’s a mistake that’s so simple it often gets overlooked.
Upvotes: 0
Reputation: 6625
Your FLASK_APP variable is probably not correctly configured. Could you make sure that:
Upvotes: 2