Packages Introduction

2 min read ·

Packages help organize multiple modules into a structured folder hierarchy, making large projects easier to manage.

What is a Package?

A package is a collection of modules stored inside a folder.
In simple terms:
  • Module → single Python file
  • Package → folder containing multiple modules
To make a folder a package, Python traditionally used an __init__.py file inside it.

Basic Structure of a Package


Note

In modern Python versions, init.py is optional, but it is still commonly used in real-world projects.


Using a Package

You can import modules from a package like this:

Import Specific Function


Difference Between Module and Package

  • Module → Single file containing Python code
  • Package → Folder containing multiple modules

Why Use Packages?

  • Organize large codebases
  • Avoid naming conflicts
  • Improve readability
  • Group related functionality

Pro Tip

Use packages when your project grows beyond a few modules.


Folder Structure Example


Using Modules from Package


Important Points

  • Folder name should be valid Python identifier
  • Avoid spaces in package names
  • Use meaningful names

Caution

Incorrect folder structure or missing files can cause import errors.


Exercise

Create a package named tools:
  • Add two modules: math_tools.py and string_tools.py
  • Add functions in both files
  • Import and use them in main.py