GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Python Tutorial
  • Data Types
  • Interview Questions
  • Examples
  • Quizzes
  • DSA Python
  • Data Science
  • NumPy
  • Pandas
  • Practice
  • Django
  • Flask
  • Introduction1 min read
  • Input & Output2 min read

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — Python Modules - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • Python Tutorial
  • Data Types
  • Interview Questions
  • Examples
  • Quizzes
  • DSA Python
  • Data Science
  • NumPy
  • Pandas
  • Practice
  • Django
  • Flask

Python Modules

Last Updated : 19 Sep, 2026

Module is a file containing definitions and statements. A module can define functions, classes and variables. Modules help organize code into separate files so that programs become easier to maintain and reuse. Instead of writing everything in one place, related functionality can be grouped into its own module and imported whenever needed.

Create a Module

To create a module, write the desired code and save that in a file with .py extension.

Example: Let's create a calc.py in which we define two functions, one add and another subtract.

Python
# calc.py
def add(x, y):
    return (x+y)

def subtract(x, y):
    return (x-y)

This is all that is required to create a module.

Import module

Modules can be used in another file using the import statement. When Python sees an import, it loads the module if it exists in the interpreter’s search path. Below is the syntax to import a module:

import module

Example: Here, we are importing the calc that we created earlier to perform add operation.

Python
import calc
print(calc.add(10, 2))

Output

12

Explanation: import calc loads the module and calc.add() accesses a function through dot notation.

Types of Import Statements

1. Import From Module: This allows importing specific functions, classes, or variables rather than the whole module.

Python
from math import sqrt, factorial
print(sqrt(16))
print(factorial(6))

Output
4.0
720

Explanation: Only sqrt and factorial are brought into the local namespace, so the prefix math. is not required.

2. Import All Names: * imports everything from a module into the current namespace.

Python
from math import *
print(sqrt(16))
print(factorial(6))

Output
4.0
720

Explanation: Every public name of math becomes directly accessible. (Not recommended in large projects due to namespace conflicts.)

3. Import With Alias: You can shorten a module's name using as.

Python
import math as m
print(m.pi)

Output
3.141592653589793

Explanation: math is accessed through the shorter alias m.

Types of Modules

Python provides several kinds of modules. Each type plays a different role in application development.

1. Built-in Modules: These come bundled with Python and require no installation - e.g., math, random, os.

Python
import random
print(random.randint(1, 5))

Output
4

Explanation: random.randint() returns a random number within the given range.

2. User-Defined Modules: These are modules you create yourself, such as calc.py.

Python
import calc
print(calc.subtract(20, 5))

Output

15

Explanation: The module is created manually and then imported into another script.

3. External (Third-Party) Modules: These modules are installed using pip - e.g., NumPy, Pandas, Requests.

Python
import requests
r = requests.get("https://example.com")
print(r.status_code)

Output

200

Explanation: requests is installed separately (pip install requests) and provides HTTP utilities.

4. Package Modules: A package is a directory containing multiple modules, usually with an __init__.py file.

Example Directory

mypkg/
__init__.py
calc.py
utils.py

Using a module from a package

Python
from mypkg import utils
print(utils.some_func())

calls a function named some_func(), the output will be whatever that function returns.

If utils.py contains something like:

Python
def some_func():
    return "Hello"

Output

Hello

Locating a Module

Python searches for modules in a predefined list of directories known as the module search path. You can view this list using sys.path.

Python
import sys
for p in sys.path:
    print(p)

Output
/home/guest/sandbox
/usr/local/lib/python313.zip
/usr/local/lib/python3.13
/usr/local/lib/python3.13/lib-dynload
/usr/local/lib/python3.13/site-packages

Explanation: Python checks each path in order until it finds the module you’re trying to import.

Comment

Explore

    Python Fundamentals
    • Introduction1 min read
    • Input & Output2 min read
    • Variables4 min read
    • Operators4 min read
    • Keywords2 min read
    • Data Types4 min read
    • Conditional Statements3 min read
    • Loops3 min read
    • Functions4 min read
    Python Data Structures
    • String4 min read
    • List3 min read
    • Tuples3 min read
    • Dictionary3 min read
    • Set2 min read
    • Arrays5 min read
    Advanced Python
    • OOP Concepts3 min read
    • Exception Handling5 min read
    • File Handling4 min read
    • Python Database2 min read
    • MongoDB3 min read
    • MySQL7 min read
    • Packages4 min read
    • Modules3 min read
    • DSA Libraries5 min read
    • Python GUI2 min read
    Data Science with Python
    • Numpy3 min read
    • Pandas4 min read
    • Matplotlib3 min read
    • Seaborn3 min read
    • StatsModel2 min read
    • Model Building6 min read
    • TensorFlow2 min read
    • PyTorch5 min read
    Web Development with Python
    • Flask3 min read
    • Django5 min read
    • Django ORM4 min read
    • Jinja2 Templating5 min read
    • Django Templates5 min read
    • REST API3 min read
    • Build API with DRF4 min read
    Python Practice
    • Quiz1 min read
    • Practice Problems2 min read
    • Interview Q & A15+ min read
    Courses
    • Placement 360 Course2 min read
    • Data Science 360 Course2 min read
    • AI Engg Course2 min read
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.