Open In App

Import module in Python

Last Updated : 11 Aug, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In Python, modules help organize code into reusable files. They allow you to import and use functions, classes and variables from other scripts. The import statement is the most common way to bring external functionality into your Python program.

Python modules are of two types: built-in modules like random and math, which come with Python, and external modules like pandas and numpy, which need to be installed separately.

Why do we need to import modules?

  • Reuse Ready-Made Tools: Python modules let you use pre-written code like math.sqrt() or random.randint() instead of writing everything yourself.
  • Keep Code Organized: Modules break large programs into smaller, manageable pieces across multiple files.
  • Boost Productivity: You get access to Python’s standard library or third-party packages without reinventing the wheel.
  • Write Less, Do More: Importing a module gives you lots of functionality in a single line.
  • Avoid Clutter: Code stays cleaner, shorter and more readable when organized into reusable modules.

Importing built-in Module

Built-in modules can be directly imported using "import" keyword without any installtion. This allows access to all the functions and variables defined in the module.

Example:

Python
import math
pie = math.pi
print("Value of pi:", pie)

Output
Value of pi: 3.141592653589793

Explanation:

  • math module is imported using import math.
  • We access the constant pi using math.pi and then the value is printed as part of a formatted string.

Importing External Modules

To use external modules, we need to install them first, we can easily install any external module using pip command in the terminal, for example:

pip install module_name

Make sure to replace "module_name" with the name of the module we want to install, example: pandas, numpy, etc.

After installation, we can import the module like a regular built-in module using "import statement".

Example:

Python
import pandas

# Create a simple DataFrame
data = {
    "Name": ["Elon", "Trevor", "Swastik"],
    "Age": [25, 30, 35]
}

df = pandas.DataFrame(data)
print(df)

Output
      Name  Age
0     Elon   25
1   Trevor   30
2  Swastik   35

Explanation:

  • import pandas: imports the external pandas library
  • pd.DataFrame(data): creates a table-like data structure (DataFrame) from the given dictionary.

Importing Specific Functions

Instead of importing the entire module, we can import only the functions or variables we need using the from keyword. This makes the code cleaner and avoids unnecessary imports.

Example:

Python
from math import pi
print(pi)

Output
3.141592653589793

Explanation:

  • from math import pi imports only the pi constant, so we can use it directly without math. prefix.
  • This reduces unnecessary module overhead when only specific functions or constants are needed.

Importing Modules with Aliases

To make code more readable and concise, we can assign an alias to a module using as keyword. This is especially useful when working with long module names.

Example:

Python
import math as m
result = m.sqrt(25)
print("Square root of 25:", result)

Output
Square root of 25: 5.0

Explanation:

  • import math as m imports the math module and assigns it the alias m.
  • m.sqrt(25) calls the square root function using the alias.

Importing Everything from a Module (*)

Instead of importing specific functions, we can import all functions and variables from a module using the * symbol. This allows direct access to all module contents without prefixing them with the module name.

Example:

Python
from math import *
print(pi)         # Accessing the constant 'pi'
print(factorial(6))  # Using the factorial function

Output
3.141592653589793
720

Explanation:

  • from math import * imports all functions and constants from the math module.
  • pi and factorial(6) are accessed directly without using math. as a prefix.
  • While convenient, this method is not recommended in larger programs as it can lead to conflicts with existing variables and functions.

Handling Import Errors in Python

When importing a module that doesn’t exist or isn't installed, Python raises an ImportError. To prevent this, we can handle such cases using try-except blocks.

Example:

Python
try:
    import mathematics  # Incorrect module name
    print(mathematics.pi)
except ImportError:
    print("Module not found! Please check the module name or install it if necessary.")

Output
3.141592653589793
720

Explanation:

  • try block attempts to import a module, if the module is missing or misspelled, Python raises an ImportError.
  • The except block catches the error and displays a user-friendly message instead of crashing the program.

Article Tags :
Practice Tags :

Similar Reads