...

/

Importing a Module

Importing a Module

Learn how to import different types of modules and their usage in Python.

To use the definitions and statements of one module in another module, we need to import the first module into the second one, as shown below:

Press + to interact
main.py
functions.py
import functions
functions.display( )
functions.show( )

When we execute main.py, it runs as a module with the name __main__.

The import functions statement makes the definitions in functions.py available in main.py.

A module can import multiple modules, as shown below:

Press + to interact
import math
import random
a = 100
b = 200
print(__name__)
print(math.sin(0.5))
print(math.cos(0.5))
print(random.random( ))
print(random.randint(30, 45))

In the code above, __name__ contains __main__. This indicates that we are executing the main ...