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.
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.
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 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 *.
As said above import uses __import__() to search for module, and if not found, it would raise ImportError
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 email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
- Create and Import modules in Python
- Why import star in Python is a bad idea
- How to import JSON File in MongoDB using Python?
- grp module in Python
- pwd module in Python
- MySQL-Connector-Python module in Python
- twitter-text-python (ttp) module - Python
- Keyword Module in Python
- Pyscaffold module in Python
- Functools module in Python
- Toolz module in Python
- AppJar module in Python
- Arrow module in Python
- Platform Module in Python
- Python-interface module
- Cmdparse module in Python
- PyDictionary module in Python
- Pymatrix module in python
- struct module in Python
- Pyperclip module in Python