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.
Why pip Is Needed
Section titled “Why pip Is Needed”When you develop data analysis or AI projects in Python, the standard library is usually not enough. For example:
- Data analysis often needs
pandasfor data handling andnumpyfor numerical computation - AI / machine learning often needs
scikit-learnortorch(PyTorch) - Web development often needs
flaskorfastapi
Manually downloading and placing these packages is not practical. With pip, dependencies are installed automatically together with the packages they need.
Checking the Installation
Section titled “Checking the Installation”Python 3.4 and later include pip by default. First check whether pip is available:
pip --versionIf 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 --versionOn 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.
Basic Commands
Section titled “Basic Commands”| Command | Description | When to Use |
|---|---|---|
pip install <pkg> | Install a package | When you want to add a new library |
pip install <pkg>==1.2.3 | Install a specific version | When a specific version is required |
pip uninstall <pkg> | Uninstall a package | When it is no longer needed |
pip list | List installed packages | When you want to see what is installed |
pip freeze | Output installed packages with versions | When creating requirements.txt |
pip show <pkg> | Show package details | When checking version or install location |
pip install -r requirements.txt | Install packages listed in a file | When setting up a project |
Example Commands
Section titled “Example Commands”# 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 flaskExample 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, werkzeugHow to Use requirements.txt
Section titled “How to Use requirements.txt”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.
Why requirements.txt Is Needed
Section titled “Why requirements.txt Is Needed”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.
Creating requirements.txt
Section titled “Creating requirements.txt”Write the packages currently installed in your environment to requirements.txt:
pip freeze > requirements.txtExample 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.3Installing from requirements.txt
Section titled “Installing from requirements.txt”When someone else sets up the project, or when you are setting up a new environment, run:
pip install -r requirements.txtThis installs all packages listed in requirements.txt at the same versions.
Commit requirements.txt to Git
Section titled “Commit requirements.txt to Git”requirements.txt should be tracked in Git. Anyone who joins the project can run pip install -r requirements.txt and recreate the same environment.
Working with Virtual Environments (venv)
Section titled “Working with Virtual Environments (venv)”What a Virtual Environment Is
Section titled “What a Virtual Environment Is”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.xwhile Project B needsflask 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.
How to Use venv
Section titled “How to Use venv”venv is the virtual environment tool included with Python 3.3 and later.
Step 1: Create a virtual environment
Section titled “Step 1: Create a virtual environment”Run this command inside your project folder:
python -m venv venvThis creates a venv folder inside your project:
my-project/
├── venv/ ← virtual environment (packages are stored here)
│ ├── bin/
│ ├── lib/
│ └── ...
└── main.pyStep 2: Activate the virtual environment
Section titled “Step 2: Activate the virtual environment”# macOS / Linux
source venv/bin/activate
# Windows (Command Prompt)
venv\Scripts\activate.bat
# Windows (PowerShell)
venv\Scripts\Activate.ps1When it is active, (venv) appears at the start of the prompt.
(venv) $Step 3: Install packages
Section titled “Step 3: Install packages”When pip install is run inside the virtual environment, packages are installed there instead of globally.
(venv) $ pip install flaskStep 4: Deactivate the virtual environment
Section titled “Step 4: Deactivate the virtual environment”When you are done, leave the environment with:
deactivateDo Not Commit the venv Folder to Git
Section titled “Do Not Commit the venv Folder to Git”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__/
*.pycThe 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.txtPractical Example: Hello World with Flask
Section titled “Practical Example: Hello World with Flask”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 flaskNext, 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.pyAfter 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
deactivateCommon Errors and Fixes
Section titled “Common Errors and Fixes”pip: command not found
Section titled “pip: command not found”Cause: Python is not installed or the PATH is not set.
Fix: First try pip3.
pip3 --versionIf that still does not help, check whether Python is installed correctly:
python3 --versionIf Python is installed, you can enable pip with:
python3 -m ensurepip --upgradeModuleNotFoundError: 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.
pip install Is Slow
Section titled “pip install Is Slow”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.
Summary
Section titled “Summary”- pip is Python’s standard package manager and comes with Python
- You can install libraries with
pip install <package-name> requirements.txtrecords and reproduces project dependencies- Combine pip with
venvto manage separate environments per project - Add the
venvfolder to.gitignoreso Git does not track it
Next Step
Section titled “Next Step”Frequently Asked Questions
Section titled “Frequently Asked Questions”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.