A virtual environment is an isolated workspace for a Python project. It allows you to install project-specific packages without affecting other Python projects on your system. Using a virtual environment helps manage dependencies and multiple projects on a machine.
Create a Virtual Environment
python -m venv venv
Here, venv is the name of the virtual environment folder.
Activate the Virtual Environment
Windows
venv\Scripts\activate
macOS/Linux
source venv/bin/activate
After activation, the terminal will display the environment name, such as:
(venv)
Install Django
pip install django
Verify Installation
django-admin --version
Deactivate the Virtual Environment
deactivate
Using virtual environments is considered a best practice for all Django projects because it keeps dependencies organized and project setups consistent.