Question

Install pip on macOS: Python Package Manager Guide

pythonmacospipinstallation

Question

How can I install pip, the Python package manager, on macOS (formerly OS X)? I have searched for a clear installation method and need a reliable way to set up and use it with Python.

Short Answer

You will learn how to check whether pip is already installed, install it safely for Python 3 when needed, and use it inside a virtual environment. You will also learn why python3 -m pip is usually more reliable than typing pip alone.

Concept

pip is Python's package installer. A package is reusable code published by Python developers, such as an HTTP client, a web framework, or a data-processing library.

On current macOS systems, the important detail is that Python and pip come in matching pairs:

  • python3 runs a particular Python 3 installation.
  • pip3 may refer to that installation's package installer.
  • python3 -m pip explicitly runs the pip module belonging to that exact python3 command.

Using python3 -m pip prevents a common problem: installing a package with one Python installation and trying to import it from another.

Many Python 3 installations include pip already. Before installing anything, check for it:

python3 -m pip --version

If this prints a version and a path, pip is ready. If it reports that there is no pip module, Python may be able to install its bundled copy through ensurepip:

python3 -m ensurepip --upgrade

For project work, install packages in a virtual environment rather than globally. This keeps each project's dependencies separate and avoids modifying system-managed Python files.

Mental Model

Think of Python as a kitchen and pip as the delivery service for ingredients.

  • A Python installation is one kitchen.
  • pip delivers packages to a specific kitchen.
  • A virtual environment is a small private kitchen for one recipe (project).

If your computer has several kitchens, saying pip install ... can send ingredients to the wrong one. python3 -m pip install ... is like giving the delivery service the exact kitchen address.

Take Quiz

Syntax and Examples

The most dependable general form is:

python3 -m pip <command>

Check pip

python3 -m pip --version

Install a package

python3 -m pip install requests

This downloads and installs the requests package for the Python interpreter named by python3.

Upgrade pip

python3 -m pip install --upgrade pip

Install from a dependency file

python3 -m pip install -r requirements.txt

Recommended: use a virtual environment

mkdir weather-app
cd weather-app
python3 -m venv .venv
source .venv/bin/activate
python -m pip install requests

After activation, python and pip refer to the environment in .venv. Leave it later with:

deactivate

Step by Step Execution

Consider this terminal session:

mkdir sample-project
cd sample-project
python3 -m venv .venv
source .venv/bin/activate
python -m pip install rich
python -c "from rich import print; print('[green]Installed successfully[/green]')"
  1. mkdir sample-project creates a folder for the project.
  2. cd sample-project moves the terminal into that folder.
  3. python3 -m venv .venv creates an isolated Python environment in .venv.
  4. source .venv/bin/activate changes the current shell so its python and pip commands use .venv first.
  5. python -m pip install rich downloads rich and installs it inside .venv, not into another Python installation.
  6. python -c ... starts that same environment's Python, imports rich, and prints formatted output.

You can confirm the active environment's installer location with:

python -m pip --version

Its displayed path should contain sample-project/.venv.

Real World Use Cases

pip is used whenever a Python project needs code that is not part of the standard library.

  • Web APIs: install fastapi, flask, or django.
  • HTTP requests: install requests or httpx to call external services.
  • Data work: install pandas, numpy, or matplotlib.
  • Testing: install tools such as pytest.
  • Automation scripts: install packages for reading spreadsheets, interacting with cloud services, or parsing HTML.
  • Team projects: install the exact dependency set recorded in requirements.txt.
Take Quiz

Real Codebase Usage

In real codebases, developers usually avoid installing packages directly into a shared system Python. A typical workflow is:

python3 -m venv .venv
source .venv/bin/activate
python -m pip install -r requirements.txt

Common project patterns include:

  • One environment per project: .venv keeps versions from different projects from conflicting.
  • Pinned dependencies: a file may specify a precise version, such as requests==2.32.3, so teammates and deployment systems use compatible code.
  • Separate development tools: projects often store test and formatting tools in a development dependency file or configuration.
  • Reproducible setup: a new developer clones the repository, creates an environment, and installs the listed packages.
  • Automation: continuous integration jobs run python -m pip install -r requirements.txt before executing tests.

If you use Homebrew to install Python, Homebrew normally provides a Python 3 interpreter and its matching pip. Check it rather than assuming a command name:

brew install python
python3 -m pip --version

Do not use sudo pip install ... as a normal setup method. It can alter files managed by macOS or another Python distribution.

Common Mistakes

Using pip without checking which Python it belongs to

This can install a package into a different interpreter than the one running your script.

pip install requests
python3 app.py

Prefer:

python3 -m pip install requests
python3 app.py

Installing globally when a virtual environment is needed

Global installs can create version conflicts between projects. Create and activate .venv first:

python3 -m venv .venv
source .venv/bin/activate
python -m pip install requests

Using sudo pip install

sudo pip install some-package

This may damage permissions or conflict with a Python installation managed by macOS, Homebrew, or another tool. Use a virtual environment instead.

Confusing pip with pip3

On some machines, pip may be missing or may point to Python 2 from an old installation. python3 -m pip makes the Python 3 relationship explicit.

Ignoring an externally managed environment error

Some Python distributions intentionally prevent global package installation. This is not a signal to force the install; it is a signal to use a virtual environment.

Installing a package but getting ModuleNotFoundError

Usually, the package was installed under a different Python. Compare these paths:

python3 -m pip --version
python3 -c "import sys; print(sys.executable)"

Then reinstall using that same interpreter's -m pip command.

Comparisons

Command or toolBest useKey point
pip install packageA controlled environment where pip is known to be correctCan be ambiguous on machines with multiple Pythons.
pip3 install packageSystems where pip3 is configured for Python 3Usually clearer than pip, but still depends on shell configuration.
python3 -m pip install packageMost macOS Python 3 setupsExplicitly pairs pip with python3.
python -m pip install packageAn activated virtual environmentUses the environment's Python.
python3 -m ensurepip --upgradepip is missing from a Python installation that supports ensurepipBootstraps or repairs pip; it does not install arbitrary packages.
Homebrew brew install pythonYou need a maintained Python installationInstalls Python and typically its corresponding pip.

A virtual environment is not an alternative package manager. It is an isolated location where pip installs packages safely for one project.

Take Quiz

Cheat Sheet

# Check the Python 3 package installer
python3 -m pip --version

# Install pip if this Python supports ensurepip and pip is missing
python3 -m ensurepip --upgrade

# Upgrade pip for this Python
python3 -m pip install --upgrade pip

# Create and activate a project environment
python3 -m venv .venv
source .venv/bin/activate

# Install a package in the active environment
python -m pip install package_name

# Install all listed dependencies
python -m pip install -r requirements.txt

# See installed packages
python -m pip list

# Leave the environment
deactivate

Rules of thumb:

  • Prefer python3 -m pip over bare pip.
  • Activate a virtual environment before installing project dependencies.
  • Avoid sudo pip install.
  • Run python -m pip --version to verify where packages will be installed.

FAQ

Is pip included with Python on macOS?

Many modern Python 3 installations include it. Verify with python3 -m pip --version instead of assuming.

How do I install pip if python3 -m pip fails?

Try python3 -m ensurepip --upgrade. If ensurepip is unavailable, install a current Python distribution, for example with brew install python, then check python3 -m pip --version again.

Should I use pip, pip3, or python3 -m pip on macOS?

Use python3 -m pip for Python 3 because it explicitly selects the interpreter whose packages you are managing.

Why does macOS say my Python environment is externally managed?

Your Python distributor is protecting its managed files from global changes. Create a virtual environment and install packages there instead.

Why is a package installed but Python cannot import it?

The package was probably installed for another Python interpreter. Install it with the same interpreter that runs your program, for example python3 -m pip install package_name.

Do I need sudo to install Python packages?

Usually no. A virtual environment avoids permission issues and is safer than modifying global Python directories.

How do I see where pip installs packages?

Run python3 -m pip --version. The output includes the pip version and its installation path.

Take Quiz

Mini Project

Description

Set up a small Python project that uses an external package in an isolated virtual environment. This mirrors how a developer prepares a script without changing global Python packages.

Goal

Create a virtual environment, install requests, and run a script that fetches a public JSON response.

Requirements

Create a new project directory and a .venv virtual environment. Activate the virtual environment before installing dependencies. Install the requests package using the active environment's Python. Create a Python script that requests https://api.github.com. Print the HTTP status code and the API response's current_user_url value.

Take Quiz

Keep learning

Related questions

View more