0
0
Pythonprogramming~10 mins

Package structure and usage in Python - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Package structure and usage
Create package folder
Add __init__.py file
Add modules (python files)
Import package/module in main script
Use functions/classes from package
Run main script
This flow shows how to create a package folder with __init__.py, add modules, import them, and use their contents in a main script.
Execution Sample
Python
mypackage/
  __init__.py
  greetings.py

# greetings.py
def say_hello():
    return 'Hello!'

# main.py
from mypackage.greetings import say_hello
output = say_hello()
print(output)
This code creates a package with a greetings module and uses its function in main.py to print a greeting.
Execution Table
StepActionFile/ModuleResult/Output
1Create folder 'mypackage' and add __init__.pymypackage/__init__.pyPackage recognized by Python
2Write function say_hello in greetings.pymypackage/greetings.pyFunction say_hello defined
3Import say_hello from mypackage.greetings in main.pymain.pysay_hello function available
4output = say_hello()main.pyoutput = 'Hello!'
5print(output)main.pyOutput: Hello!
💡 Program ends after printing the greeting.
Variable Tracker
VariableStartAfter callFinal
say_hellofunction definedcalledreturned 'Hello!'
outputundefined'Hello!''Hello!'
Key Moments - 3 Insights
Why do we need the __init__.py file in the package folder?
The __init__.py file tells Python this folder is a package, so imports like 'from mypackage.greetings' work. See execution_table step 1.
How does Python find the say_hello function when we import it?
Python looks inside the greetings.py module in the mypackage folder because of the import statement in step 3.
What happens if we forget to call the function before printing?
If we print say_hello without parentheses, it prints the function object, not the string. See step 4 and 5 for correct call.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step is the package recognized by Python?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Check the 'Result/Output' column for when the package is recognized.
According to variable_tracker, what is the value of 'output' after the function call?
Afunction object
B'Hello!'
Cundefined
DNone
💡 Hint
Look at the 'After call' and 'Final' columns for 'output' variable.
If we remove __init__.py from the package folder, what will happen when importing?
AImport will fail because Python won't recognize the folder as a package
BImport will work normally
CFunction will return None
DPython will run the function automatically
💡 Hint
Refer to key_moments about the role of __init__.py and execution_table step 1.
Concept Snapshot
Package structure in Python:
- Create a folder for the package
- Add __init__.py file inside (can be empty)
- Add modules (python files) inside the folder
- Import modules/functions using 'from package.module import func'
- Use imported functions/classes in your script
- Run the script to use the package
Full Transcript
To create a Python package, first make a folder and add an __init__.py file inside it. This file tells Python the folder is a package. Then add Python files (modules) with functions or classes. In your main script, import what you need from the package modules. Call the functions and print or use their results. The execution table shows each step: creating the package, defining functions, importing, calling, and printing output. Variables track the function and output values. Key moments explain why __init__.py is needed and how imports work. The quiz tests understanding of these steps and concepts.