Python Mastery – From Basics to Brilliance / Modules & Packages

Virtual environments

🌐 Virtual Environments

A virtual environment is an isolated Python environment created for a project.

It allows each project to have its own packages and package versions without affecting other projects.


πŸ€” Why Use Virtual Environments?

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 stays independent.


πŸ› οΈ Create a Virtual Environment

python -m venv venv

Here:

  • python -m venv β†’ Creates a virtual environment

  • venv β†’ Name of the environment


▢️ Activate It

Windows

venv\Scripts\activate

macOS / Linux

source venv/bin/activate

Once activated, packages installed using pip are installed inside that environment.

pip install requests

❌ Deactivate

When you're finished:

deactivate

Virtual Environment = A separate Python workspace for each project.