A Python package is a way to organize related modules together. Think of a package as a folder for Python code that belongs to the same part of a project.
This lesson follows OSPython.008 on modules/imports, OSPython.009 on virtual environments, and OSPython.010: pip and Package Installation. Now we will build a tiny package of our own.
Package vs. Module
A module can be a single Python file such as player.py. A package groups modules under one package name.
A Tiny Package
game/
__init__.py
player.py
scores.py
main.py
Here, game is the package. player.py and scores.py are modules inside it.
What Does __init__.py Do?
In a regular Python package, __init__.py identifies the directory as a package and can contain initialization code. For a beginner project, it can simply be an empty file.
Video: Understanding __init__.py
This focused tutorial demonstrates how __init__.py is used when organizing and importing Python code from a package.
Import From the Package
Suppose player.py contains a simple function:
def show_player(name):
print("Player:", name)
Then main.py can import that module from the package:
from game import player
player.show_player("Satoshi")
The package name comes first, followed by the module we want to use.
Gaming Example
A small game might keep player code in player.py, scoring code in scores.py, and map code in maps.py. Putting those files inside a game package keeps related code together instead of putting every file in one folder.
Bitcoin Mining Example
A simple mining-monitor project could use a miner package containing temperature.py, hashrate.py, and status.py. Each module handles one small job while the package groups them under one clear name.
Packages You Create vs. Packages You Install
In OSPython.010, you used pip to install packages written by other people. In this lesson, you are learning the basic structure used to organize your own Python code. Both ideas use packages, but from different sides: installing reusable code and creating organized reusable code.
Practice
- Create a folder named
game. - Add an empty
__init__.py. - Create
player.pywith a simple function. - Create
main.pyoutside the package. - Use
from game import playerand call your function.
Key Takeaway
A module is a Python file; a package organizes related modules under one name. A regular package commonly contains __init__.py, and package imports let larger programs stay organized as they grow.

Leave a comment