Skip to content
X

pip - Standard Python Package Manager

pip is Python’s built-in package manager. You can install and manage Python libraries and tools with a single command.

Just as npm comes with Node.js, pip comes with Python. Think of it as the “App Store for Python.” You can get more than 500,000 packages from the official repository called PyPI with a single command.

When you develop data analysis or AI projects in Python, the standard library is usually not enough. For example:

  • Data analysis often needs pandas for data handling and numpy for numerical computation
  • AI / machine learning often needs scikit-learn or torch (PyTorch)
  • Web development often needs flask or fastapi

Manually downloading and placing these packages is not practical. With pip, dependencies are installed automatically together with the packages they need.

Python 3.4 and later include pip by default. First check whether pip is available:

pip --version

If you see something like the following, the check succeeded:

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

If pip: command not found appears, try pip3:

pip3 --version

On macOS, python may point to Python 2, and the Python 3 version of pip may be named pip3. This document uses pip from here on, but you can read it as pip3 when your environment requires it.

CommandDescriptionWhen to Use
pip install <pkg>Install a packageWhen you want to add a new library
pip install <pkg>==1.2.3Install a specific versionWhen a specific version is required
pip uninstall <pkg>Uninstall a packageWhen it is no longer needed
pip listList installed packagesWhen you want to see what is installed
pip freezeOutput installed packages with versionsWhen creating requirements.txt
pip show <pkg>Show package detailsWhen checking version or install location
pip install -r requirements.txtInstall packages listed in a fileWhen setting up a project
# Install Flask
pip install flask

# Install a specific version
pip install flask==3.0.0

# Show installed packages
pip list

# Show detailed information
pip show flask

Example output from pip show flask:

Name: Flask
Version: 3.0.3
Summary: A simple framework for building complex web applications.
Location: /usr/local/lib/python3.12/site-packages
Requires: blinker, click, itsdangerous, jinja2, werkzeug

requirements.txt is a text file that records the packages and versions needed by a project. Think of it as the project’s ingredient list.

When working in a team or moving a project to another computer, you need other people to reproduce the packages you installed, with the same versions. With requirements.txt, you can recreate the same environment with one command.

Write the packages currently installed in your environment to requirements.txt:

pip freeze > requirements.txt

Example requirements.txt:

flask==3.0.3
blinker==1.8.2
click==8.1.7
itsdangerous==2.2.0
jinja2==3.1.4
markupsafe==2.1.5
werkzeug==3.0.3

When someone else sets up the project, or when you are setting up a new environment, run:

pip install -r requirements.txt

This installs all packages listed in requirements.txt at the same versions.

requirements.txt should be tracked in Git. Anyone who joins the project can run pip install -r requirements.txt and recreate the same environment.

A virtual environment is a separate Python runtime environment for each project. Think of it as a separate workroom for each project.

If you install packages with pip without a virtual environment, they go into the global environment for the whole computer. That causes problems such as:

  • Project A needs flask 2.x while Project B needs flask 3.x, and both cannot be used at the same time
  • Packages from one project affect another project

Virtual environments let you manage packages separately for each project.

venv is the virtual environment tool included with Python 3.3 and later.

Run this command inside your project folder:

python -m venv venv

This creates a venv folder inside your project:

my-project/
├── venv/          ← virtual environment (packages are stored here)
│   ├── bin/
│   ├── lib/
│   └── ...
└── main.py
# macOS / Linux
source venv/bin/activate

# Windows (Command Prompt)
venv\Scripts\activate.bat

# Windows (PowerShell)
venv\Scripts\Activate.ps1

When it is active, (venv) appears at the start of the prompt.

(venv) $

When pip install is run inside the virtual environment, packages are installed there instead of globally.

(venv) $ pip install flask

Step 4: Deactivate the virtual environment

Section titled “Step 4: Deactivate the virtual environment”

When you are done, leave the environment with:

deactivate

The venv folder should not be tracked in Git. It is large and differs by OS and version, so add it to .gitignore:

# .gitignore
venv/
__pycache__/
*.pyc

The setup process for other people looks like this:

# Clone the repository
git clone <repository-url>
cd <project-folder>

# Create and activate the virtual environment
python -m venv venv
source venv/bin/activate

# Install the required packages
pip install -r requirements.txt

As a practical example with pip and venv, try running a Flask web server.

# 1. Create a project folder and move into it
mkdir my-flask-app
cd my-flask-app

# 2. Create and activate a virtual environment
python -m venv venv
source venv/bin/activate   # macOS / Linux

# 3. Install Flask
pip install flask

# 4. Confirm the installation
pip show flask

Next, create app.py:

# app.py
from flask import Flask

app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello, World!"

if __name__ == "__main__":
    app.run(debug=True)
# 5. Start the server
python app.py

After the server starts, open http://localhost:5000 in your browser and you will see “Hello, World!”.

# 6. Create requirements.txt to share with others
pip freeze > requirements.txt

# 7. Deactivate the virtual environment when finished
deactivate

Cause: Python is not installed or the PATH is not set.

Fix: First try pip3.

pip3 --version

If that still does not help, check whether Python is installed correctly:

python3 --version

If Python is installed, you can enable pip with:

python3 -m ensurepip --upgrade

ModuleNotFoundError: No module named 'xxx'

Section titled “ModuleNotFoundError: No module named 'xxx'”

Cause: The package is not installed, or it was installed outside the virtual environment.

Fix: Confirm that the virtual environment is active, then install the package:

# Activate the virtual environment (look for (venv) in the prompt)
source venv/bin/activate

# Install the package
pip install <package-name>

ERROR: Could not install packages due to an OSError

Section titled “ERROR: Could not install packages due to an OSError”

Cause: Not enough disk space, or a permissions problem.

Fix: Check whether you are using a virtual environment. Permission errors are common when installing globally. Inside a virtual environment, you can install without sudo.

Large packages or packages with many dependencies can take time to install. If you need a faster package manager, consider uv, which is introduced in the next step.

  • pip is Python’s standard package manager and comes with Python
  • You can install libraries with pip install <package-name>
  • requirements.txt records and reproduces project dependencies
  • Combine pip with venv to manage separate environments per project
  • Add the venv folder to .gitignore so Git does not track it

Q: What is the difference between pip and pip3?

A: pip and pip3 differ in which Python version they target. On macOS, python can point to Python 2, and in that case pip may also target Python 2. If you are using Python 3, pip3 is the safer choice. Inside a virtual environment, pip automatically targets Python 3.

Q: What is the difference between requirements.txt and package.json (npm)?

A: They serve similar roles, but requirements.txt is a simple text file generated with pip freeze. package.json is JSON and also includes project names and scripts. In Python, pyproject.toml is closer to package.json.

Q: What is the difference between venv and conda (Anaconda)?

A: venv is Python’s built-in virtual environment tool and manages only Python packages. conda is a package manager aimed at data science and can manage non-Python packages such as C libraries. For general development, venv is lighter and simpler.

Q: Do I have to use a virtual environment?

A: It is not mandatory, but it is strongly recommended. If you keep installing packages globally with pip, version conflicts across projects become more likely. Using virtual environments helps prevent the “works on my machine, fails elsewhere” problem.