agyanadda@gmail.com

Python pip and Virtual Environments

Functions and Modules 1 min read Updated Dec 4, 2026 Topic 20 of 32

pip

pip installs packages from PyPI (pypi.org):

  • pip install requests
  • pip install requests==2.32.3 — exact version
  • pip uninstall requests
  • pip list, pip show requests

On some systems use python -m pip to be sure you install into the right Python.

Why virtual environments?

Different projects need different package versions. A virtual environment gives each project its own isolated set of packages.

venv

  1. Create: python -m venv .venv
  2. Activate:
    • Windows: .venv\Scripts\activate
    • macOS / Linux: source .venv/bin/activate
  3. Your prompt shows (.venv) — now pip install goes into this project only
  4. deactivate to leave

requirements.txt

  • Save: pip freeze > requirements.txt
  • Install on another computer: pip install -r requirements.txt

Add .venv/ to .gitignore. Newer tools such as uv and Poetry manage environments and dependencies even faster.

Example

Example — Set up a project Bash
mkdir weather-app && cd weather-app

# Create and activate a virtual environment
python -m venv .venv
.venv\Scripts\activate          # Windows
# source .venv/bin/activate     # macOS / Linux

(.venv) pip install requests python-dotenv
(.venv) pip list

# Save exact versions
(.venv) pip freeze > requirements.txt

# Later, on another machine:
pip install -r requirements.txt

(.venv) deactivate

Packages install only inside .venv for this project. requirements.txt lists them (e.g. requests==2.32.3) so anyone can recreate the same setup.

Test Yourself With Exercises

Exercise 1 Multiple Choice

Which command creates a virtual environment?

Exercise 2 True / False

You should commit the .venv folder to Git.

Exercise 3 Fill in the Blank

Install packages from a file: pip install -r ___.txt

Frequently Asked Questions

Run Set-ExecutionPolicy -Scope CurrentUser RemoteSigned in PowerShell once, then activate again. Or use the Command Prompt and run .venv\Scripts\activate.bat.