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:
virtualenv venv
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
?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With