Skip to content

Setting Up: Virtual Environments and pip

Beginning of the lesson

Setting Up: Virtual Environments and pip

Give every project its own clean, isolated set of packages

Big Idea: A virtual environment is a private, throwaway copy of Python for one project. Its packages live in their own folder, so two projects can depend on different versions of the same library without ever clashing. pip is the tool that installs those packages, and requirements.txt records exactly what a project needs so anyone can rebuild it.
By the end you will be able to:
  • explain why global installs cause dependency conflicts,
  • create and activate a virtual environment with python -m venv,
  • install, inspect and remove packages with pip,
  • freeze a project into requirements.txt and rebuild it elsewhere,
  • read version specifiers like ==, >= and ~=.

1. The problem: one Python, many projects

When you run pip install without any setup, the package goes into the global site-packages folder shared by every project on your machine. That feels convenient until two projects disagree. Imagine an old website that only runs on django==3.2 and a new one that needs django==5.0. There is only one global slot for Django, so installing one version breaks the other. This is a dependency conflict.

Global installs cause other pain too: you cannot tell which packages a given project actually uses, you often need administrator rights to install system-wide, and a change made for one project can silently break another months later. The fix is isolation: give each project its own private set of packages.

Analogy: Think of a shared kitchen where everyone dumps ingredients into one pantry. Sooner or later two recipes need different, incompatible items. A virtual environment is like giving each cook their own labelled box of ingredients — nothing gets mixed up, and you can throw a box away and start fresh whenever you like.

Continue this lesson on YourAIZone

The full lesson has the complete explanation, worked examples, common mistakes, tests with instant checking and an AI tutor. Start right away as a guest, or create an account to keep your progress.

In the full lesson

  • The complete explanation
  • Worked examples
  • Common mistakes and how to avoid them
  • Test questions: 15
  • AI tutor that answers questions about the topic
  • Progress tracking for students, parents and teachers