- Python’s power comes from its modular ecosystem. Instead of rewriting functionality, you can use packages collections of reusable modules and install additional external libraries using PIP, Python’s package manager.
What is PIP?¶
PIP stands for "Pip Installs Packages". It is the official package manager for Python, allowing you to:
- Install packages (libraries/modules) from the Python Package Index (PyPI).
- Manage dependencies required for your Python projects.
- Uninstall or upgrade packages when needed.
What is a Python Package?¶
A Python package is a directory that contains multiple modules and includes an optional
__init__.pyfile. In most cases:- A module is a single
.pyfile (Python script). - A package is a collection of modules bundled for reuse.
- A module is a single
These packages can be installed via PIP and imported into your project to extend its capabilities.
Basic structure
my_package/
__init__.py
module_a.py
module_b.py
Understanding Modules vs Packages vs Libraries¶
| Concept | Meaning | Example |
|---|---|---|
| Module | A single .py file containing code |
math.py |
| Package | A directory of modules + __init__.py |
numpy/ |
| Library | A collection of packages | TensorFlow, Scikit-Learn |
| Framework | Large structured library providing full workflows | Django, Flask |
Checking if PIP is Installed¶
- PIP is included by default in Python versions 3.4 and above. To verify whether PIP is already installed on your system:
pip --version
- If you see a version number, it means PIP is installed.
In [2]:
pip --version
pip 24.1.2 from /usr/local/lib/python3.12/dist-packages/pip (python 3.12)
Installing PIP (If Not Already Installed)¶
- If running
pip --versionthrows an error, PIP might not be installed. To install it:
- Visit the official PIP installation page: https://pypi.org/project/pip/
- Alternatively, if using Python from python.org, PIP comes bundled with Python 3.4 and above.
Installing a Python Package using PIP¶
- To install a package, use the
pip install <package-name>command.
pip install numpy
Using an Installed Package in Python¶
- Once a package is installed using PIP, it can be imported and used in your project to add powerful features.
Example: Using numpy for Numerical Computation¶
In [3]:
# Make sure you've installed numpy using:
# pip install numpy
# Importing numpy after installing it with PIP
import numpy as np
# Creating a numerical array
arr = np.array([2, 4, 6, 8])
# Performing a mathematical operation
squared = arr ** 2
print("Original array:", arr)
print("Squared array:", squared)
# Expected Output:
# Original array: [2 4 6 8]
# Squared array: [ 4 16 36 64 ]
Original array: [2 4 6 8] Squared array: [ 4 16 36 64]
Searching for Packages on PyPI¶
- When you need a new feature or tool in your project, PyPI is the primary place to discover reliable, community-maintained packages.
- You can search directly from the website, or use command-line tools to locate relevant packages.
Searching Directly on the PyPI Website¶
- You can explore thousands of available packages on: https://pypi.org
Searching from the Command Line Using pip¶
- pip also provides a basic search feature:
pip search "machine learning"
This command queries PyPI and returns a list of packages whose metadata matches the given phrase.
Note: Some systems disable pip search due to API changes, but many environments still support it.
Uninstalling a Package¶
- If you no longer need a package, use the uninstall command:
In [6]:
pip uninstall numpy
Found existing installation: numpy 2.0.2
Uninstalling numpy-2.0.2:
Would remove:
/usr/local/bin/f2py
/usr/local/bin/numpy-config
/usr/local/lib/python3.12/dist-packages/numpy-2.0.2.dist-info/*
/usr/local/lib/python3.12/dist-packages/numpy.libs/libgfortran-040039e1-0352e75f.so.5.0.0
/usr/local/lib/python3.12/dist-packages/numpy.libs/libquadmath-96973f99-934c22de.so.0.0.0
/usr/local/lib/python3.12/dist-packages/numpy.libs/libscipy_openblas64_-99b71e71.so
/usr/local/lib/python3.12/dist-packages/numpy/*
Proceed (Y/n)? y
Successfully uninstalled numpy-2.0.2
- Press
yto confirm and complete the uninstallation.
Show information about package¶
- The pip show command displays detailed information about a specific installed Python package
pip show <package_name>
Example:
pip show numpy
Listing All Installed Packages¶
- To see all installed Python packages in your environment:
pip list
Output¶
Package Version
------------------------------------- -------------------
absl-py 1.4.0
accelerate 1.8.1
aiofiles 24.1.0
aiohappyeyeballs 2.6.1
aiohttp 3.11.15
.............................................
.............................................
Upgrading an Existing Package¶
- To upgrade a package to the latest version:
pip install --upgrade pandas
- You can also upgrade pip itself:
pip install --upgrade pip
Saving and Reproducing Environments¶
pip help¶
- To see a list of available commands and options:
pip help
- This shows general help information, including the most commonly used commands.
Summary of PIP Commands¶
| Task / Description | Example |
|---|---|
| Install packages. | pip install numpy |
| Download packages without installing. | pip download pandas |
| Uninstall packages. | pip uninstall matplotlib |
| Output installed packages in requirements format. | pip freeze |
| Inspect the Python environment. | pip inspect |
| List installed packages. | pip list |
| Show information about installed packages. | pip show numpy |
| Verify installed packages have compatible dependencies. | pip check |
| Search PyPI for packages. | pip search numpy |
| Show information useful for debugging. | pip debug |
| Show help for pip commands. | pip help / pip install --help |