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:- Python Modules: Creating and importing files.
- Python Packages: Organizing modules into folders using
__init__.py. - Prominent Standard Libraries
- 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 namedutils.py:
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 namedcalculator.py with two functions: add(a, b) and multiply(a, b). In a separate script, import these functions and call them.
Solution
Solution
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).
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).
The pathlib Module (Recommended for Paths)
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 usesrandom.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.
Solution
Solution
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".
Solution
Solution
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