Advertisement
❮ Previous: Python Get Started & VirtualEnv Next: Python Syntax, Statements & Indentation ❯

Python Package Manager

PIP (which stands for PIP Installs Packages or Preferred Installer Program) is Python's standard package manager. It allows you to search, install, upgrade, and manage third-party software packages or libraries that are not included in Python’s standard library.


1. Checking If PIP Is Installed

PIP is included by default when you download and install modern versions of Python (Python 3.4+).

To check if PIP is already installed on your machine, open your terminal (macOS/Linux) or Command Prompt (Windows) and run:

pip --version

If PIP is not recognized, you can run it through Python directly:

python -m pip --version
# On macOS/Linux with python3 explicitly:
python3 -m pip --version


Advertisement

2. Basic PIP Commands

Here are the primary commands you will use daily to manage packages:

Installing Packages

To download and install a package from the Python Package Index (PyPI):

pip install package_name

Example: pip install requests

Installing a Specific Version

If your project requires a specific release:

pip install package_name==2.28.1
pip install "package_name>=2.0,<3.0"

Listing Installed Packages

To view every package currently installed in your environment:

pip list

Upgrading a Package

To update an already-installed package to its latest version:

pip install --upgrade package_name

Uninstalling a Package

To remove a package from your system or virtual environment:

pip uninstall package_name

Showing Package Information

To get details about an installed package (e.g., version, location, dependencies):

pip show package_name


Advertisement

3. Managing Project Dependencies (requirements.txt)

In Python projects, package dependencies are typically saved in a plain-text file named requirements.txt. This allows other developers (or deployment systems) to replicate your exact environment.

Generating requirements.txt

To export a list of all installed packages and their versions:

pip freeze > requirements.txt

Example contents of requirements.txt:

numpy==1.26.0
pandas==2.1.1
requests==2.31.0

Installing from requirements.txt

To install all the dependencies listed in a project's requirements.txt file:

pip install -r requirements.txt


Advertisement

4. Best Practices: Using PIP with Virtual Environments

Installing packages globally using pip install can lead to conflicts between projects that require different versions of the same package.

Recommended Workflow: Always create and activate an isolated virtual environment before running pip install:

# 1. Create a virtual environment named 'venv'
python -m venv venv

# 2. Activate the virtual environment
# On Windows (Command Prompt):
venv\Scripts\activate
# On Windows (PowerShell):
venv\Scripts\Activate.ps1
# On macOS/Linux:
source venv/bin/activate

# 3. Upgrade PIP inside the environment
python -m pip install --upgrade pip

# 4. Install your dependencies safely
pip install requests

❮ Previous: Python Get Started & VirtualEnv Next: Python Syntax, Statements & Indentation ❯
Advertisement