Python Flask Virtual Environment Create
Flask is a web framework that provides libraries to build lightweight web applications in python.
Install Virtual Environment:
-
virtualenv is considered as the virtual python environment builder which is used to create the multiple python virtual environment side by side.
-
At first open
Visual Studio Codethen use following command to install virtual environment:pip install virtualenv
-
Once it is installed, we can create the new virtual environment into a folder as given below.
python -m venv env
-
To activate the corresponding environment, use the following command on the Windows operating system.
.\env\Scripts\activate
-
If your
pipis not upgraded then upgrade thepip:python -m pip install --upgrade pip
-
Now install the flask by using the following command:
pip install flask
-
After installation now create a first Flask application. Create this application outside the
envfolder.Like-app.py, you can use any types of name. Write the following lines of code to check the application.
from flask import Flask
app = Flask (__name__)
@app.route("/")
def home():
return "Hello world"
if __name__ == "__main__":
app.run(debug=True) - For run this code
Select InterpreterpressCtrl+Shipt+P. Then selectenvinterpreter. Like this type-Python 3.10.10('env':venv).\env\Script\python.exe - Let's run this python code using this command:
python -m flask run
- To show this output on web browser
ctrl+clickthe following lines in your terminal:Running on http://127.0.0.1:5000