Python Import Command

The import command in Python is used to get access to other modules. Modules are the same as a code library in Java, C, C++, or C#. A module typically involves a set of functions and variables. When we need to include or use these functions of modules in our code, we can simply import the module by using the import command and we can easily invoke the module functions and variables. The import command is the simplest and common way of including modules into your code.

Python comes up with many built-in modules that we can include in our code easily. We can also create our module by just saving the Python code file with the .py extension.

In this article, we will learn that how we can import our own and built-in modules in Python. Spyder3 editor is used to creating and running the Python scripts.

How to use the import command

We use the import keyword to import the modules in Python. While importing the module in our code, we write the module name with import command in this way:

import module_name

Import Python built-in modules

Python comes up with many built-in modules. Math module is one of the common modules that is used to perform the mathematical functions.

Let’s import the math module by using the import keyword and use its functions to perform mathematical calculations. When we access any function from a module, we write the name of the module and put a dot and write the name of the function like that:

module_name.function_name()
# importing the math module
import math
# printing the value of pi constant
print("The value of PI is: ",math.pi)

# calculating the factorial of a number using factorial function
print("The factorial of number 5 is: ",math.factorial(5))

# calculating the log of a number using the log function
print("The log of 10 is: ",math.log(10))

# printing the value of Euler's number
print("The value of Euler's number is: ", math.e)

# calculating the radians from degrees
rad = math.radians(90)
print("The radians of 90 is: ",rad)

# calculating the sin value
print("The sin of 90 is: ",math.sin(90))

# calculating the coa value
print("The cos of 90 is: ",math.cos(90))

# calculating the tan value
print("The tan of 90 is: ",math.tan(90))

Output

The output is displayed on the Python console.

In some cases, if we want to import only a specific function or a constant from a module, we can do in this way:

from module_name import function_name or constant_name

For example, only the pi constant from the math module can be imported in this way

from math import pi

Let’s see an example of it.

# importing only pi value from the math module
from math import pi
# printing the value of pi constant
#here we use pi directly instead of math.pi()
print("The value of PI is: ", pi)

Output

The output is displayed on the Python console.

All the functions and constants can be imported in this way:

from module_name import *

In the case of the math module it would be like this:

# importing only pi value from the math module
from math import *
# Now we don't need to specify math with the constant and function
# printing the value of pi constant
print("The value of PI is: ",pi)

# calculating the value of sin 90
print("The value of sin 90 is:", sin(90))

# calculating the factorial of 8
print("The factorial of 8 is: ",factorial(8) )

Output

The output is displayed on the Python console.

The import command searches for the module name if the module is not found, then it shows an error. Let’s try to import the module “Tokenizer”.

import tokenizer
print(tokenizer.token())

Output

In the output, you can see that it throws an error “ModuleNotFoundError”.

Create your module

To create your module, create a python file, write the code, and save it with .py extension.

Let’s see an example of it.

Example

We have created a new module named “calculate.py”. It has a function, which takes two numbers as an argument and returns it sum.

def sum(val_1,val_2):
  print("Sum is: ",val_1+val_2)

Now let’s create another Python file (test.py) and call the “calculator” module in that file.

# importing the calculator module
import calculator
# calling the sum function
print(calculator.sum(1,2))

Output

The output is displayed on the Python console.

Now let’s modify the calculator module file and create two variables here.

val_1=0
val_2=0
def sum():
  print("Sum is: ",val_1+val_2)

Let’s try to access the variables of calculator module in test.py

# importing the calculator module
import calculator
# accessing the first variable and assigning a value
calculator.val_1=10
# accessing the second variable and assigning a value
calculator.val_2=20
# calling the sum function from calculator module
print(calculator.sum())

Output

The output is displayed on the Python console.

We can also create an alias while importing a module by using the “as” keyword and it will work fine.

# importing the calculator module as cal
import calculator as cal
# accessing the first variable and assigning a value
cal.val_1=10
# accessing the second variable and assigning a value
cal.val_2=20
# calling the sum function from calculator module
print(cal.sum())

Output

In the output, you can see that it works fine and does not show any type of error.

We can use the Python built-in dir() function to list down all the available functions and variables of a module.

# importing the calculator module as cal
import calculator as cal
# using dir() function
print(dir(cal))

Output

The output shows all the available variables and functions of the “calculator” module.

Conclusion

This article explains the Python import command in detail with the help of simple examples. The import command is used to call the built-in and user-defined modules in Python files.



from Linux Hint https://ift.tt/3cCVvWV

Post a Comment

0 Comments