Python20 min read
Python Virtual Environments
Learn virtual environments with a real workflow: create, activate, install, freeze, share, and avoid dependency conflicts.
Michael Brown
September 4, 2025
6.8k254
A virtual environment (venv) creates an isolated space for your project’s packages.
Without venv:
- packages get installed globally
- projects can break each other due to version conflicts
With venv:
- each project has its own dependencies
- your computer stays clean
## Create and activate a venv (step by step)
Create:
```bash
python -m venv .venv
```
Activate:
### Windows (PowerShell)
```bash
.venv\Scripts\Activate.ps1
```
### macOS/Linux
```bash
source .venv/bin/activate
```
## Install packages inside the venv
```bash
pip install requests
```
## Save dependencies (so others can set up the project)
```bash
pip freeze > requirements.txt
```
Install from file:
```bash
pip install -r requirements.txt
```
## Best practice: .gitignore
Do not commit your environment folder:
```
.venv/
```
## Graph: isolation concept
```mermaid
flowchart LR
A[Project A] --> B[(.venv A)]
C[Project B] --> D[(.venv B)]
B --> E[Packages A]
D --> F[Packages B]
```
In the next lesson, you will learn *args and **kwargs, which helps you build flexible, reusable functions in professional code.
#Python#Intermediate#Virtual Env