pip is the official package manager for Python. It allows you to install, update, and remove external libraries (packages) from the Python Package Index.
With pip, you can easily add powerful libraries to your Python projects without writing everything from scratch.
Example popular packages installed with pip:
- NumPy β numerical computing
- Pandas β data analysis
- Requests β HTTP requests
- Flask / Django β web development
π§ What is pip?
pip stands for βPip Installs Packages.β
It helps you:
β Install new Python libraries
β Upgrade existing packages
β Remove packages
β Manage project dependencies
π₯ Checking if pip is Installed
Most modern Python versions install pip automatically.
Check by running:
pip --version
Example output:
pip 24.0 from Python312
If pip is not installed:
python -m ensurepip --upgrade
π¦ Installing a Package
Use the following command:
pip install package_name
Example:
pip install requests
This installs the Requests library used for making HTTP requests.
Example Python code using it:
import requests
response = requests.get("https://example.com")
print(response.status_code)
β¬οΈ Upgrading a Package
To upgrade a package to the latest version:
pip install --upgrade package_name
Example:
pip install --upgrade requests
β Uninstalling a Package
To remove a package:
pip uninstall package_name
Example:
pip uninstall requests
π Listing Installed Packages
To see all installed packages:
pip list
Example output:
Package Version
------------ -------
requests 2.31.0
numpy 1.26.4
π Installing from requirements.txt
Projects often store dependencies in a requirements.txt file.
Example:
requests
numpy
pandas
Install all packages with:
pip install -r requirements.txt
π Searching for Packages
You can search for packages on the Python Package Index (PyPI) website.
Website:
https://pypi.org
Example packages:
- requests
- numpy
- pandas
- flask
π§ Why pip is Important
Benefits:
β Easy installation of libraries
β Manages dependencies automatically
β Access to thousands of packages
β Essential for modern Python development
π§Ύ Example Workflow Using pip
Install libraries for a project:
pip install flask
pip install pandas
pip install matplotlib
Then import them in Python:
import flask
import pandas
import matplotlib
β Summary
| Command | Purpose |
|---|---|
pip install package | Install a package |
pip uninstall package | Remove a package |
pip list | Show installed packages |
pip install --upgrade package | Upgrade package |
pip install -r requirements.txt | Install project dependencies |