What is the use of import statement in Python?


The import statement in Python is used to bring code from external modules or libraries into your program. This is a powerful feature that allows you to reuse code and avoid duplicating code across multiple programs. Here are some examples of how to use the import statement:

Import a single module

Let's say you want to use the math module in your program, which provides a variety of mathematical functions. To import the math module, you simply use the import statement followed by the name of the module:

Example

In this example, we import the math module and use the sqrt() function to compute the square root of 36. Note that we prefix the function name with the name of the module (math.) to indicate that it comes from the imported module.

import math
# Now you can use functions from the math module
print(math.sqrt(36))  

Output

 6.0

Import a module with an alias

Sometimes you may want to use a shorter or more convenient name for a module in your code. To do this, you can use an alias when importing the module.

Example

In this example, we import the math module and give it an alias of m. We can then use m instead of math to access functions in the module.

import math as m

# Now you can use the alias instead of the full module name
print(m.sqrt(36)) 

Output

 6.0

Import specific functions from a module

Sometimes you may only need to use one or a few functions from a module, rather than importing the entire module. To do this, you can use the from ... import syntax to import specific functions from the module.

Example

In this example, we import only the sqrt() function from the math module, rather than importing the entire module. This can make your code more efficient and easier to read, especially if you only need a few functions from a large module.

from math import sqrt

# Now you can use the sqrt() function directly
print(sqrt(36))  

Output

 6.0

Import all names from a module

Sometimes you may want to import all names (variables, functions, classes) from a module into your current namespace. To do this, you can use the from ... import * syntax.

Example

In this example, we import all names from the math module using the * wildcard character. This is generally not recommended, as it can lead to name conflicts and make it harder to read your code. However, it can be useful in some cases where you need to use many names from a module.

from math import *
# Now you can use all names from the math module
print(sqrt(36))  

Output

 6.0

Import a module from a package

In Python, a package is a directory that contains one or more modules, along with an __init__.py file. To import a module from a package, you can use dot notation to specify the package and module names. Here's an example:

Example

In this example, we import the my_module module from the my_package package. Note that my_package must be a directory that contains a file named my_module.py, and it must also contain an __init__.py file to indicate that it's a package.

import my_package.my_module
# Now you can use functions from the mymodule module
print(my_package.my_module.my_function())

Import a module from a different directory

Sometimes you may want to import a module from a directory that's not in the same directory as your main program file. To do this, you can add the directory path to the Python path using the sys.path variable. Here's an example:

Example

In this example, we add the path /path/to/my/module to the Python path using the sys.path variable. We can then import the mymodule module using the standard import statement, and use functions from the module as usual. Note that you should replace /path/to/my/module with the actual path to your module directory.

import sys
sys.path.append('/path/to/my/module')
import my_module
# Now you can use functions from the my_module module
print(my_module.my_function())

Updated on: 11-Aug-2023

729 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements