In Python, modules are an essential part of the language that allows you to reuse code, organize your code into separate files, and improve code modularity. There are two ways to import modules in Python:
1) using the import statement.
2) using the from keyword.
In this article, we’ll take a closer look at how to import modules in Python using both methods.
import statement: This is the most common way to import modules in Python. It’s used to import an entire module or package. The syntax for the import statement is as follows:
import module_name
Here, module_name is the name of the module or package that you want to import. For example, if you want to import the math module, you can use the following code:
import math
This imports the entire math module, which contains a range of mathematical functions, constants, and classes. You can then use any of these functions, constants, or classes by prefixing them with the module name, like this:
import math print(math.pi) # Output: 3.141592653589793
Here, we’re using the pi constant from the math module to print the value of Ï€.
from keyword: This is another way to import modules in Python. It’s used to import specific functions, variables, or classes from a module. The syntax for the from keyword is as follows:
from module_name import function_name
Here, module_name is the name of the module that you want to import from, and function_name is the name of the function, variable, or class that you want to import. For example, if you want to import the sqrt function from the math module, you can use the following code:
from math import sqrt
This imports only the sqrt() function from the math module, which is used to calculate the square root of a number. You can then use this function directly in your code, without prefixing it with the module name:
from math import sqrt print(sqrt(16)) # Output: 4.0
Here, we’re using the sqrt function directly, without having to prefix it with the math module name.
Also if you want to import everything (like variables/class/functions/methods) from the module then the syntax is as follows:
from module_name import *
For example,
from math import * print(sqrt(16)) # Output: 4.0 print(pi) # Output: 3.141592653589793
Importing a module with an alias
You can also import a module with an alias, using the as keyword. This can be useful if you want to use a shorter name for a module, or if you want to avoid naming conflicts. The syntax for importing a module with an alias is as follows:
import module_name as alias_name
Here, module_name is the name of the module that you want to import, and alias_name is the name that you want to use as an alias. For example, if you want to import the numpy module with an alias np, you can use the following code:
import numpy as np
This imports the numpy module with an alias np. You can then use the np alias instead of the full module name numpy:
import numpy as np x = np.array([1, 2, 3]) print(x)
Output:
[1 2 3]
In conclusion, importing modules in Python is an important aspect of the language that allows you to reuse code, organize your code into separate files, and improve code modularity. The two most common ways to import modules are using the import statement or the from keyword.