Python and Flask Virtual Environment Setup
This tutorial will explain how to set up your Python development environment using Flask.
Q: What is Flask Python?
Ans:
A Python module that makes it simple to create web applications is called Flask. Its core is compact and simple to extend; it's a microframework that has lots of features, such as url routing and a template engine.
Install Python
Install the Python on Windows from https://www.python.org/downloads/
Verify Python Version
Check the Python version using below command:
D:\PythonCrudExample> python --version
Python 3.10.5
Editor to use
There are various editor available which can be use for Python, some of them are listed below:
- Simple-to-install and free text editor that is available across all platforms is Visual Studio Code.
- A complete Integrated Development Environment (IDE) for Python is called PyCharm.
To separate our project dependencies, we will use python virtual environments, and pip to handle package management.
What is virtual environment in Python?
A virtual environment virtualenv
is used to create independent, separate virtual
environments with their own
dependencies for multiple project. It generates a new folder for the new virtual environment which
has its own libraries folder, pip, and
Python interpreter for the Python version used to activate the environment.
Create a new virtual environment
- To create a new virtual environment, use the
virtualenv
command. - Give new environment a name.
- Each operating system has a different set of instructions for activating a new virtual environment.
- We have given virtual environment name as
crudProject
.
D:\PythonCrudExample> python -m venv crudProject
Select Python Interpreter
From PyCharm IDE, select the Python Interpreter for our project from the right corner option.
Use new virtual environment for the python project
Use created virtual environment for the project using virtualenv crudProject
command.
D:\PythonCrudExample> virtualenv crudProject
created virtual environment CPython3.10.5.final.0-64 in 17254ms
creator CPython3Windows(dest=D:\PythonCrudExample\crudProject, clear=False, no_vcs_ignore=False, global=False)
seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=C:\AppData\Local\pypa\virtualenv)
added seed packages: pip==23.0, setuptools==67.1.0, wheel==0.38.4
activators BashActivator,BatchActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator
If a user encounters below error when attempting to use a virtual environment that was created for a
project using the virtualenv crudProject
command, follow the instructions provided here to fix the
issue.
D:\PythonCrudExample> virtualenv crudProject
'virtualenv' is not recognized as an internal or external command,
operable program or batch file.
Activate your new created virtualenv
Activate the newly created virtual environment crudProject
. When working on this Python
project, must activate this environment.
Activate using crudProject/Scripts/activate
command.
D:\PythonCrudExample> crudProject/Scripts/activate
We can see in the terminal that the project is pointing to our newly created environment.
D:\PythonCrudExample> crudProject/Scripts/activate
(crudProject) D:\PythonCrudExample>
Install Flask
Install flask in the newly created virtual environment using pip install flask
command.
(crudProject) D:\PythonCrudExample> pip install flask
Collecting flask
Using cached Flask-2.2.3-py3-none-any.whl (101 kB)
Collecting Werkzeug>=2.2.2
Using cached Werkzeug-2.2.3-py3-none-any.whl (233 kB)
Collecting itsdangerous>=2.0
Using cached itsdangerous-2.1.2-py3-none-any.whl (15 kB)
Collecting Jinja2>=3.0
Using cached Jinja2-3.1.2-py3-none-any.whl (133 kB)
Collecting click>=8.0
Using cached click-8.1.3-py3-none-any.whl (96 kB)
Collecting colorama
Using cached colorama-0.4.6-py2.py3-none-any.whl (25 kB)
Collecting MarkupSafe>=2.0
Using cached MarkupSafe-2.1.2-cp310-cp310-win_amd64.whl (16 kB)
Installing collected packages: MarkupSafe, itsdangerous, colorama, Werkzeug, Jinja2, click, flask
Successfully installed Jinja2-3.1.2 MarkupSafe-2.1.2 Werkzeug-2.2.3 click-8.1.3 colorama-0.4.6 flask-2.2.3 itsdangerous-2.1.2
[notice] A new release of pip is available: 23.0 -> 23.0.1
[notice] To update, run: python.exe -m pip install --upgrade pip
Test Python development virtual environment
Create a new file called app.py
in the python project and print the simple text using
below code.
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "TechGeekNext - Python Example"
if __name__ == "__main__":
app.run()
Test the newly created flask example app.py
using python app.py
command.
(crudProject) PS D:\PythonCrudExample> python app.py
* Serving Flask app 'app'
* Debug mode: off
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
* Running on http://127.0.0.1:5000
Press CTRL+C to quit