Import module in Python

PythonServer Side ProgrammingProgramming

In this article, we will learn about all the methods by which we can import modules in Python 3.x. Or earlier. The import statement is used to include all the dependencies/modules needed to execute the code.

The most common way :

Syntax

import < module name >

Example

import math
print(math.log10(100))

Output

2.0

The efficient way :

Syntax

from <module name> import <function name>

Example

from math import pi
print(math.pi)

Output

3.14159265358979

Importing all functions from the module

Syntax

from math import *

Example

from math import *
print(math.log10(100))

Output

2.0

Specifying an alias name to the import module

Syntax

import <module name> as <alias name>

Example

import math as m
print(m.log10(100))


Output

2.0

Conclusion

In this article, we learned about importing external modules and libraries in Python 3.x. Or earlier.

raja
Published on 07-Aug-2019 10:41:04
Advertisements