Maintenance

Site is under maintenance — quizzes are still available.

Go to quizzes

Install packages with pip

Learn to install and manage Python packages using pip, a fundamental tool for extending Python's functionality. This tutorial covers core concepts, hands-on steps, troubleshooting, and what to study next.

Focus: install and manage packages with pip

Sponsored

You've written some impressive Python scripts, but you're about to hit a wall. The standard library is powerful, but it can't do everything — you need to parse complex JSON, make HTTP requests, or generate a plot. Without a way to install and manage extra code, you'd be stuck re-inventing the wheel. This is precisely the problem pip solves: it's the standard package manager that opens Python up to a universe of over 400,000 community-contributed libraries.

The problem this lesson solves

Before pip, adding external code meant downloading a tarball, manually extracting it, hoping the dependencies were already installed, and crossing your fingers. It was a fragile, time-consuming nightmare. The core pain is dependency hell — you need library A, which depends on library B version 2.0, but you already have B version 1.0 for another project. Without a package manager, you either break your other project or give up entirely. Pip eliminates this by automating downloads, resolving dependencies, and managing versions in a repeatable way.

Core concept / mental model

Think of Python as a construction site, and pip as your supply ordering system. The standard library is the toolbox of basic tools (hammers, screwdrivers). Pip lets you order specialized equipment: a concrete mixer (requests), a laser level (pandas), or a crane (numpy). Each order arrives with all its required parts (dependencies) and is placed in a designated storage area (your Python environment). The mental model is simple:

  • Package: A bundle of Python code (typically a directory with __init__.py) plus metadata (name, version, dependencies).
  • Index: The central warehouse — PyPI (Python Package Index) at pypi.org. It's where pip looks for packages.
  • Installation: Pip copies the package from PyPI into your local site-packages directory so your import statements can find it.

Pro Tip: Pip is not a Python environment itself. It works inside a specific Python interpreter. If you're using a virtual environment (which you almost always should), pip installs into that environment, not your global system Python.

How it works step by step

When you run pip install requests, this is what happens behind the scenes:

  1. Resolution: Pip checks PyPI for a package named requests and fetches its metadata, including dependencies.
  2. Dependency graph: Pip calculates all required dependencies (e.g., urllib3, charset-normalizer, certifi, idna).
  3. Download: Pip downloads the best matching version of each package — usually the latest stable release.
  4. Extraction: Pip extracts each package archive (.tar.gz or .whl) into a temporary directory.
  5. Installation: Pip copies the extracted code into the current environment's site-packages/ directory.
  6. Caching: Pip caches the downloaded archives locally (~/.cache/pip/), so future installations of the same version are instant.

Hands-on walkthrough

Let's get practical. Open your terminal or command prompt. First, check your pip version:

pip --version

Expected output (your version will vary):

pip 24.0 from /usr/lib/python3.12/site-packages/pip (python 3.12)

If you see an error, you may need pip3 instead, or you need to install pip via your system package manager.

Install a package

Install the popular requests library:

pip install requests

Expected output:

Collecting requests
  Downloading requests-2.31.0-py3-none-any.whl (62 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 62.6/62.6 KB 3.2 MB/s eta 0:00:00
Collecting urllib3>=1.21.1,<1.27
  Downloading urllib3-1.26.18-py2.py3-none-any.whl (143 kB)
...
Successfully installed certifi-2023.7.22 charset-normalizer-3.3.0 idna-3.4 requests-2.31.0 urllib3-1.26.18

Now you can use it in Python:

import requests
response = requests.get('https://api.github.com')
print(response.status_code)
# Output: 200

Install a specific version

pip install numpy==1.24.0

List installed packages

pip list

Sample output:

Package        Version
-------------- -------
certifi        2023.7.22
charset-normalizer 3.3.0
idna           3.4
numpy          1.24.0
pip            24.0
requests       2.31.0
urllib3        1.26.18

Uninstall a package

pip uninstall requests -y

Freeze to a requirements file

This is critical for sharing projects:

pip freeze > requirements.txt

Now requirements.txt looks like:

certifi==2023.7.22
charset-normalizer==3.3.0
idna==3.4
numpy==1.24.0
urllib3==1.26.18

Another developer can recreate your exact environment:

pip install -r requirements.txt

Compare options / when to choose what

Pip is the standard, but it's not the only game in town. Here's a quick comparison:

Tool Use case Strength Weakness
pip Everyday package install Universal, works everywhere No environment isolation by itself
conda Data science, complex native deps Manages non-Python deps (C libs) Slower, larger index
pipenv Project-level dependency management Combines pip + virtualenv + Pipfile Slower resolution, fewer features now
poetry Modern dependency mgmt + build Lock file, virtual environment, pyproject.toml support Learning curve for beginners

When to choose the baseline: - Use pip + venv if you're learning or building small to medium projects. - Use conda for data science where you need numpy, scipy, or tensorflow with native optimizations. - Use poetry if you're building a serious Python package or application with many dependencies.

Troubleshooting & edge cases

"pip: command not found"

If pip is missing, Python 3.4+ includes it — try python3 -m pip instead.

Permission denied (Linux/macOS)

pip install --user requests

Or better, use a virtual environment to avoid needing sudo.

"ERROR: Package 'X' has no metadata"

This usually means the package is malformed or from an untrusted source. Try upgrading pip:

pip install --upgrade pip

"Dependency conflict"

If pip says it cannot find a compatible version of dependencies, you can: - Install a different version of the conflicting package. - Use pip check to see the issue. - Remove constraints by temporarily uninstalling a package.

"Package installed but import fails"

Check that you're running the same Python environment where pip installed. In VSCode, use the Command Palette to select the correct interpreter.

Installing from a requirements.txt fails

Make sure all packages listed exist on PyPI. Try commenting out one line at a time to isolate the problem.

What you learned & what's next

You now know the central idea behind pip — it's the Python package manager that downloads and installs third-party libraries from PyPI, handling dependencies automatically. You practiced installing, listing, uninstalling, and freezing packages. This skill is the foundation for extending Python into any domain: web development, data science, automation, or machine learning.

Next lesson: Now that you can install packages, you need a clean, isolated workspace for each project — learn how to create and manage virtual environments with venv, so your pip install never conflicts with other projects.

Practice recap

Create a new directory for a practice project. Inside, run python3 -m venv venv and activate it. Then install fake-library-name (or any real dummy package like cowsay) with pip install. Run pip freeze > requirements.txt and inspect the file. Finally, deactivate the environment and delete the project folder.

Common mistakes

  • Running pip install without a virtual environment, polluting the global Python installation and risking version conflicts.
  • Forgetting to use --user or sudo on Linux/macOS when installing globally without admin rights, leading to permission errors.
  • Not pinning versions in requirements.txt — using pip freeze > requirements.txt is safe; manually writing lines like requests>=2.0.0 can cause inconsistent builds.
  • Adding a package to requirements.txt without running pip freeze, so the list becomes stale and missing transitive dependencies.

Variations

  1. Use python3 -m pip instead of pip to guarantee you're using the pip associated with your Python interpreter.
  2. For data science projects, try conda install which can also handle native C library dependencies automatically.
  3. For reproducible builds, use pip install --no-cache-dir to force a fresh download every time.

Real-world use cases

  • A web developer installs flask and requests via pip to build a REST API that consumes external services.
  • A data analyst installs pandas, numpy, and matplotlib to clean and visualize sales data from a CSV file.
  • A machine learning engineer installs tensorflow and scikit-learn to train a model, then freezes requirements.txt for deployment.

Key takeaways

  • Pip is the standard tool to install Python packages from PyPI, automatically handling dependencies.
  • Always use pip install -r requirements.txt to reproduce an environment; never list packages manually.
  • Isolate projects using virtual environments (venv) before running pip install to avoid system conflicts.
  • Pin exact versions in requirements.txt via pip freeze for deterministic builds.
  • Use pip list, pip show, and pip uninstall to inspect and manage your environment.

Sponsored

Sponsored

Discussion

Questions, corrections, and tips help everyone reading this page.

0 comments

Add a comment

Shown publicly with your comment.

Be constructive · max 4,000 characters

No comments yet — start the thread.

Related tutorials, quizzes, and articles for this topic.