Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
🟡 Advanced Python  ·  Lesson 26

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.

ToolWhat it does
pipDownloads and installs packages from PyPI (pypi.org)
venvCreates an isolated Python environment for one project (built into Python 3)
requirements.txtA 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:

Terminal
# 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

SystemCommand
Windows – Command Prompt.venv\Scripts\activate
Windows – PowerShell.venv\Scripts\Activate.ps1
macOS / Linuxsource .venv/bin/activate
Leave the environmentdeactivate

After activation the prompt starts with (.venv). Check that Python now comes from the project folder:

Terminal
(.venv) $ python -m pip --version
(.venv) $ pip list
pip 26.2.1 from ~/school-report/.venv/lib/python3.13/site-packages/pip (python 3.13) Package Version ------- ------- pip 26.2.1

A fresh environment contains only pip – none of your global packages. (Your version numbers will differ; this run used Python 3.13.)

⚠️ PowerShell: “running scripts is disabled”

Run once: Set-ExecutionPolicy -Scope CurrentUser RemoteSigned, then activate again. Or use Command Prompt.

Step 3: install packages with pip

Terminal
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:

certifi==2026.10.23 charset-normalizer==3.5.2 idna==3.20 requests==2.32.3 urllib3==2.8.0
💡 Use python -m pip

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

Terminal
# 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:

Python – check_env.py (run inside the activated .venv)
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")
Python 3.13.16 inside a venv: True pip 26.2.1 not-a-real-package 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:

.gitignore
.venv/
__pycache__/

A clean project layout

Folder structure
school-report/
├── .venv/              ← not committed
├── .gitignore
├── requirements.txt
├── README.md
├── report.py
└── data/
    └── marks.csv

Common errors and fixes

ErrorReasonFix
pip is not recognizedPython not on PATHUse py -m pip (Windows) or reinstall Python with “Add to PATH” ticked
ModuleNotFoundError after installInstalled into a different PythonActivate the venv; in VS Code choose the interpreter .venv (Ctrl+Shift+P → Python: Select Interpreter)
externally-managed-environment (Linux/Mac)System Python is protectedCreate a venv – that is the intended solution
Moved project folder, venv brokenvenv stores absolute pathsDelete .venv, create again, pip install -r requirements.txt

Practice tasks

  1. Create a folder weather-app, make a venv, install requests and save requirements.txt.
  2. Delete .venv and rebuild the environment only from requirements.txt.
  3. Run check_env.py inside and outside the venv and compare the “inside a venv” line.
  4. Select the .venv interpreter 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 with pip freeze > requirements.txt.
  • Commit requirements.txt, ignore .venv.

Frequently Asked Questions

What is the difference between venv, virtualenv and conda?
venv is built into Python 3 and is enough for most projects. virtualenv is an older third-party tool with extra features. conda (Anaconda/Miniconda) also manages non-Python libraries and is popular in data science.
Do I need a venv for small practice programs?
Not for programs that use only the standard library. As soon as you pip install anything for a project, create a venv for that project.
Can I name it something other than .venv?
Yes, any name works (venv, env). .venv is recommended because it is hidden, common and auto-detected by VS Code and many tools.
← Back to Python Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicWeb Scraping Basics Your progress: 26 / 66 (39%) · Lesson 27 / 66 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free