Python Modules Explained

A Python module is one of the main ways Python keeps code organized and reusable.

In simple terms, a module is usually a Python file that contains code such as variables, functions, or classes. You can then use that code in another Python file instead of writing it again.

This page explains what modules are, why they matter, and how beginners use them. It keeps the focus on the idea of modules, not the full details of import syntax or packages.

import math

print(math.sqrt(25))

Quick example: a module is a Python file you can import to use code written elsewhere.

What this page teaches

  • What a module is in simple terms
  • How modules help organize code
  • The difference between your own modules and built-in modules
  • A beginner-level introduction to importing

What is a Python module?

A Python module is usually a single .py file.

That file can contain:

  • Variables
  • Functions
  • Classes

The main idea is simple: if you write useful code in one file, you can reuse it from another file.

For example, instead of writing the same function in many places, you can put it in one module and import it when needed.

Modules help you:

  • Avoid repeating code
  • Keep related code together
  • Make programs easier to read

Why modules are useful

Modules are useful because they help you split code into smaller, clearer parts.

Instead of putting everything in one long file, you can organize code by topic. For example:

  • One file for math helpers
  • One file for file handling
  • One file for user input

This makes larger programs easier to manage.

Modules also let you reuse code in multiple files. If you improve a function in one module, every file that imports it can use the updated version.

Another big benefit is that Python already includes many useful modules in its standard library. For example, you can use modules for math, JSON data, file paths, and system information without writing those tools yourself.

Types of modules beginners should know

There are two main types of modules beginners should understand.

Your own modules

These are Python files you create yourself.

Example:

  • helpers.py
  • tools.py
  • greetings.py

If a file contains Python code, it can often be used as a module.

Built-in and standard library modules

These are modules that come with Python.

Examples include:

  • math
  • json
  • os
  • sys

You can learn more from pages like the Python math module overview, Python json module overview, Python os module overview, and Python sys module overview.

What about third-party packages?

Third-party packages are code written by other people and installed separately.

They are different from a simple module, so this page does not cover them in depth. If you want the bigger picture, it also helps to understand what a package is in Python.

Basic example of using a module

A common beginner example is the math module.

import math

result = math.sqrt(25)
print(result)

Output:

5.0

What is happening here:

  • import math makes the math module available
  • math.sqrt(25) uses the sqrt() function from that module
  • The dot means “use this item from that module”

This is one of the most common patterns in Python:

module_name.function_name()

If you want to go deeper into import syntax later, read How Import Works in Python.

Creating your own module

You can create your own module with just two files.

File 1: greetings.py

def say_hello(name):
    return f"Hello, {name}!"

This file is your module. It contains one function called say_hello.

File 2: main.py

import greetings

message = greetings.say_hello("Maya")
print(message)

Output:

Hello, Maya!

How this works

For this simple example:

  • Put both files in the same folder
  • Run main.py
  • Python imports the code from greetings.py

The key line is:

greetings.say_hello("Maya")

That means:

  • Use the module named greetings
  • Then use the function named say_hello

Important beginner idea: module name dot function name

A very important pattern in Python is:

module_name.function_name()

Example:

math.sqrt(25)

The dot means:

  • start with the module
  • then access something inside it

This is helpful because it keeps names clear.

For example, many modules could have a function with the same name. Using the module name helps avoid confusion.

import math

print(math.sqrt(16))

Here, Python knows you want the sqrt() function from the math module.

What this page does not cover in depth

This page focuses on the basic idea of modules.

It does not explain these topics in detail:

Common beginner mistakes

Here are some common module-related mistakes.

Naming your file the same as a standard module

If you create a file named math.py, it can conflict with Python’s real math module.

That can cause confusing import problems.

Forgetting that Python must be able to find the file

If your own module is in the wrong folder, Python may not find it.

In simple beginner examples, keep both files in the same folder.

If import fails, pages like ModuleNotFoundError: No module named X and ImportError: No module named X can help.

Trying to use a function before importing the module

This will fail:

print(math.sqrt(25))

Because math has not been imported yet.

You need:

import math

print(math.sqrt(25))

Confusing modules with packages

A module is usually one file.

A package is a way to organize multiple modules together.

FAQ

What is a module in Python?

A module is usually a Python file that contains code you can reuse in another Python file.

What is the difference between a module and a package?

A module is usually one file. A package is a way to organize multiple modules together.

Do I need pip to use modules?

No. Many modules come with Python already, and you can also create your own modules.

Why does import fail for my own module?

Common reasons are:

  • The file is in the wrong folder
  • The name is wrong
  • The filename conflicts with another module

See also