
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
The import Statements in Python
You can use any Python source file as a module by executing an import statement in some other Python source file.
Syntax
The import has the following syntax −
import module1[, module2[,... moduleN]
When the interpreter encounters an import statement, it imports the module if the module is present in the search path. A search path is a list of directories that the interpreter searches before importing a module. For example, to import the module support.py, you need to put the following command at the top of the script −
#!/usr/bin/python # Import module support import support # Now you can call defined function that module as follows support.print_func("Zara")
When the above code is executed, it produces the following result −
Hello : Zara
A module is loaded only once, regardless of the number of times it is imported. This prevents the module execution from happening over and over again if multiple imports occur.
The from...import Statement
Python's from statement lets you import specific attributes from a module into the current namespace. The from...import has the following syntax −
from modname import name1[, name2[, ... nameN]]
For example, to import the function fibonacci from the module fib, use the following statement −
from fib import fibonacci
This statement does not import the entire module fib into the current namespace; it just introduces the item fibonacci from the module fib into the global symbol table of the importing module.
The from...import * Statement
It is also possible to import all names from a module into the current namespace by using the following import statement −
from modname import *
This provides an easy way to import all the items from a module into the current namespace; however, this statement should be used sparingly.
- Related Articles
- What are the differences between import and static import statements in Java?
- Import module in Python
- The implementation of import in Python (importlib)
- Import a module in Python
- How to import Matplotlib in Python?
- What is the use of import statement in Python?
- What is the use of "from...import" Statement in Python?
- What is the use of "from...import *" Statement in Python?
- How to import other Python files?
- How we can import Python modules in Jython?
- Multiple Statements in Python
- How I can dynamically import Python module?
- Python import modules from Zip archives (zipimport)
- What are the “best practices” for using import in a Python module?
- How to import a Python module given the full path?
