Skip to main content

Python Modules, Packages & Standard Libraries

As codebases grow, keeping all code in a single file becomes unmanageable. Python provides Modules and Packages to organize code into reusable components. Additionally, Python comes with a rich set of built-in Standard Libraries that handle common engineering tasks.

Topics Covered

In this module, you’ll learn:
  1. Python Modules: Creating and importing files.
  2. Python Packages: Organizing modules into folders using __init__.py.
  3. Prominent Standard Libraries
  4. Environment Variables: Working with python-dotenv.
Try Yourself: πŸ’» VS Code | πŸš€ Colab | πŸ“₯ Download
Verify Solutions: πŸ’» VS Code | πŸš€ Colab | πŸ“₯ Download

1. Python Modules

A module is simply a file containing Python code (functions, classes, or variables) with a .py extension. You can use code from one module in another using the import statement.

Creating and Importing a Module

Suppose we have a file named utils.py:
You can import and use utils.py in your main script (main.py) in three different ways:

A. Standard Import

B. Importing Specific Elements

C. Alias Import (Renaming)

Exercise 1

Create a module named calculator.py with two functions: add(a, b) and multiply(a, b). In a separate script, import these functions and call them.

2. Python Packages

A package is a directory that contains multiple modules. It allows you to group related modules together.

Package Structure

To turn a directory into a package, it traditionally contains a file named __init__.py (which can be empty).
Inside main.py, you can import from the db package:

3. Prominent Standard Libraries

Python’s standard library contains pre-built modules for math, randomness, paths, and OS interactions.

The math Module

Provides mathematical functions and constants.

The random Module

Used to generate pseudo-random numbers, make random selections, or shuffle sequences.

The os Module

Provides functions to interact with the operating system (creating directories, reading env variables).
Modern Python prefers pathlib over os.path because it treats file paths as objects rather than strings, making path manipulation cleaner and safer across different operating systems (Windows uses \, macOS/Linux uses /).

Exercise 2

Write a script that uses random.choice to pick a random host from a list of servers ["server-a", "server-b", "server-c"]. Then, use pathlib to check if a folder named backup exists in your project root, and create it if it does not.

4. Configuration with python-dotenv

In production, you should never hardcode database credentials, secret keys, or configurations in your code. Instead, you store them in environment variables. The python-dotenv package allows you to load variables from a .env file into os.environ during development.

Step 1: Create a .env file

Create a file named .env in the root of your project:

Step 2: Load variables in your script

Exercise 3

Write a program that loads a configuration .env file. Retrieve the variable API_KEY. If API_KEY is not set, print a warning message; otherwise, print "API Key Loaded".

Practice

To reinforce what you’ve learned in this section, practice with the interactive follow-along notebook:

Follow-Along Practice

Practice structuring code with modules and packages, working with math, random, os, pathlib, and environment variables.πŸ’» VS Code | πŸš€ Colab | πŸ“₯ Download

Summary

In this module, you learned how to organize your code using Python modules and packages, interact with the system using standard libraries, and manage configuration using environment variables.