Menu

Modules and Packages

6. Modules and Packages

a. Importing Modules

When a program grows, you do not want everything in one huge file. Modules help you break your code into smaller files and reuse code written by you or others. Importing a module means telling your program, “I want to use the code defined in that other file.”

Most languages provide a simple keyword (like import or use) to bring a module into the current file. After importing, you can call functions, use classes, or access constants defined there. This keeps your main code shorter and easier to read.

Using modules also encourages code organization. Instead of copying and pasting the same logic everywhere, you group related functions in one module and import them wherever needed.

b. Creating Custom Modules

Creating a custom module is just separating related code into its own file. For example, you might create a math_utils module for all your math-related functions or a user_helpers module for user-related logic.

The key idea is: any file that defines functions, classes, or variables can act as a module. You then import this file in another place to reuse its code. This avoids duplication and makes your project more maintainable.

As your application grows, you may create many modules: one for database logic, one for API calls, one for business rules, and so on. Thinking in modules from the start helps you design cleaner architectures.

c. Working with Packages

A package is a collection of related modules organized in a folder structure. Where a module is one file, a package is a directory that groups several modules together under a common name.

Packages help you organize code at a higher level. For example, you might have a payments package containing modules like billing, invoices, and transactions. From the outside, you treat this whole folder structure as one logical unit.

Languages usually provide a way to reference modules inside packages using dotted paths, like package.module. This keeps naming clear and avoids collisions between unrelated modules in different packages.

d. Package Management

Package management is about installing, updating, and removing external libraries. Instead of downloading code manually, you use a package manager (like pip in Python or npm in JavaScript) to fetch and track dependencies.

A package manager lets you:

  • Install a library with one command
  • Lock versions so your project is reproducible
  • Update libraries safely when needed

This matters because modern development often uses many third‑party packages for tasks like HTTP, logging, testing, or database access. A package manager keeps all these dependencies under control.

Modules vs Packages

Aspect

Module

Package

Basic unitSingle fileFolder/directory containing modules
PurposeGroup related code in one fileGroup related modules under one namespace
ScaleSmallerLarger, often multi-module
Import styleFile-level importPackage-level, then module-level imports
Use caseReusing a small set of functionsOrganizing a full feature area or library