Skip to content

๐Ÿ“ฆ Python Packages Guide

Beginner-friendly documentation about Python packages, imports, modules, and project organization.

This document explains: - what packages are - how imports work - __init__.py - package organization - absolute vs relative imports - common beginner mistakes - project structure examples


๐Ÿ“š Table of Contents


๐Ÿ“– What is a Package?

A package is a folder containing Python modules.

Packages help: - organize code - separate logic - structure large projects - improve readability


๐Ÿ“ฆ What is a Module?

A module is simply:

a Python file

Example:

utils.py

This file itself is a module.


๐Ÿงฑ Basic Package Structure

project/
โ”‚
โ”œโ”€โ”€ main.py
โ”œโ”€โ”€ parser/
โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ”œโ”€โ”€ config_parser.py
โ”‚   โ””โ”€โ”€ validator.py
โ”‚
โ””โ”€โ”€ render/
    โ”œโ”€โ”€ __init__.py
    โ””โ”€โ”€ draw.py

๐Ÿ“ฅ Importing Modules

Import Entire Module

import math

Usage:

math.sqrt(25)

Import Specific Function

from math import sqrt

Usage:

sqrt(25)

๐Ÿ“Œ init.py

__init__.py tells Python that a folder should behave like a package.

Without it, imports may fail in some project structures.


๐ŸŒฟ Absolute Imports

Absolute imports start from the project root.

Example

from parser.config_parser import parse_config

๐Ÿ”— Relative Imports

Relative imports use dots (.).

Example

from .validator import validate_config

๐Ÿ› ๏ธ Organizing Large Projects

Example organization:

project/
โ”‚
โ”œโ”€โ”€ core/
โ”œโ”€โ”€ parsing/
โ”œโ”€โ”€ render/
โ”œโ”€โ”€ utils/
โ”œโ”€โ”€ tests/
โ””โ”€โ”€ assets/
โ””โ”€โ”€ main.py

And each folder would have a init.py file to make it a package.


๐Ÿ“Œ init.py Contents

In many Python projects, __init__.py is often left empty.

Example:

# __init__.py

This is completely normal and commonly used only to mark the folder as a package.


Why Leave It Empty?

An empty __init__.py: - keeps the package simple - avoids unnecessary imports - helps Python recognize the directory as a package


What Can Be Inside init.py?

Although often empty, it may also contain: - package imports - helper variables - package initialization code - exposed public functions/classes


Example

from .config_parser import parse_config
from .validator import validate_config

This allows imports like:

from parser import parse_config

instead of:

from parser.config_parser import parse_config

Why This is Useful

This can: - simplify imports - expose public APIs - improve package organization

Very common in: - libraries - frameworks - large Python projects


Good Practice

For beginner projects: - keeping __init__.py empty is completely fine

As projects grow larger: - it may be used to organize package exports and initialization logic


โš ๏ธ Common Beginner Mistakes

โŒ Missing init.py

Problem:

ModuleNotFoundError

โŒ Circular Imports

Example:

# a.py imports b.py
# b.py imports a.py

โŒ Running Files Directly

Bad:

python3 parser/config_parser.py

Better:

python3 main.py

๐Ÿ“š Final Notes

Understanding packages is extremely important for modern Python development.

Good package organization helps: - readability - maintainability - debugging - scalability - teamwork