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.

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.

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.

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 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.

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.

Common pip version specifiers
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

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.

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.

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.

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
Step 1 — Create a Virtual EnvironmentOpen 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
Step 2 — Activate the Virtual EnvironmentOn 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
Step 3 — Install the PackagesType 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
Step 4 — Verify the InstallationRun 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
Step 5 — Write and Run Your ScriptCreate 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
Step 6 — Save Dependencies for Your TeamRun 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.

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.

Comparing Python package management tools
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.
✦ 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.

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.

From pip basics to professional workflows
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.

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.

Practice Problems

PROBLEM 1 — CONCEPTUAL
Explain the difference between installing a package with pip and importing a package in a Python script. Why are both steps necessary?
PROBLEM 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.
PROBLEM 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.
PROBLEM 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.)
PROBLEM 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.

Lesson 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