Aziz
Aziz

Reputation: 21

Flask Error in Python: "Could not import webapp"

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

Answers (6)

BeginnerRP
BeginnerRP

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

Tom Saleeba
Tom Saleeba

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

blastoise
blastoise

Reputation: 284

The error is because the documentation is incorrect in the VS Code tutorial for Flask.

  1. To correct this error, set 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
        }
    ]
}

  1. In terminal, run export set FLASK_APP=hello_app.webapp instead of export set FLASK_APP=webapp as mentioned in the tutorial.
  2. Finally, run python3 -m flask run

Upvotes: 0

sahasrara62
sahasrara62

Reputation: 11228

this error is because you set FLASK_APP environmental variable and your script name is different.

I suggest you to:

  1. change your script name (the file where you have to write given code )to webapp.py

  2. 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

D. Tulloch
D. Tulloch

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

Alterlife
Alterlife

Reputation: 6625

Your FLASK_APP variable is probably not correctly configured. Could you make sure that:

  1. Your file (the one with this code) is named webapp.py OR
  2. alternatively, that your FLASK_APP variable (however you set it) is set to the filename of the file containing this code.

Upvotes: 2

Related Questions