Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: Could not locate a Flask application in VSCode

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: venv\Scripts\activate (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:\Users\kashy\OneDrive\Desktop\Flask\Flask_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?

like image 405
Junkrat Avatar asked Oct 10 '19 09:10

Junkrat


People also ask

How do I locate a flask application?

For the flask script to work, an application needs to be discovered. This is achieved by exporting the FLASK_APP environment variable. It can be either set to an import path or to a filename of a Python module that contains a Flask application.


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

like image 162
arunp9294 Avatar answered Sep 30 '22 14:09

arunp9294