Modules

We will learn about modules with examples.

In simple terms, a module is a file consisting of Python or PowerShell code that has functions, classes, and variable definitions inside it.

Modular programming

A module can also be a logical organization of our code. The process of breaking large and complex programming tasks into small, manageable subtasks or modules is called modular programming. These small subtasks can be coupled together to form the building blocks of a complex but neat programming system.

Modular programming has several advantages, including simplicity, reusability, and maintainability in large programs.

Importing modules

Let’s start by importing a single module.

Importing single module

PowerShell provides an Import-module cmdlet to import modules into the current session.

## syntax: Import-Module <name of module>
Import-Module PSReadLine

Type the above command in the terminal below to see it in action.

Terminal 1
Terminal
Loading...

Alternatively, Python has a keyword import to load modules into the current Python session. However, the import statement does not make content in the module directly accessible to the caller. Content is instead only accessible when it is prefixed with <module name> followed by a ( . ) dot notation: <module name>.<property/method>.

Press + to interact
import os # importing module
print(os.getcwd()) # gets current working directory

Importing multiple modules

Both PowerShell and Python allow us to import multiple modules in the session by referencing the module names separated by ( , ) comma(s). For example, in Python, the syntax would look something ...