Python Modules

You can extend the capabilities of your Python program by leveraging, or "importing" other files of code called "modules".

Some selected Python modules of interest include:

You can also create and import your own modules to help you organize your code into separate logical files.

For more details, follow along with this official tutorial on modules:

Usage

To load any module, whether a built-in module or a custom module you create, use the import statement. Then after importing the module, you can reference code contained within.

To see this concept in action, create a new directory on your computer called "modules-overview" and place inside the following two files...

Script:

Module:

Then execute the script to prove it has access to code in the module:

It is also possible to execute the module directly:

Modules in Subdirectories

If your python file is located in a subdirectory, you can reference it using the [directory name].[file name]. Like this:

Last updated

Was this helpful?