Package Structure

Until now, we have written all our code in a single file. However, in real-world programming, code can span hundreds or even thousands of lines. What would happen if we put all that code into one file?

It would be hard to find, hard to fix, and a total mess.

That’s why programmers organize code into multiple files and folders. In Python, these are called Modules and Packages.


Module

A module is very simple. A single Python file (.py) is a module.

For example, if you create a file named calculator.py:

# calculator.py

def add(a, b):
    return a + b

def subtract(a, b):
    return a - b

This file itself becomes a module named calculator.

You can use this module in other files:

# main.py

import calculator

result = calculator.add(3, 5)
print(result)  # 8

import means exactly what it says: “to bring in.” It means you want to borrow code from another file.


Package

When you have multiple modules, you can group them into folders. This folder is a package.

my_project/
├── main.py
└── utils/              ← This folder is a package
    ├── __init__.py     ← A file telling Python "this folder is a package"
    ├── calculator.py   ← Module
    └── greeting.py     ← Module

__init__.py is a special file. It needs to exist for Python to recognize the folder as a package rather than just a regular folder. (It’s okay if the file is empty.)

To use a module inside a package:

# main.py

from utils import calculator

result = calculator.add(10, 20)
print(result)  # 30

from utils import calculator means “from the utils package, bring in the calculator module.”


Difference between from and import

# Method 1: Import the entire module
import calculator
calculator.add(3, 5)

# Method 2: Import only a specific function
from calculator import add
add(3, 5)

# Method 3: Import a module inside a package
from utils import calculator
calculator.add(3, 5)

# Method 4: Import a specific function from a module inside a package
from utils.calculator import add
add(3, 5)

You can choose the method that is most convenient for your situation.


What a Real Project Looks Like

my_game/
├── main.py                 ← Program entry point
├── characters/             ← Character-related package
│   ├── __init__.py
│   ├── hero.py
│   └── monster.py
├── items/                  ← Item-related package
│   ├── __init__.py
│   ├── weapon.py
│   └── potion.py
└── utils/                  ← Toolset package
    ├── __init__.py
    └── helpers.py

By grouping related code into folders, you can quickly find what you need: “Character-related code is in the characters folder, and item-related code is in the items folder!”


Using Pre-made Packages

One of Python’s greatest strengths is that you can use thousands of packages created by developers worldwide for free.

We use a tool called pip in the terminal:

pip install requests

Once installed, you can use it immediately:

import requests

response = requests.get("https://example.com")
print(response.status_code)

pip is Python’s package manager. Think of it like downloading apps from an App Store.


To summarize:

  • Module = A single Python file (.py)
  • Package = A folder containing modules (requires __init__.py)
  • import = Using code from another file
  • pip = A tool to install packages made by others

Keeping your code clean and organized is one of the most important habits of a programmer!