Skip to content

Modules & Packages

Modules are simply Python files with a .py extension. You can use their contents in your own programs using the import statement.

Modules in Python

To use a module’s functions, you can import the entire module and access its contents with dot notation.

import math
print(math.sqrt(16)) # Output: 4.0

Alternatively, you can import specific functions directly into your program’s namespace, so you don’t need to use the module name.

from math import sqrt
print(sqrt(16)) # Output: 4.0

To import all functions and variables from a module, you can use the * wildcard. However, this is generally not recommended as it can lead to name conflicts.

from math import *
print(ceil(4.2)) # Output: 5

The dir() function can be useful to see all the available attributes and functions within a module.

import math
print(dir(math))

The random module is used for generating random numbers and sequences.

  • random.choice(seq): Returns a random element from a sequence.
  • random.randrange(start, stop, step): Returns a random integer from a specified range.
  • random.random(): Returns a random float between 0.0 and 1.0.
  • random.seed(x): Sets the seed to ensure that random numbers are reproducible.
  • random.shuffle(lst): Shuffles the items in a list randomly in-place.

The math module provides a wide range of mathematical functions and constants.

  • math.ceil(x): Rounds a number up to the nearest integer.
  • math.floor(x): Rounds a number down to the nearest integer.
  • math.sqrt(x): Returns the square root of a number.
  • math.pow(x, y): Returns x raised to the power of y.
  • abs(x): A built-in function that returns the absolute value of a number.

Do we need to know about all these modules & Its method ??

Section titled “Do we need to know about all these modules & Its method ??”

No, We don’t need to learn about these modules. We need to use them as per our need. The fact is, modules are made by some other developers, the team or individual developer who develops the module provides its documentation like how to use this package!

For Example, Explore the Module made by me which got 2.5k Mothly downloads till now - ahcodecompiler and still counting!!!


A package is a way to organize related modules into a directory structure. A directory becomes a Python package if it contains a special __init__.py file (which can be empty).

A typical package structure looks like this:

mypackage/
├── __init__.py
├── math_operations.py
└── string_operations.py
  • __init__.py makes the mypackage directory recognizable as a package.
  • math_operations.py and string_operations.py are the modules within the package.

To use the package, you can import its modules or specific functions.

# From math_operations.py
def add(a, b):
return a + b
# From __init__.py
from .math_operations import add

With this setup, you can import and use the function like this:

from mypackage import add
print(add(2, 3)) # Output: 5

pip is the standard package manager for Python. It allows you to install, update, and manage third-party libraries from the Python Package Index (PyPI), such as NumPy for numerical operations or Flask for web development.

  • Install a package: pip install package_name
  • Install multiple packages: pip install numpy pandas
  • List installed packages: pip list
  • Upgrade a package: pip install --upgrade package_name
  • Uninstall a package: pip uninstall package_name