Python Virtual Environment
๐ Python Virtual Environment
A virtual environment (venv) in Python is an isolated workspace where you can install packages specific to a project without affecting the global Python installation or other projects.
โ Why Use a Virtual Environment?
| Without venv | With venv |
|---|---|
| Packages install globally | Packages isolated per project |
| Version conflicts may occur | Each project can have different versions |
| Hard to manage dependencies | Easy and clean dependency management |
๐ Create a Virtual Environment
Step 1: Check Python version
or (on some systems)
Step 2: Create a virtual environment
Here, myenv is the environment name โ you can rename it.
โถ Activate the Virtual Environment
| OS | Command |
|---|---|
| Windows (CMD) | myenv\Scripts\activate |
| Windows (PowerShell) | .\myenv\Scripts\Activate.ps1 |
| Mac / Linux | source myenv/bin/activate |
๐ After Activation
You will see something like:
Now, anything you install will go inside myenv.
๐ฆ Installing Packages
๐ Freeze Installed Packages
To save installed dependencies (useful for sharing projects):
๐ฅ Install Packages from requirements.txt
โ Deactivate the Virtual Environment
๐งน Remove a Virtual Environment (Optional)
Just delete the folder:
