Virtual Environment and pip in Python
In this lesson: Create and activate venv, install packages with pip and use requirements.txt — commands for Windows and Linux. In Hindi & English.
Why a virtual environment?
Imagine two projects on one laptop: a school report app that needs pandas 1.5 and a new dashboard that needs pandas 2.2. If both use the same global Python, installing one breaks the other. A virtual environment (venv) is a private folder of packages for each project.
| Tool | What it does |
|---|---|
pip | Downloads and installs packages from PyPI (pypi.org) |
venv | Creates an isolated Python environment for one project (built into Python 3) |
requirements.txt | A list of exact package versions so anyone can recreate the environment |
Step 1: create the environment
Open a terminal inside your project folder (in VS Code: Terminal → New Terminal) and run:
# Windows py -m venv .venv # macOS / Linux python3 -m venv .venv
This creates a hidden folder .venv with its own copy of the Python launcher and pip. The name .venv is a common convention – VS Code detects it automatically.
Step 2: activate it
| System | Command |
|---|---|
| Windows – Command Prompt | .venv\Scripts\activate |
| Windows – PowerShell | .venv\Scripts\Activate.ps1 |
| macOS / Linux | source .venv/bin/activate |
| Leave the environment | deactivate |
After activation the prompt starts with (.venv). Check that Python now comes from the project folder:
(.venv) $ python -m pip --version (.venv) $ pip list
A fresh environment contains only pip – none of your global packages. (Your version numbers will differ; this run used Python 3.13.)
Run once: Set-ExecutionPolicy -Scope CurrentUser RemoteSigned, then activate again. Or use Command Prompt.
Step 3: install packages with pip
pip install requests # latest version pip install requests==2.32.3 # exact version pip install "pandas>=2,<3" # any 2.x version pip install --upgrade requests # update pip uninstall requests # remove pip show requests # details: version, location, dependencies
Installing requests also installs the packages it depends on. This is what pip freeze showed after pip install requests==2.32.3:
python -m pip install … guarantees that pip installs into the same Python you run. It avoids the classic “I installed it but Python says ModuleNotFoundError”.
Step 4: share the project with requirements.txt
# save the exact versions you are using pip freeze > requirements.txt # on another computer (after creating and activating a new .venv) pip install -r requirements.txt
Your Python code can also check which versions are installed – useful in bug reports:
import sys
from importlib import metadata
print("Python", sys.version.split()[0])
print("inside a venv:", sys.prefix != sys.base_prefix)
for pkg in ["pip", "not-a-real-package"]:
try:
print(pkg, metadata.version(pkg))
except metadata.PackageNotFoundError:
print(pkg, "is not installed")Step 5: keep .venv out of Git
The .venv folder is large and specific to your computer. Never upload it or copy it to another PC. Commit requirements.txt instead and add this line to .gitignore:
.venv/ __pycache__/
A clean project layout
school-report/
├── .venv/ ← not committed
├── .gitignore
├── requirements.txt
├── README.md
├── report.py
└── data/
└── marks.csvCommon errors and fixes
| Error | Reason | Fix |
|---|---|---|
pip is not recognized | Python not on PATH | Use py -m pip (Windows) or reinstall Python with “Add to PATH” ticked |
ModuleNotFoundError after install | Installed into a different Python | Activate the venv; in VS Code choose the interpreter .venv (Ctrl+Shift+P → Python: Select Interpreter) |
externally-managed-environment (Linux/Mac) | System Python is protected | Create a venv – that is the intended solution |
| Moved project folder, venv broken | venv stores absolute paths | Delete .venv, create again, pip install -r requirements.txt |
Practice tasks
- Create a folder
weather-app, make a venv, installrequestsand saverequirements.txt. - Delete
.venvand rebuild the environment only fromrequirements.txt. - Run
check_env.pyinside and outside the venv and compare the “inside a venv” line. - Select the
.venvinterpreter in VS Code and run a file with the Run button.
Summary
- One project = one
.venv:python -m venv .venv. - Activate before working; the prompt shows
(.venv). - Install with
python -m pip install; record versions withpip freeze > requirements.txt. - Commit requirements.txt, ignore .venv.
