How import works in Python
What is import? In Python, import is used to bring in modules or specific functions/classes/variables from python modules so that we can use them in our current script, as well as any additional modules that the imported module may depend on.
A module is basically a Python file (.py) containing variables, functions, or classes.
Basic Import To import a module in Python, we use the import statement followed by the name of the module. For example, to import the math module, which contains a variety of mathematical functions, we would use the following statement:
Python
# Import the whole module import math
Once a module is imported, we can use any of the functions and variables defined in the module by using the dot notation. For example, to use the sqrt function from the math module, we would write:
Python
import math result = math.sqrt(9) # Using sqrt function from math module print(result) # Output: 3.0
- Here, the entire module is imported, so you need to use module_name.function_name.
Import Specific Functions/Classes/variables We can also import specific functions or variables from a module using the from keyword. For example, to import only the sqrt function from the math module, you would write:
Python
# Import only sqrt function from math module from math import sqrt result = sqrt(9) # No need to write math.sqrt print(result) # Output: 3.0
Points to note:
- Only the sqrt function is imported.
- It is directly usable without the module prefix.
We can also import multiple functions or variables at once by separating them with a comma:
Python
from math import sqrt, pi result = sqrt(9) print(result) # Output: 3.0 print(pi) # Output: 3.141592653589793
Importing everything (*) It's also possible to import all functions and variables from a module using the * wildcard. However, this is generally not recommended as it can lead to confusion and make it harder to understand where specific functions and variables are coming from.
Python
from math import * result = sqrt(9) print(result) # Output: 3.0 print(pi) # Output: 3.141592653589793
The "as" keyword Python also allows you to rename imported modules using the as keyword. This can be useful if you want to use a shorter or more descriptive name for a module, or if we want to avoid naming conflicts with other modules or variables in our code.
Python
# Import math module with alias import math as m result = m.sqrt(9) print(result) # Output: 3.0 print(m.pi) # Output: 3.141592653589793
Importing Your Own Modules Suppose we have a file my_module.py:
Python
# my_module.py
def greet(name):
return f"Hello, {name}!" We can import it in another Python file:
Python
import my_module
print(my_module.greet("Ashish")) Or:
Python
from my_module import greet
print(greet("Ashish")) The dir function Python has a built-in function called dir that we can use to view the names of all the functions and variables defined in a module. This can be helpful for exploring and understanding the contents of a new module.
Python
import math print(dir(math))
This will output a list of all the names defined in the math module, including functions like sqrt and pi, as well as other variables and constants.
Python
# Correct way to import scikit-learn modules import sklearn as sk # List all available attributes in sklearn print(dir(sk))