How Imports Module In Python. Topic – 30

How Imports Module In Python

How Imports Module In Python of bringing external modules or packages into your current Python script. The import statement is the gateway to reusability and modularity in Python. It gives you to access all functionalities from external files (modules) into the current code and reusebility is extend of your programme. Importing is a fundamental concept in Python programming, and it’s crucial to understand how it works.

Here’s a step-by-step guide for beginners to advanced users on How Imports Module In Python:

1. Importing Built-in Modules:

Python comes with verious standered library that provide various functionalities. We can import these module using directly by using import keyword and can use in our programme.

Core Concepts:

Imagine a tool box that containing verious tools. And tools are the function in the module, toolbox is the module.

The import statement lets you bring specific tools (functions) or the entire toolbox (module) into your program.


import math # Imports the entire math module
# Math is the tool box

# Use the tools (functions) from the toolbox:
print(math.pi)  # Access the pi constant
print(math.sqrt(16))  # Use the square root function

2. Importing Specific Functions or Classes:

You can import specific functions or classes from a module instead of importing the entire module. This can make your code more concise and efficient.

Core Concepts:

Let’s assume a toolbox contain verious items like pendrive, memorycard, SSD, Respbary pi etc. You need a pendrive. So you will go to shop and only buy the pendrive from the toolbox. Let’s implement into the programme.


from math import sqrt
# We import only the sqrt from the math module

print(sqrt(25))  # Output: 5.0

3. Aliasing Modules or Functions:

You can give an alias to modules or functions during import, which can be useful to avoid name conflicts or to provide a shorter name.

During the import process, you can assign an alias (a different name) to a module or function using the as keyword

Core Concepts:

Let’s assume you are buying a new tool and you don’t know the name of this. So how you will call this tool with new name. Aliasing Module is like this.


  • Shorter Names: Aliases are particularly useful for modules or functions with long names. Assigning a shorter alias can make your code more concise and easier to read.

Example: import math as m (using m instead of typing math repeatedly)

  • Avoiding Conflicts: If you have two functions or variables from different modules with the same name, aliasing helps you differentiate between them.

Example: from module1 import function1 as f1; from module2 import function1 as f2


# Import the math module and assign it an alias 'm'
import math as m

# Use the aliased name 'm' to access the sqrt function
print(m.sqrt(25))  # Output: 5.0

4. Importing All Functions and Classes:

It’s also possible to import all functions and variables from a module using the * wildcard. However, this is generally not recommended as it can lead to confusion and make it harder to understand where specific functions and variables are coming from.

Python also allows you to rename imported modules using the as keyword. This can be useful if you want to use a shorter or more descriptive name for a module, or if you want to avoid naming conflicts with other modules or variables in your code.


from math import *
print(sqrt(25))  # Output: 5.0

5. Importing Custom Modules:

A custom module is a Python file (.py extension) containing functions, variables, and classes that you can define and reuse in other programs.

  • The simplest scenario involves modules within the same directory as your main script.
  • Use the import statement followed by the module name (without the .py extension).
  • Example (assuming and are in the same directory):
import my_math
result = my_math.add(5, 3)
  • Example (assuming in a folder named helpers):
from .helpers import utils  # Relative import using dot (.)
utils.greet("Alice")  # Call the function from the imported module

I hope you understand “How Imports Module Works in Python” Need guidance or have questions? Drop a comment below. Share your email for a personalized touch—I’ll send exclusive resources and answer queries. Let’s code together, creating efficient code and memorable learning moments! 🚀

Finite number of  lines code and infinite possibilities

Programmer, Arpan Bera

Leave a Reply

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

Arpan Bera

Coding is like Love – everyone can start, but only a few finish without errors

Keep Updated to our News and Blog