Open In App

Import module in Python

Improve
Improve
Like Article
Like
Save
Share
Report

Import in Python is similar to #include header_file in C/C++. Python modules can get access to code from another module by importing the file/function using import. The import statement is the most common way of invoking the import machinery, but it is not the only way.

Import Modules in Python

When we import a module with the help of the Python import module it searches for the module initially in the local scope by calling __import__() function. The value returned by the function is then reflected in the output of the initial code. 

Python3




import math
pie = math.pi
print("The value of pi is : ", pie)


Output

The value of pi is :  3.141592653589793

Importing Module using “from”

In the above code module, math is imported, and its variables can be accessed by considering it to be a class and pi as its object. The value of pi is returned by __import__(). pi as a whole can be imported into our initial code, rather than importing the whole module. We can also import a file in Python using from.

Python3




from math import pi
print(pi)


Output

3.141592653589793

Import Python Built-In Modules

In this example, the built-in ‘random’ module is imported in Python using the import statement. The randint function from the ‘random’ module is then utilized to generate a random integer between 1 and 10, and the result is printed. We can also import a file in Python by using import statement.

Python3




# Import the 'random' module, which is a built-in module for random number generation
import random
 
# Generate a random number between 1 and 10
random_number = random.randint(1, 10)
print("Random Number:", random_number)


Output

Random Number: 5

Import a Module and Assign an Alias in Python

In this example, the ‘math’ module is imported with the alias ‘m’ using the import statement. The sqrt function from the ‘math’ module, accessed through the alias ‘m’, is then used to calculate the square root of 25. The result is printed as “Square root of 25.

Python3




# Import the 'math' module with the alias 'm'
import math as m
 
# Use functions from the 'math' module with the alias
result = m.sqrt(25)
print("Square root of 25:", result)


Output

Square root of 25: 5.0

Importing `*` in Python

In the above code module, math is not imported, rather just pi has been imported as a variable. 
All the functions and constants can be imported using *. 

Python3




from math import *
print(pi)
print(factorial(6))


Output

3.141592653589793
720

As said above import uses __import__() to search for the module, and if not found, it would raise ImportError 

Python3




import mathematics
print(mathematics.pi)


Output:

Traceback (most recent call last):
  File "C:/Users/GFG/Tuples/xxx.py", line 1, in 
    import mathematics
ImportError: No module named 'mathematics'



Last Updated : 11 Dec, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads