The import Statements in Python



In Python, accessing and using the code from one module to another module is possible by the importing process. That means, you can use any Python source file as a module by executing an import statement in some other Python source file. This is done using the import statement in Python.

The import Statement

When the Python interpreter encounters an import statement, it imports the module if the module is present in the search path. A search path is a list of directories that the interpreter searches before importing a module. This import operation in Python internally uses the built-in function __import__() from the importlib module.

Syntax

The import has the following syntax ?

"import" module ["as" identifier] ("," module ["as" identifier])*

A module is loaded only once, regardless of the number of times it is imported. This prevents the module execution from happening over and over again if multiple imports occur.

Example

For example, to import the module math.py, you need to put the following command at the top of the script.

# Import module math
import math

# Now you can call defined function from that module as follows
result = math.factorial(4)
print(result)

When we run above program, it produces following result:

24

Example

Let's take an example where we import two modules at once using the import statement. Here we will import the math.py and time.py modules within a single statement.

# Import math and time module
import math, time 

# Get the factorial of a number using the math module
result = math.factorial(4)
print("Factorial of 4:",result)

# Get the current time using the time module
localtime = time.asctime( time.localtime(time.time()) )
print ("Local current time :", localtime)

When we run above program, it produces the following result ?

Factorial of 4: 24
Local current time : Tue Jun 24 19:27:08 2025

The from...import Statement

Python's from...import statement helps you to import specific attributes from a module into the current namespace. This is useful when we need certain functions, classes, or variables from a module.

Syntax

The from...import has the following syntax ?

"from" relative_module "import" identifier ["as" identifier]("," identifier ["as" identifier])*

Example

Let's take an example to import only the factorial function from the math module.

# Only import the factorial function
from math import factorial

# Get the factorial of a number
result = factorial(4)
print("Factorial of 4:",result)

When we run above program, it produces following result ?

Factorial of 4: 24

In this example, the import statement does not import the entire math module into the current namespace; it just introduces the item factorial from the math module into the global symbol table of the importing module.

The from...import * Statement

It is also possible to import all names from a module into the current namespace by using the following import statement ?

"from" relative_module "import" "*"

Example

Let's take an example to import all names from the math module using the from...import * statement.

# import all function from the math
from math import *

# Get the factorial of a number
result = factorial(4)
print("Factorial of 4:",result)

print("square root of 16 is:",sqrt(16))

When we run above program, it produces the following result ?

Factorial of 4: 24
square root of 16 is: 4.0

This provides an easy way to import all the items from a module into the current namespace; however, import * is not recommended in most cases, especially in large projects.

Updated on: 2025-09-01T11:37:47+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements