Error: Could not locate a Flask application in VSCode

Question:

I am trying to learn Flask using VScode.

The tutorial that I am following is: Python Flask Tutorial: Full-Featured Web App Part 1 – Getting Started.

I did the following things:

  1. Created a new virtualenv in a folder using: virtualenv venv
  2. activated it as: venvScriptsactivate (I am on Windows 10)

After that, I created a new directory named Flask_Blog using mkdir Flask_Blog and in it, I created a new flaskblog.py file containing the following code:

from flask import Flask


app = Flask(__name__)


@app.route('/')
def hello():
    return 'Hello'

Then, in the terminal of VScode, I changed my working directory in order to be in the Flask_Blog directory using cd Flask_Blog.

Now, when I am doing set FLASK_APP=flaskblog.py followed by flask run, I am getting the following error:

(venv) PS C:UserskashyOneDriveDesktopFlaskFlask_Blog> flask run
 * 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 locate a Flask application. You did not provide the "FLASK_APP" environment variable, and a "wsgi.py" or "app.py" module was not found in the current directory.

But

When I do the same in the cmd prompt, the code runs and I get to see the output.

I am completely new to this. Can anyone please tell me what is the mistake I am doing in VSCode and why is it working in the cmd?

Asked By: some_programmer

||

Answers:

Issue raised in VsCode

Under Powershell, you have to set the FLASK_APP environment variable as follows:

$env:FLASK_APP = "webapp"

Then you should be able to run “python -m flask run” inside the hello_app folder. In other words, PowerShell manages environment variables differently, so the standard command-line “set FLASK_APP=webapp” won’t work.

Answered By: arunp9294

Try Set FLASK_APP = Full path of the folder/filename.py.

This worked for me

Answered By: deepak

This worked for me on the VSCode:

$env:FLASK_APP= ‘C:Pythonex003main:app’

Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.