7 Packages
1 What Are Packages?
A package is a collection of functions, constants, and classes that extend Python’s functionality.
It’s similar to R packages such as ggplot2
or dplyr
, which you load to gain new features.
We load a package in Python with the import
command:
import package_name
Example:
import math
Once imported, you can use all the tools the package provides.
1.1 Why Use Packages?
- To reuse existing, well-tested code
- To extend Python beyond its base features
- To make code modular and easier to maintain
Examples of common packages:
math
— mathematical functionsstatistics
— summary statisticsnumpy
— numerical computingpandas
— data manipulationmatplotlib
— data visualization
Just as R has CRAN, Python packages are distributed through the Python Package Index (PyPI) and Conda repositories.
2 Installing Packages for the First Time
Some packages are not included with base Python, so we need to install them before importing.
2.1 Installing with pip
pip
is Python’s built-in package manager. Run the following in your command prompt or terminal (not inside Python):
pip install package_name
Examples:
pip install numpy
pip install pandas
pip install matplotlib
Similar in R:
install.packages("ggplot2")
library(ggplot2)
2.1.1 Updating or Removing Packages
pip install --upgrade package_name
pip uninstall package_name
2.1.2 Viewing Installed Packages
pip list
pip show package_name
2.2 Installing with Conda (for Anaconda Users)
If you are using the Anaconda distribution, it comes with its own package manager: conda
. You can install or update packages as follows:
conda install package_name
Examples:
conda install numpy
conda install pandas
To install from a specific channel (like conda-forge
):
conda install -c conda-forge seaborn
Both pip
and conda
achieve the same goal, which is to install packages, but Conda is often preferred for large scientific libraries such as NumPy or SciPy because it handles binary dependencies better.
3 Namespaces
When a package is imported, Python creates a namespace to store its contents. Namespaces prevent accidental overwrites. For example, your own mean
will not clash with statistics.mean
if you keep the package prefix.
We access its objects using the format:
package_name.object_name
Example:
import math
print(math.sqrt(25))
In R, this is similar to:
::rnorm(5) stats
4 The math
Package
The math
package provides basic mathematical functions and constants.
import math
print("√20 =", math.sqrt(20))
print("10! =", math.factorial(10))
print("e^5 =", math.exp(5))
print("log₁₀(100) =", math.log(100, 10))
It also includes constants and trigonometric functions:
print(math.pi)
print(math.sin(math.pi/3))
print(math.cos(math.pi/3))
print(math.tan(math.pi/3))
5 Using Aliases
If a package name is long, we can give it a short alias to make code easier to write.
import math as mt
print(mt.pi)
print(mt.sin(mt.pi/4))
from math import pi as PI
print(PI)
Common aliases:
Package | Common Alias |
---|---|
numpy |
np |
pandas |
pd |
matplotlib.pyplot |
plt |
6 Importing Specific Objects
We can import only specific functions or constants instead of the entire package:
from math import pi, sin
print(pi)
print(sin(pi/2))
Similar to pkg::fun()
in R. You import only what you name.
7 Importing All Objects (Not Recommended)
from math import *
This brings everything from the package into your workspace. However, it can:
- Overwrite existing variable names
- Make your code less readable
In R, library(pkg)
exposes all exported functions by design. In Python, doing the same with from pkg import *
is discouraged.
8 R vs Python Package Comparison
Task | R Command | Python Command | Remarks |
---|---|---|---|
Install a package | install.packages("ggplot2") |
pip install matplotlib or conda install matplotlib |
Both install packages from official repositories (CRAN / PyPI / Conda). |
Load a package | library(ggplot2) |
import matplotlib |
R loads all exported functions directly; Python keeps them under a namespace. |
Use alias for a package | Not available | import numpy as np |
R has no aliasing system for package names. |
Access specific function | stats::rnorm(10) |
math.sqrt(25) |
Both use the package name followed by the function name. |
Import specific functions | dplyr::select() |
from math import pi, sin |
R uses double colons for selective access; Python explicitly imports functions. |
Import all objects | library(ggplot2) (default behavior) |
from math import * |
Both expose all functions; in Python it’s discouraged, in R it’s standard. |
Update a package | update.packages("ggplot2") |
pip install --upgrade matplotlib or conda update matplotlib |
Both update to the newest version available. |
Uninstall / remove a package | remove.packages("ggplot2") |
pip uninstall matplotlib or conda remove matplotlib |
Removes the installed package. |
List installed packages | installed.packages() |
pip list or conda list |
Shows all installed packages. |
Show details of a package | Not available | pip show pandas or conda show pandas |
Displays version, author, and installation path. |
Install from community source | remotes:: install_github("user/pkg") |
conda install -c conda-forge seaborn |
Installs from community repositories (GitHub / Conda-Forge). |
9 Summary
- Packages extend Python’s functionality just like R packages.
- Use
pip
orconda
for installation. - Always use aliases or selective imports for clarity.
- Avoid
from package import *
. - Installation is a one-time step. Once installed, just
import
when needed.