Python import Statement: How to Import Modules with Syntax and Examples

Overview

Python provides many useful features through reusable pieces of code called modules. Some modules are included with Python, while others can be created to organize reusable functions, variables, classes, and other code.

To actually use a module inside a program, Python provides the import statement. It allows one Python file to access code stored in another module without copying or rewriting it, making programs easier to organize and maintain.

In this tutorial, you will learn what the Python import statement is, how its syntax works, and how to use it with both built-in and user-defined modules. By the end of this guide, you will understand how Python shares reusable code across different programs.

Quick Navigation

Navigate through the sections below to learn the Python import statement, its syntax, module importing techniques, examples, and best practices.

What is the import Statement?

The import statement is used to load a Python module into the current program. Once a module is imported, its functions, variables, classes, and other members become available for use based on the import method used.

For example, Python provides a built-in module called math that contains common mathematical functions. Instead of writing these functions manually, you can simply import the module and use them when needed.

Real-World Analogy

Imagine writing a report using information from a textbook. You do not rewrite the book—you simply refer to it when needed. Modules work the same way: they store reusable code that your program can access through the import statement.

Why Use the import Statement?

The import statement helps organize code and enables reuse by allowing programs to access functionality from separate modules instead of duplicating logic.

  • Reduces duplicate code
  • Keeps programs organized and maintainable
  • Provides access to Python’s standard library
  • Supports reuse of custom modules across projects
  • Helps manage large applications efficiently

Almost every Python program uses the import statement in some form.

⬆ Move to Top

Syntax of the import Statement

The basic syntax of the import statement is simple. You write the keyword import followed by the name of the module you want to use.

import module_name

Syntax Components

Part Description
import The Python keyword used to load a module.
module_name The name of the built-in or user-defined module to import.

How the Syntax Works

When Python executes an import statement, it loads the module into memory and makes its members available for use in the program.

For example, after importing the math module, its sqrt() function can be accessed using dot notation:

import math

print(math.sqrt(25))


# Output
5.0

Code Explanation

Here, math.sqrt(25) uses dot notation to call the sqrt() function from the math module. It calculates the square root of 25 and returns 5.0.

Note: Importing a module does not run all its code automatically. It only loads the module so its functions, variables, and classes can be used when needed.

⬆ Move to Top

Importing Built-in Modules

Python includes many ready-to-use modules called built-in modules. They provide functions for common tasks such as mathematical calculations, random number generation, file handling, and working with dates and times.

To use a built-in module, simply import it by specifying its name. Once imported, you can access its functions, variables, and other members using the dot (.) operator.

Example: Importing the math Module

import math

print(math.sqrt(64))
print(math.factorial(5))


#Output
8.0
120

Code Explanation

This program imports Python’s built-in math module and uses two of its functions.

In this example, math.sqrt(64) returns the square root of 64, while math.factorial(5) calculates the factorial of 5. The results 8.0 and 120 are printed on separate lines.

⬆ Move to Top

Importing User-defined Modules

Python also allows you to create your own modules. A user-defined module is a Python file that stores reusable code such as functions, variables, or classes, helping you organize and reuse code efficiently.

Once created, a user-defined module can be imported just like a built-in module. After importing, its members can be accessed in the current program as long as the file is in a location where Python can find it.

Example: Importing User-defined Modules

calculator.py

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

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

main.py

import calculator

print(calculator.add(20, 10))
print(calculator.subtract(20, 10))

# Output
30
10

Code Explanation

The calculator.py file defines two reusable functions: add() and subtract(). After importing the module, these functions are accessed using dot notation.

In main.py, calculator.add(20, 10) returns 30 and calculator.subtract(20, 10) returns 10, which are then printed as output.

Both files should be in the same directory so Python can locate the module. Later, you will learn how Python searches for modules in different locations.

⬆ Move to Top

Accessing Module Members

After importing the math module, its functions can be accessed using dot notation, where the module name is followed by a dot (.) and the required function name.

The general syntax for accessing a module member is shown below.

module_name.member_name

Example: Accessing Module Members

import math

print(math.ceil(8.2))
print(math.floor(8.9))


# Output
9
8

Code Explanation

After importing the math module, its functions can be accessed using dot notation, where the module name is followed by a dot (.) and the function name.

In this example, math.ceil(8.2) rounds 8.2 up to 9, while math.floor(8.9) rounds 8.9 down to 8. These functions demonstrate how module members are used after importing a module.

What Can Be Accessed from a Module?

Depending on how a module is designed, it may contain different types of members. The most common ones include:

  • Functions – Perform specific tasks, such as mathematical calculations or file operations.
  • Variables – Store values that can be reused throughout the program.
  • Classes – Define blueprints for creating objects.
  • Constants – Represent fixed values provided by the module.

In the next section, you will learn how to import multiple modules within the same program and organize imports more efficiently.

⬆ Move to Top

Importing Multiple Modules

Many Python programs use more than one module. For example, a program may need the math module for mathematical calculations and the random module to generate random values. Python allows you to import multiple modules within the same program, making it easy to combine different features as needed.

The most common approach is to write each import statement on a separate line. This improves readability and makes the code easier to maintain, especially as your program grows.

Example: Importing Multiple Modules

import math
import random

print(math.sqrt(81))
print(random.randint(1, 10))


# Sample Output (may vary)
9.0
7

Code Explanation

This program imports two built-in modules: math and random, each providing different functionality.

math.sqrt(81) calculates the square root of 81, while random.randint(1, 10) generates a random number between 1 and 10. The output shows the result of both operations.

⬆ Move to Top

Importing Modules from Another Python File

One of the biggest advantages of modules is the ability to reuse code stored in another Python file. Instead of placing every function in a single program, you can organize related code into separate files, import it whenever needed, and keep your programs easier to organize, maintain, and reuse as they grow.

Example Project Structure

project/
│
├── calculator.py
└── main.py

Consider the following project, where the calculator.py file contains reusable functions and the main.py file imports and uses them.

calculator.py

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

def multiply(a, b):
    return a * b

The main.py file imports the module and uses its functions.

main.py

import calculator

print(calculator.add(15, 5))
print(calculator.multiply(15, 5))


# Output:
20
75

Code Explanation

The calculator.py file acts as a user-defined module that contains reusable functions add() and multiply().

In main.py, the module is imported using the import statement and its functions are accessed using dot notation. The program then performs addition and multiplication using the imported functions, displaying the results 20 and 75.

⬆ Move to Top

Common import Statement Errors

Beginners often encounter a few common mistakes while working with the import statement. Understanding these errors will make debugging much easier.

Below are the most common ones:

  1. ModuleNotFoundError
  2. NameError
  3. Common Typing Mistakes

Let’s go through each of these errors one by one to understand why they occur and how to fix them.

1. ModuleNotFoundError

This error occurs when Python cannot locate the module that you are trying to import.

import calculator

Possible reasons include:

  • The module file does not exist.
  • The file name is incorrect.
  • The module is located in a different folder.
  • The required package has not been installed.

⬆ Move to Section Top

2. NameError

A NameError usually occurs when a module or one of its members is referenced incorrectly.

import math

print(sqrt(25))


# Output
NameError: name 'sqrt' is not defined

The correct way is:

import math

print(math.sqrt(25))

Since only the module was imported, the function must be accessed through the module name.

⬆ Move to Section Top

3. Common Typing Mistakes

Python is case-sensitive, so even small typing mistakes can result in errors.

Incorrect

Import math

Correct

import math

Similarly, importing Math instead of math or misspelling a module name will also cause errors.

⬆ Move to Section Top

⬆ Move to Top

Best Practices for Using import

The following best practices help you write cleaner, easier to understand, and more maintainable code.

  • Import only the modules that your program actually needs.
  • Place import statements at the beginning of the file unless there is a specific reason not to.
  • Use meaningful module names for your own modules.
  • Avoid creating user-defined modules with the same names as Python’s built-in modules.
  • Remove unused imports to keep the code clean.

⬆ Move to Top

Key Takeaways: import statement

Here are the key points to remember about the Python import statement:

  • The import statement allows a Python program to use code from another module.
  • You can import both built-in and user-defined modules.
  • Module members are accessed using the dot (.) operator.
  • Multiple modules can be imported within the same program.
  • Understanding common import errors makes debugging much easier.
  • Following good import practices improves code readability and maintainability.

⬆ Move to Top

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top