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 module_name
When import is used, it searches for the module initially in the local scope by calling __import__() function. The value returned by the function are then reflected in the output of the initial code.
import math print(math.pi) |
Output:
3.141592653589793
import module_name.member_name
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 whole can be imported into our intial code, rather than importing the whole module.
from math import pi # Note that in the above example, # we used math.pi. Here we have used # pi directly. print(pi) |
Output:
3.141592653589793
from module_name import *
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 *.
from math import *print(pi) print(factorial(6)) |
Output:
3.141592653589793 720
As said above import uses __import__() to search for module, and if not found, it would raise ImportError
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'
This article is contributed by Piyush Doorwar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Why import star in Python is a bad idea
- Create and Import modules in Python
- How to import JSON File in MongoDB using Python?
- How to import an excel file into Python using Pandas?
- Different ways to import csv file in Pandas
- Ways to import CSV files in Google Colab
- How to import excel file and find a specific column using Pandas?
- MySQL-Connector-Python module in Python
- twitter-text-python (ttp) module - Python
- OS Module in Python with Examples
- struct module in Python
- Secrets | Python module to Generate secure random numbers
- Python calendar module | formatmonth() method
- Python | Writing to an excel file using openpyxl module
- Count frequencies of all elements in array in Python using collections module
- Stack and Queue in Python using queue Module
- Testing in Python using doctest module
- Python | ASCII art using pyfiglet module
- Python | Adjusting rows and columns of an excel file using openpyxl module
- Python | Plotting charts in excel sheet using openpyxl module | Set - 1

