Python Modules


Modules in Python help you organise your code into separate files so you can reuse functions, variables and classes whenever you need them. Instead of writing the same code again and again, you can place it inside a module and simply import it in any program. Modules make your projects cleaner, easier to manage and more efficient.

In this tutorial, you will learn what modules are, how to create your own module, how to import modules in different ways, how to use built-in modules, how packages work, and how modules improve code structure in real projects.

What Is a Module?

A module is a Python file that contains functions, variables and classes. Every file with a .py extension automatically becomes a module that you can import in another program.

For example, if you create a file named math_tools.py, it becomes a module called math_tools.

Modules help you:

  • reuse code

  • keep files small

  • organise projects

  • avoid repeating logic

Why Modules Are Used

Modules allow you to split a large project into logical parts. You can keep related code together instead of creating one huge file. This makes debugging easier and helps you work faster.

Some common reasons to use modules:

  • You want to use the same function in many programs

  • Your code is getting too long

  • You want to separate tasks, such as calculations, input handling or file operations

  • You want to share your functions with others

Types of Modules in Python

Python supports three main types of modules:

  • Built-in modules (already available in Python)

  • User-defined modules (modules you create)

  • Third-party modules (installed using PIP)

You will work with all three in real projects.

Built-in Modules

Python has many modules ready to use. You don’t need to install anything. Just import and start using them.

Examples of built-in modules:

  • math

  • random

  • datetime

  • os

  • json

Example:

import math

print(math.sqrt(25))

Creating Your Own Module

To create a module, simply create a new .py file.

Example: greeting.py

def welcome(name):
    return "Hello " + name

Now you can use this module in another file.

Importing a Module

To import a module, use the import keyword.

Example:

import greeting

print(greeting.welcome("Aarohi"))

Here, greeting is the module and welcome is the function inside it.

Importing Specific Items

Instead of importing the whole module, you can import only what you need.

Example:

from greeting import welcome

print(welcome("Bhoomi"))

This saves time and makes the code cleaner.

Importing With an Alias

Sometimes module names are long, so you can use an alias.

Example:

import greeting as g

print(g.welcome("Tara"))

This approach is common in Python projects.

Module Variables

Modules can also hold variables.

Example: data.py

title = "Python Learning"
year = 2025

Use them like this:

import data

print(data.title)
print(data.year)

Using the dir() Function

The dir() function shows all functions and variables inside a module.

Example:

import math
print(dir(math))

This helps you explore what a module can do.

Working With Standard Library Modules

Python has a rich standard library. Here are a few useful examples.

math module example

import math
print(math.pi)
print(math.pow(2, 3))

random module example

import random
print(random.randint(1, 10))

datetime module example

import datetime
print(datetime.datetime.now())

Packages in Python

A package is a group of modules inside a folder. If you want to organise modules even more, you can use packages.

A package must contain a file named __init__.py. This file tells Python that the folder is a package.

Example structure:

myapp/
    __init__.py
    tools.py
    helpers.py

You can import modules like this:

from myapp import tools
from myapp.helpers import info

Packages help you build large-scale applications.

Real Life Example: Calculator Package

Imagine you want to build a calculator.

Folder:

calculator/
    __init__.py
    add.py
    subtract.py

File: add.py

def add(a, b):
    return a + b

File: subtract.py

def sub(a, b):
    return a - b

Use it:

from calculator.add import add
from calculator.subtract import sub

print(add(10, 5))
print(sub(10, 5))

This approach is clean and easy to maintain.

Practical Examples

  1. Importing a full module

import math
print(math.sqrt(16))
  1. Importing specific functions

from math import ceil
print(ceil(4.2))
  1. Using an alias

import random as r
print(r.choice([1, 2, 3]))
  1. User-defined module

import greeting
print(greeting.welcome("Kavya"))
  1. Module with variables

import data
print(data.title)
  1. Nested package import

from calculator.add import add
  1. Getting module info

import os
print(dir(os))
  1. Random number generation

import random
print(random.random())
  1. Current date and time

import datetime
print(datetime.date.today())
  1. Using math constants

import math
print(math.pi)

Summary of the Tutorial

Modules allow you to organise Python code into separate files so you can reuse functions, variables and classes. Python includes many built-in modules, and you can create your own or install third-party modules with PIP. You learned how to import modules, use aliases, explore module contents and create packages. Modules keep your programs clean, manageable and easy to extend as your projects grow.


Practice Questions

Q1. Write a Python program to import the math module and use the ceil() function to round up a number.

Q2. Write a Python program to use random.choice() to pick a random item from a list of your favorite fruits.

Q3. Write a Python program to import the datetime module with an alias dt and print the current date.

Q4. Write a Python program to create a module file calc.py with an add() function that adds two numbers.

Q5. Write a Python program to import add() from the calc module and use it in your main script.

Q6. Write a Python program to use from math import pi and print the value of pi.

Q7. Write a Python program to try importing a non-existing module and handle the ModuleNotFoundError using try/except.

Q8. Write a Python program to use the os module to get and print the current working directory.

Q9. Write a Python program to use dir() to list all attributes and functions of the datetime module.

Q10. Write a Python program to call a function from your own custom module stored in a separate .py file.


Try a Short Quiz.

coding learning websites codepractice

No quizzes available.

Go Back Top