
Table of Contents(目次)
Welcome to the World of Modules and Packages
In this section, you will learn about Python modules and packages.
Your goal in this lesson is to build a simple random-message application called “Fortune Cookie.” When executed, the program gives you your fortune for the day in a “fortune-cookie style” message—essentially a small fortune-telling app.
A key feature of this app is its multilingual support. We will begin with two languages: English and Japanese. For example, if the user selects Japanese, the program will display a message like “大吉!最高の一日になりそうです!”, while choosing English will produce an output such as “Great Fortune! A wonderful day awaits you.”
When preparing different messages for each language, putting everything into a single file quickly becomes messy. Therefore, this course will show you how to organize your code using modules and packages.
What Are Modules and Packages?
In Python, you can break a program into multiple files, each containing a specific feature, and reuse those files. To understand this mechanism, keep the following two terms in mind:
- Module: A single Python file (e.g.,
fortune.py,language_en.py). It contains functions, classes, and global variables. For instance, you might create a module dedicated solely to storing English messages. - Package: A folder (directory) that bundles multiple modules together. A folder becomes a “package” when it contains a special file named
__init__.py.
In short, a module is a “component,” and a package is a “container for components.” Understanding these two concepts will help you build well-organized structures even in complex programs.
Start by Building Everything in One File
To begin, let’s create a simple Fortune Cookie app using just a single file named fortune.py.
# fortune.py
import random
def main():
# Choose language
lang_code = input('Choose a language (jp/en): ')
# Switch text based on the selected language
if lang_code == 'en':
# messages stores various text strings used in the app as a dict
messages = {
'app_title': 'Fortune Cookie',
'prompt_open': 'Would you like to open a cookie? (y/n): ',
'fortune_prefix': 'Your fortune for today is:',
'goodbye': 'See you tomorrow with more luck!'
}
# fortunes is a list of fortunes displayed at random
fortunes = [
'Great Fortune! A wonderful day awaits you.',
'Fair Fortune. Keep steady progress.',
'Little Fortune. Stay patient and try again.',
'Bad Fortune... but tomorrow is another chance.'
]
else: # 'jp' or any other input
# messages stores various text strings used in the app as a dict
messages = {
'app_title': 'フォーチュンクッキー',
'prompt_open': 'クッキーを開きますか? (y/n): ',
'fortune_prefix': 'あなたの今日の運勢は:',
'goodbye': 'また明日も幸運を!',
}
# fortunes is a list of fortunes displayed at random
fortunes = [
'大吉!最高の一日になりそうです!',
'中吉。ちょっとした幸運が訪れるでしょう。',
'小吉。小さな努力が実を結びます。',
'凶...でも心配しすぎないでください。'
]
# Open a cookie?
print(f"=== {messages['app_title']} ===")
answer = input(messages['prompt_open'])
if answer == 'y':
print(f"{messages['fortune_prefix']} {random.choice(fortunes)}")
else:
print(messages['goodbye'])
main()
When you run this code, the program displays a random message depending on the selected language. Selecting "en" shows English text, while "jp" displays Japanese text.
The random.choice function on line 47 selects one random element from a list passed as its argument. It works by importing the random module using import random.
Improvement Plan: Organize the Code by Splitting Files
The current version works as a single file, but it has a problem: fortune.py contains everything.
If you want to add new languages in the future—such as French or Spanish—you would have to keep adding more language data directly into fortune.py. The file will grow longer and longer, making it difficult to see which part belongs to which language.
Additionally, when someone needs to update the messages, they must edit fortune.py directly. This makes it hard to separate the work between translators and developers.
To solve this, we will “split files by language.” By organizing the project as follows, the structure becomes much clearer, and we can also create modules to manage language selection.
fortune_cookie/
├─ fortune.py # Main executable file
└─ manager/
├─ __init__.py # Package initializer
└─ language_manager.py # Manages selected languages
└─ message/
├─ __init__.py # Package initializer
├─ language_en.py # English messages
└─ language_jp.py # Japanese messages
This structure allows fortune.py to contain only the minimum logic about which modules to use, while each language’s data is stored separately.
Starting in the next lecture, we will gradually split the code into multiple files and learn the concepts of modules and packages step by step.
Because this section focuses on modules and packages, we use Python files for language management (specifically language_jp.py and language_en.py).
However, in real applications, translators typically edit Excel or similar files, and developers import those datasets. Excel makes it easy to list primary and secondary languages side by side, which improves readability and editability. Translators are usually not programmers, so giving them direct access to Python files can introduce risks.
Please keep in mind that the approach in this section is not necessarily the ideal method for real-world language management.






