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:
python3runs a particular Python 3 installation.pip3may refer to that installation's package installer.python3 -m pipexplicitly runs thepipmodule belonging to that exactpython3command.
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.
pipdelivers 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.
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:
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]')"
mkdir sample-projectcreates a folder for the project.cd sample-projectmoves the terminal into that folder.python3 -m venv .venvcreates an isolated Python environment in.venv.source .venv/bin/activatechanges the current shell so itspythonandpipcommands use.venvfirst.python -m pip install richdownloadsrichand installs it inside.venv, not into another Python installation.python -c ...starts that same environment's Python, importsrich, and prints formatted output.
You can confirm the active environment's installer location with:
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, ordjango. - HTTP requests: install
requestsorhttpxto call external services. - Data work: install
pandas,numpy, ormatplotlib. - 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.
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:
.venvkeeps 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.txtbefore 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.
Comparisons
| Command or tool | Best use | Key point |
|---|---|---|
pip install package | A controlled environment where pip is known to be correct | Can be ambiguous on machines with multiple Pythons. |
pip3 install package | Systems where pip3 is configured for Python 3 | Usually clearer than pip, but still depends on shell configuration. |
python3 -m pip install package | Most macOS Python 3 setups | Explicitly pairs pip with python3. |
python -m pip install package | An activated virtual environment | Uses the environment's Python. |
python3 -m ensurepip --upgrade |
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 pipover barepip. - Activate a virtual environment before installing project dependencies.
- Avoid
sudo pip install. - Run
python -m pip --versionto 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?
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.
Keep learning
Related questions
@staticmethod vs @classmethod in Python Explained
Learn the difference between @staticmethod and @classmethod in Python with clear examples, use cases, mistakes, and a mini project.
Add Rows to a Pandas DataFrame in Python
Learn how to add rows to a Pandas DataFrame, why repeated row appends are slow, and when to use loc, concat, or record lists.
Call a Function by Name in a Python Module
Learn how to call a function by name in a Python module using strings, getattr, and safe patterns for dynamic function dispatch.