📓 Python Basics

In [1]:

Virtual Environment

2025-10-16 02:44

1️⃣ Create Virtual Environment

python -m venv venv

2️⃣ Activate Virtual Environment
venv\Scripts\activate

 

# 🐍 Python Virtual Environment (venv) Basics

A **virtual environment (venv)** allows you to create isolated environments for Python projects — so dependencies from one project don’t affect another.

---

## 1️⃣ Create Virtual Environment

```bash
python -m venv venv

This creates a folder named venv with a self-contained Python environment.


2️⃣ Activate Virtual Environment

▶️ Windows (CMD / PowerShell)

venv\Scripts\activate

▶️ macOS / Linux

source venv/bin/activate

Once activated, you’ll see (venv) in your terminal prompt.


3️⃣ Install Packages

Use pip inside the activated venv:

pip install requests

Check installed packages:

pip list


4️⃣ Deactivate Virtual Environment

To exit the venv:

deactivate


5️⃣ Save and Restore Dependencies

Save:

pip freeze > requirements.txt

Restore:

pip install -r requirements.txt


✅ Check Python Path

which python
# or on Windows
where python

This should point to your virtual environment’s Python executable.


💡 Tip:
Each project should have its own venv to keep dependencies clean and manageable.