Virtual Environment in Python
A virtual environment is an isolated environment for a Python project.
It allows each project to have its own Python packages and dependencies without affecting other projects.
1. Why Use a Virtual Environment?
Suppose you have two projects:
Project A → Django 4 Project B → Django 5
Installing both globally can cause version conflicts.
With virtual environments:
Project A └── Virtual Environment └── Django 4 Project B └── Virtual Environment └── Django 5
Each project can use its own package versions.
2. Creating a Virtual Environment
First, open your terminal inside the project folder.
python -m venv venv
If your system uses py:
py -m venv venv
Here:
python / py → Python interpreter -m venv → use Python's venv module venv → name of the environment
You can use another name:
python -m venv myenv
But venv is a common choice.
3. Virtual Environment Folder
After creating it:
myproject/ │ ├── venv/ │ └── main.py
The venv folder contains the isolated environment.
You normally do not edit files inside venv manually.
4. Activating on Windows
Command Prompt
venv\Scripts\activate
PowerShell
venv\Scripts\Activate.ps1
After activation, you may see:
(venv) C:\myproject>
The (venv) shows that the virtual environment is active.
5. Activating on Linux / macOS
Use:
source venv/bin/activate
After activation:
(venv) user@computer:~/myproject$
6. Installing Packages
After activating the environment:
pip install requests
The package will be installed inside the virtual environment.
For example:
pip install flask
7. Checking Installed Packages
pip list
Example:
Package Version ---------- ------- Flask 3.x.x requests 2.x.x
Only packages installed in the active environment are shown.
8. Checking Python
After activating:
python --version
You can also check which Python executable is being used.
Linux/macOS
which python
Windows
where python
It should point to the virtual environment's Python executable.
9. Deactivating
When finished working:
deactivate
The (venv) will disappear from the terminal.
10. Installing Packages from requirements.txt
A project can store its dependencies in:
requirements.txt
Example:
flask requests numpy
Install them with:
pip install -r requirements.txt
11. Creating requirements.txt
After installing your project's packages:
pip freeze > requirements.txt
Example:
Flask==3.x.x requests==2.x.x
Now another developer can recreate the environment using:
pip install -r requirements.txt
12. Virtual Environment Workflow
A typical workflow is:
mkdir myproject cd myproject python -m venv venv # Activate source venv/bin/activate pip install requests pip freeze > requirements.txt
On Windows:
mkdir myproject cd myproject py -m venv venv venv\Scripts\activate pip install requests pip freeze > requirements.txt
13. Deleting a Virtual Environment
A virtual environment is just a folder.
If you no longer need it, you can delete the venv folder.
Linux/macOS
rm -rf venv
Windows
rmdir /s /q venv
You can always create it again:
python -m venv venv
Then reinstall the dependencies:
pip install -r requirements.txt
14. Virtual Environment and Git
You normally should not upload venv to GitHub.
Add this to .gitignore:
venv/
Also commonly:
__pycache__/ *.pyc
Example:
myproject/ │ ├── venv/ ← Don't upload ├── main.py ├── requirements.txt └── .gitignore
15. Virtual Environment vs Global Python
Without virtual environment
Computer │ └── Global Python ├── Flask ├── Django ├── Requests └── Other packages
Different projects may interfere with each other.
With virtual environments
Computer │ ├── Project A │ └── venv │ └── Flask 3 │ ├── Project B │ └── venv │ └── Flask 2 │ └── Project C └── venv └── Django
Each project is isolated.
16. Important Commands
| Task | Command |
|---|---|
| Create environment | python -m venv venv |
| Activate — Windows CMD | venv\Scripts\activate |
| Activate — Windows PowerShell | venv\Scripts\Activate.ps1 |
| Activate — Linux/macOS | source venv/bin/activate |
| Install package | pip install package |
| Install requirements | pip install -r requirements.txt |
| List packages | pip list |
| Save dependencies | pip freeze > requirements.txt |
| Deactivate | deactivate |
Simple Flow
Create Project ↓ Create Virtual Environment ↓ Activate Environment ↓ Install Packages ↓ Write Python Code ↓ Save Requirements ↓ Deactivate
In simple words: A virtual environment gives each Python project its own isolated Python package environment, which prevents dependency conflicts between projects.