Python • Getting Started and Tooling

pip & Package Installation — Install and import third-party packages using pip (conceptual workflow)

Learn how pip connects your Python projects to a vast ecosystem of community-built tools and libraries.

Code braces with terminal and markup symbols representing Python programming.
Inside this lesson 10 sections 5 questions 5 figures

SECTION 1

Historical Context & Motivation

When Python was first released in 1991, programmers who wanted to share code with each other had very few options. If you wrote a useful library—say, a module for working with images—you might post it on a personal website and hope people found it. There was no standard way to download, install, or manage someone else's code. As the Python community grew, this became a real problem: developers were spending too much time hunting for libraries and manually copying files instead of actually writing programs. The need for a package manager—a tool that automates downloading, installing, and organizing third-party code—became urgent.

2000 • distutils Ships with Python

Python's standard library included distutils, a basic tool for packaging and distributing code. It was a start, but it couldn't automatically download or install packages from the internet.

2003 • PyPI Launches

The Python Package Index (PyPI) went live as a central online repository where developers could upload and share packages. Think of it as an app store for Python code.

2008 • pip Is Created

Ian Bicking created pip ("pip installs packages") as a more reliable replacement for an older tool called easy_install. pip could uninstall packages, handle dependencies, and interact cleanly with PyPI.

2014 • pip Bundled with Python

Starting with Python 3.4, pip was included automatically when you installed Python. This meant every new Python user had a package manager ready to go—no extra setup required.

2024 • Over 500,000 Packages on PyPI

PyPI now hosts more than half a million packages covering everything from web frameworks to machine learning. pip remains the default tool for accessing this massive ecosystem.

So how does pip actually work? How does a single command in your terminal reach across the internet, find the right package, download it, and make it available in your code? That is the question this lesson answers.

SECTION 2

Core Principles & Definitions

Before diving into commands, let's define the key ideas that make package installation work. Understanding these concepts will help you troubleshoot problems and make smarter choices in your projects.

1

Package

A package is a bundle of Python code organized for reuse. It can contain modules (individual .py files), sub-packages, documentation, and metadata like version numbers. When someone says "install a package," they mean downloading this bundle and placing it where Python can find it.
2

PyPI (the Repository)

The Python Package Index is a public website (pypi.org) that stores packages uploaded by developers worldwide. When you run a pip install command, pip connects to PyPI by default to find and download the package you requested.
3

Dependency

A dependency is a package that another package needs in order to function. For example, a data visualization library might depend on a math library. pip automatically identifies and installs dependencies for you.
4

Virtual Environment

A virtual environment is an isolated folder that holds a separate set of installed packages for a specific project. This prevents conflicts when different projects need different versions of the same package.
5

import Statement

After pip installs a package, you use Python's import statement in your code to load it into memory. Installation and importing are two separate steps: pip handles the first, and your Python script handles the second.
✦

KEY TAKEAWAY

Think of pip like a food delivery app. PyPI is the restaurant menu listing all available dishes (packages). When you place an order (pip install), the app finds the dish, picks up any sides it needs (dependencies), and delivers everything to your kitchen (your project's Python environment). But the food doesn't magically appear on your plate—you still have to serve it yourself with an import statement.

SECTION 3

Visual Explanation — The pip Workflow

The diagram below shows the full journey from typing a pip command to using the installed package in your Python code. Follow the numbered arrows to trace each stage of the process.

The pip Install → Import Workflow① YOUR TERMINALpip install requests(you type the command)② pip RESOLVERReads package name& version constraints(finds what to get)③ PyPI SERVERpypi.org hosts500,000+ packages(the online repository)④ DOWNLOADPackage + all itsdependencies fetched⑤ INSTALLFiles placed insite-packages folder⑥ YOUR PYTHON SCRIPTimport requestsr = requests.get(url)(now you can use the package!)site-packages/requests/urllib3/certifi/
The six stages of installing and using a package: ① you type a pip install command; ② pip resolves the package name and version; ③ it contacts PyPI; ④ the package and its dependencies are downloaded; ⑤ files are placed in the site-packages directory; ⑥ you import the package in your script to use it.

Notice that the workflow has two distinct phases. The installation phase (steps ①–⑤) happens once, typically in your terminal or command prompt. The usage phase (step ⑥) happens every time you run your script. Once a package is installed, you don't need to install it again unless you want a different version or you've switched to a new virtual environment.

SECTION 4

How pip Works Under the Hood

The Command-Line Interface

pip is a command-line tool, meaning you interact with it by typing text commands rather than clicking buttons. You open a terminal (Command Prompt on Windows, Terminal on macOS/Linux) and type commands that follow a consistent pattern.

ℹ

Command Syntax Pattern

The general form of a pip command is: pip <action> <package-name>. The most common actions are install, uninstall, list (shows installed packages), and freeze (outputs a list of installed packages with exact versions).

Dependency Resolution

One of pip's most important jobs is dependency resolution. When you install package A, pip reads A's metadata to discover that A requires packages B and C. Then it checks B and C to see if they have their own requirements, and so on. pip builds a complete list of everything needed and installs it all. This process is sometimes called resolving the dependency tree. Modern pip (version 20.3 and later) uses a backtracking resolver that carefully picks compatible versions, even when multiple packages have overlapping requirements.

Version Specifiers

You can control exactly which version of a package pip installs by adding a version specifier to the command. For example, pip install requests==2.31.0 installs exactly version 2.31.0, while pip install requests>=2.28 installs any version 2.28 or newer. If you omit the version, pip defaults to the latest stable release.

SpecifierMeaningExample
==Exactly this versionpip install flask==2.3.0
>=This version or newerpip install flask>=2.0
<=This version or olderpip install flask<=3.0
~=Compatible release (same major.minor)pip install flask~=2.3
(none)Latest stable versionpip install flask

Common pip version specifiers

Where Do Packages Go?

When pip installs a package, it places the files in a directory called site-packages. This is a special folder that Python automatically checks when you run an import statement. If you're using a virtual environment, site-packages lives inside that environment's folder, keeping your project's packages separate from the system-wide Python installation. You can find the exact path by running python -m site in your terminal.

SECTION 5

Essential pip Commands & Requirements Files

Now that you understand the concepts, let's look at the most common pip commands you'll use day to day, and a powerful feature called requirements files that helps teams collaborate on projects.

Essential pip Commands Cheat SheetINSTALL A PACKAGEpip install requestsDownloads from PyPI and installsUNINSTALL A PACKAGEpip uninstall requestsRemoves the package from site-packagesLIST INSTALLED PACKAGESpip listShows every package in your environmentFREEZE VERSIONSpip freeze > requirements.txtSaves exact versions to a fileINSTALL FROM FILEpip install -r requirements.txtInstalls everything listed in the fileSHOW PACKAGE INFOpip show requestsDisplays version, location, dependenciesREQUIREMENTS FILE (requirements.txt)requests==2.31.0flask==3.0.0numpy>=1.24pillow~=10.0← exact version pinned← exact version pinned← minimum version allowed← compatible release (10.x)
The six most commonly used pip commands and an example requirements.txt file showing different version specifiers. Sharing a requirements file lets your teammates install the exact same packages with a single command.

A requirements file is one of the most practical tools you'll encounter as you start building real projects. Imagine you and a classmate are working on a group project. Instead of texting each other "hey, make sure you install flask and requests," you just share a requirements.txt file. Your classmate runs pip install -r requirements.txt and instantly gets every package at the correct version. This practice is standard in professional software development.

💡

Pro Tip: Using python -m pip

If you have multiple versions of Python installed, running python -m pip install requests ensures you're using the pip associated with the specific Python interpreter you want. This avoids accidentally installing packages into the wrong Python version.

SECTION 6

Worked Example — Building a Weather App

Let's walk through a realistic scenario from start to finish. Suppose you want to build a simple Python script that fetches weather data from an online API and displays it in a nicely formatted table. You'll need two third-party packages: requests (for making HTTP requests) and tabulate (for formatting data into tables).

Installing and Using Packages for a Weather Script

  1. 1

    Step 1 — Create a Virtual Environment

    Open your terminal, navigate to your project folder, and create a virtual environment by typing python -m venv weather_env. This creates an isolated folder called weather_env that will hold its own copy of pip and any packages you install.
    A weather_env/ folder is created in your project directory.
  2. 2

    Step 2 — Activate the Virtual Environment

    On Windows, type weather_env\Scripts\activate. On macOS/Linux, type source weather_env/bin/activate. Your terminal prompt should change (often showing the environment name in parentheses) to confirm you're inside the virtual environment.
    Terminal prompt now shows (weather_env) — all pip commands will target this environment.
  3. 3

    Step 3 — Install the Packages

    Type pip install requests tabulate. pip connects to PyPI, downloads both packages along with any dependencies (like urllib3 and certifi, which requests needs), and installs everything into weather_env/lib/site-packages/.
    Terminal output: Successfully installed requests-2.31.0 tabulate-0.9.0 ...
  4. 4

    Step 4 — Verify the Installation

    Run pip list to confirm that requests and tabulate appear in the list. You can also run pip show requests to see detailed information like the version, author, and which dependencies it installed.
    The pip list output includes requests, tabulate, and their dependencies.
  5. 5

    Step 5 — Write and Run Your Script

    Create a file called weather.py and add your import statements at the top: import requests and from tabulate import tabulate. Now you can use requests.get() to fetch weather data and tabulate() to display it neatly.
    Running python weather.py executes your script with both third-party packages available.
  6. 6

    Step 6 — Save Dependencies for Your Team

    Run pip freeze > requirements.txt to generate a file listing all installed packages with exact versions. Share this file with teammates or include it in version control (like Git) so anyone else can recreate your environment.
    A requirements.txt file is created, ready to share.

SECTION 7

pip vs. Other Package Managers

pip is the default Python package manager, but it's not the only tool available. As your projects grow more complex, you may encounter alternatives. Understanding how pip compares to other options helps you make informed choices.

ToolStrengthsLimitations
pipComes with Python; simple syntax; huge community; works with any package on PyPI.Doesn't manage virtual environments for you; no built-in lock file (use pip freeze manually).
condaManages both Python packages and non-Python dependencies (C libraries); creates environments automatically; popular in data science.Separate installation required (Anaconda/Miniconda); not all PyPI packages available; can be slow.
PoetryManages dependencies, virtual environments, and packaging in one tool; automatic lock files; clean project config.Requires separate installation; steeper learning curve; less widespread in beginner tutorials.
pipenvCombines pip and virtual environments; generates lock files for reproducibility.Development has been inconsistent; can be slow on large projects; less popular recently.

Comparing Python package management tools

✦

KEY TAKEAWAY

For high school and early college projects, pip is the right tool to learn first. It's built into Python, it's used in virtually every Python tutorial, and it gives you direct access to the full PyPI ecosystem. Tools like conda and Poetry are valuable, but they build on the same concepts you're learning here. Master pip now, and you'll pick up alternatives easily later.

SECTION 8

Connecting to Advanced Concepts

Once you're comfortable with basic pip usage, several more advanced topics will come into play as your projects grow. Here's a preview of how the concepts you've learned connect to professional Python development.

What You Know NowWhat Comes Next
pip install package_nameUsing lock files (pip-compile, poetry.lock) to guarantee identical environments across machines.
Creating a virtual environment with venvContainer-based environments (Docker) that bundle the OS, Python version, and packages together.
Installing packages from PyPIPublishing your own packages to PyPI using tools like setuptools, build, and twine.
requirements.txt for sharing dependenciesCI/CD pipelines that automatically install dependencies, run tests, and deploy code.
Understanding dependency resolutionSemantic versioning (SemVer) policies and handling breaking changes between major versions.

From pip basics to professional workflows

The core workflow—install a package, import it, use it—stays the same no matter how advanced the surrounding tools become. Every professional Python developer uses pip or a tool built on top of it. The conceptual model you've built in this lesson is the foundation for everything that follows.

SECTION 9

Practice Problems

Question 1: CONCEPTUAL

Explain the difference between installing a package with pip and importing a package in a Python script. Why are both steps necessary?
Show answer
Installing a package (using pip install) downloads the package files from PyPI and places them into your Python environment's site-packages directory. This makes the package available on your computer but doesn't automatically load it into your running program. Importing (using import in your script) tells Python to find those files in site-packages and load them into memory so your code can use their functions and classes. Both steps are necessary because installation is a one-time setup action, while importing is done each time your script runs.

Question 2: BASIC

Write the exact pip command you would type to install version 1.25.0 of the numpy package. Then write the pip command to verify it was installed correctly.
Show answer
To install the exact version, type: pip install numpy==1.25.0. The double equals sign (==) tells pip to install exactly that version, not a newer or older one. To verify, you can run pip show numpy which will display the version number, installation location, and dependencies. Alternatively, pip list will show all installed packages including numpy and its version.

Question 3: INTERMEDIATE

You join a group project and your teammate sends you a requirements.txt file. Describe the complete sequence of terminal commands you would run to: (a) create a new virtual environment, (b) activate it, and (c) install all the packages from the requirements file.
Show answer
First, navigate to your project folder in the terminal. Then: (a) Create the virtual environment with python -m venv myenv where 'myenv' is any name you choose. (b) Activate it with source myenv/bin/activate on macOS/Linux or myenv\Scripts\activate on Windows. Your terminal prompt should change to show the environment name. (c) Install the dependencies with pip install -r requirements.txt. This reads each line of the file and installs the specified package and version. Using a virtual environment keeps these packages isolated from your system Python and from other projects.

Question 4: APPLIED

You're building a Python script that scrapes headlines from a news website and saves them to a CSV file. You decide to use the beautifulsoup4 package for HTML parsing and the requests package for HTTP requests. Write the pip install command, the import statements you'd put at the top of your script, and the requirements.txt entries you'd expect after running pip freeze. (Note: the beautifulsoup4 package is imported as bs4 in Python.)
Show answer
Install command: pip install beautifulsoup4 requests. Import statements in your script: import requests and from bs4 import BeautifulSoup. Note that the PyPI package name (beautifulsoup4) differs from the import name (bs4)—this is a common gotcha. After running pip freeze, you'd see entries like beautifulsoup4==4.12.2, requests==2.31.0, plus their dependencies (certifi, charset-normalizer, idna, soupsieve, urllib3). For the CSV part, Python's built-in csv module handles that—no pip install needed since it's part of the standard library.

Question 5: CRITICAL THINKING

Imagine you have two Python projects on your computer. Project A requires requests==2.25.0 and Project B requires requests==2.31.0. Without virtual environments, what problem would occur? Explain how virtual environments solve this issue, and discuss why pip freeze and requirements.txt become especially important in a team setting.
Show answer
Without virtual environments, both projects share the same site-packages directory. Installing requests 2.31.0 for Project B would overwrite version 2.25.0, potentially breaking Project A if it relies on behavior specific to that older version. Virtual environments solve this by giving each project its own isolated site-packages. Project A's environment can hold requests 2.25.0 while Project B's environment holds 2.31.0, with no conflict. In a team setting, pip freeze > requirements.txt becomes critical because it creates a snapshot of the exact package versions in your environment. Without it, a teammate might install different versions and encounter bugs you can't reproduce—a problem called 'works on my machine.' By committing requirements.txt to version control, every team member can recreate the exact same environment with pip install -r requirements.txt.

SUMMARY

Lesson Summary

Summary

pip is Python's default package manager, connecting your projects to the Python Package Index (PyPI)—a repository of over 500,000 community-built packages. The core workflow is straightforward: you type pip install <package> in your terminal, pip downloads the package and its dependencies from PyPI, and installs everything into the site-packages directory. Then you use an import statement in your Python script to load the package and start using it.

To keep projects organized and avoid version conflicts, use virtual environments (created with python -m venv) to give each project its own isolated set of packages. Use version specifiers (like == and >=) to control which versions pip installs. When collaborating with others, generate a requirements.txt file with pip freeze so teammates can replicate your exact environment. These skills—installing, importing, isolating, and sharing—form the foundation of every Python project you'll build from here on.

Varsity Tutors • Python • pip & Package Installation