Modules can have one or more functions. They help you to organize your code. Instead of one long Python file, you can have several files (modules).

A module is a Python file that has functions or classes. A Python program can use one or more modules.

Related course: Complete Python Bootcamp: Go from zero to hero in Python

Example

We load the os module with the import keyword. Using that module we call one of its functions.

#!/usr/bin/python
import os
os.system("dir")

In this case it will simply list the files in the directory (dir command).
To see all functions in a module, start the Python interpreter and type

python
import os
dir(os)

This will show all functions and classes in the module:

module functions

If you want, you can import a specific function instead of the whole module.

#!/usr/bin/python
from time import sleep
sleep(2)

Exercise

Try the exercises below

  1. Import the math module and call the sin function

Download answers