pip & Packages in Python
Learn how to install, manage and use external Python packages with pip.
What is pip?
pip is the standard package installer used to install Python packages from the Python Package Index and other configured package sources.
A package contains reusable Python code developed for a particular purpose.
What is a Python Package?
A Python package is reusable code that can be added to a project to provide additional functionality.
For example, developers can use external packages for web development, data analysis, machine learning, automation and many other tasks.
pip & Packages Diagram
The basic flow of installing and using a package can be understood like this:
Python Project
Your application
pip
Package installation tool
Package
Reusable functionality
Install
pip install package-name
Use
import package
Python Application
Use the installed package in your program.
Check Whether pip is Installed
We can check whether pip is available by using the following command in the terminal.
pip --version
Another commonly used form is:
python -m pip --version
The command displays the installed pip version.
Installing a Package
To install a package, use:
pip install package_name
For example:
pip install requests
This tells pip to install the
requests package.
Example: Installing requests
First install the package:
pip install requests
Then use it in Python:
import requests
response = requests.get("https://example.com")
print(response.status_code)
The package provides functionality that is not part of the basic Python syntax.
Upgrade a Package
We can upgrade an installed package using:
pip install --upgrade package_name
Example:
pip install --upgrade requests
This asks pip to update the package to a newer available version.
Uninstall a Package
To remove an installed package:
pip uninstall package_name
Example:
pip uninstall requests
pip will ask for confirmation before removing the package.
View Installed Packages
We can display packages installed in the current Python environment using:
pip list
This displays package names and their installed versions.
View Package Details
To see information about a particular package:
pip show requests
This can display information such as the package version and installation location.
Using pip freeze
The following command displays installed packages in a format that can be saved as a requirements file.
pip freeze
Example output:
requests==2.x.x
urllib3==2.x.x
certifi==202x.x.x
The exact versions depend on the environment.
requirements.txt
A requirements.txt file is commonly used
to list the Python packages required by a project.
Example:
requests==2.x.x
flask==3.x.x
Other developers can install the listed dependencies using:
pip install -r requirements.txt
This is useful when sharing or deploying a Python project.
Upgrade pip
pip itself can be upgraded with:
python -m pip install --upgrade pip
Using python -m pip is a common way to
make sure pip is associated with the intended Python
interpreter.
Common pip Commands
| Command | Purpose |
|---|---|
pip --version
|
Check pip version. |
pip install package_name
|
Install a package. |
pip install --upgrade package_name
|
Upgrade a package. |
pip uninstall package_name
|
Remove a package. |
pip list
|
Display installed packages. |
pip show package_name
|
Display package information. |
pip freeze
|
Display installed packages and versions. |
pip install -r requirements.txt
|
Install dependencies listed in a file. |
Built-in Modules vs External Packages
| Built-in / Standard Library | External Package |
|---|---|
| Available with Python's standard library. | Usually installed separately. |
Example: math
|
Example: requests
|
| Usually no separate pip installation is required. | Commonly installed using pip. |
Package Management Flow
Step 1: Create or open your Python project.
↓
Step 2: Identify the package required.
↓
Step 3: Install it using pip.
↓
Step 4: Import the package in your Python code.
↓
Step 5: Use the package functionality.
↓
Step 6: Save project dependencies when required.
Common Mistakes
- Installing a package in a different Python environment than the one used by the project.
- Using an incorrect package name.
- Forgetting to install a required dependency.
- Not maintaining project dependencies properly.
- Confusing a Python module with an externally installed package.
Practical Exercise
Try these commands in your terminal:
pip --version
pip install requests
pip show requests
pip list
pip freeze
Then create a Python file and import the installed package.
import requests
print("Requests package is ready to use.")
Summary
pip is used to install and manage Python packages.
We can install, upgrade, uninstall and inspect packages
using pip commands. The requirements.txt
file helps record project dependencies, while
pip freeze can be used to generate a list
of installed package versions.