Bird
Raised Fist0
Pythonprogramming~10 mins

__init__ file role in Python - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - __init__ file role
Create folder
Add __init__.py file
Python treats folder as package
Import modules from package
Use package contents in code
The __init__.py file tells Python to treat a folder as a package, allowing imports from it.
Execution Sample
Python
my_package/
  __init__.py
  module.py

# In main.py
from my_package import module
module.hello()
This code shows how __init__.py makes my_package a package so module can be imported.
Execution Table
StepActionEffectResult
1Create folder 'my_package'Folder createdEmpty folder
2Add empty __init__.py fileFolder becomes a packagemy_package is a package
3Add module.py with function hello()Module ready inside packagemodule.py available
4In main.py, import module from my_packagePython loads package and modulemodule imported
5Call module.hello()Function runsPrints greeting message
6End of programExecution stopsProgram ends
💡 Program ends after calling function from module inside package
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 5Final
my_packagefolderpackagepackage with modulepackage with modulepackage with module
modulenonenoneimported modulemodule function calledmodule loaded
Key Moments - 3 Insights
Why do we need an __init__.py file in the folder?
Because without __init__.py, Python does not recognize the folder as a package, so imports from it will fail (see execution_table step 2).
Can __init__.py be empty?
Yes, it can be empty and still mark the folder as a package (see execution_table step 2). It can also run setup code if needed.
What happens if __init__.py is missing?
Python treats the folder as a normal directory, so importing modules from it will cause errors (see execution_table step 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step does Python treat the folder as a package?
AStep 2
BStep 1
CStep 4
DStep 5
💡 Hint
Check the 'Effect' column in execution_table step 2 about folder becoming a package.
According to variable_tracker, what is the state of 'module' after step 4?
ANot created
BImported module
CFunction called
DNone
💡 Hint
Look at the 'module' row and 'After Step 4' column in variable_tracker.
If __init__.py was missing, what would happen at step 4 in execution_table?
AFolder still treated as package
BFunction runs normally
CImport would fail
DProgram ends successfully
💡 Hint
Refer to key_moments question about missing __init__.py and execution_table step 4.
Concept Snapshot
__init__.py file role in Python:
- Marks a folder as a package
- Can be empty or contain setup code
- Enables importing modules from the folder
- Without it, imports from folder fail
- Essential for package structure
Full Transcript
The __init__.py file is a special file placed inside a folder to tell Python that this folder should be treated as a package. When Python sees __init__.py, it allows importing modules from that folder as a package. The file can be empty or contain code to run when the package is imported. Without __init__.py, Python treats the folder as a normal directory and importing modules from it will cause errors. This visual trace shows creating a folder, adding __init__.py, adding a module, importing it, and calling a function from it. Variables track the package and module states step by step. Key moments clarify why __init__.py is needed and what happens if it is missing. The quiz tests understanding of these steps and states.

Practice

(1/5)
1. What is the main role of a __init__.py file in a Python folder?
easy
A. To mark the folder as a Python package
B. To store global variables for the project
C. To execute the main program code
D. To compile Python files into bytecode

Solution

  1. Step 1: Understand the purpose of __init__.py

    The __init__.py file tells Python that the folder should be treated as a package.
  2. Step 2: Differentiate from other file roles

    It does not store global variables, run main code, or compile files; its role is to mark the folder as a package.
  3. Final Answer:

    To mark the folder as a Python package -> Option A
  4. Quick Check:

    __init__.py marks package = C [OK]
Hint: Remember: __init__.py means 'this is a package' [OK]
Common Mistakes:
  • Thinking it runs main program code
  • Confusing it with a config file
  • Assuming it compiles Python files
2. Which of the following is a correct way to create an empty __init__.py file in a package folder?
easy
A. Create a file named __init__.py with no content
B. Create a file named init.py with no content
C. Create a file named __init__.py with a main() function
D. Create a file named __init__.txt with no content

Solution

  1. Step 1: Identify the exact filename required

    The file must be named exactly __init__.py to mark the folder as a package.
  2. Step 2: Confirm that it can be empty

    The file can be empty; no code is required inside for it to work.
  3. Final Answer:

    Create a file named __init__.py with no content -> Option A
  4. Quick Check:

    Empty __init__.py file = B [OK]
Hint: Filename must be exactly __init__.py [OK]
Common Mistakes:
  • Using wrong filename like init.py or __init__.txt
  • Adding unnecessary code inside when empty is fine
  • Confusing with main.py or other files
3. Given this folder structure and files:
mypackage/
  __init__.py
  module.py

# __init__.py content:
print('Package imported')

# module.py content:
def greet():
    return 'Hello!'

# main.py content:
import mypackage
from mypackage import module
print(module.greet())

What will be the output when running main.py?
medium
A. Hello!\nPackage imported
B. Package imported\nHello!
C. Hello!
D. ImportError

Solution

  1. Step 1: Understand import behavior

    When import mypackage runs, the code inside __init__.py executes, printing 'Package imported'.
  2. Step 2: Check subsequent import and function call

    Then from mypackage import module imports the module, and print(module.greet()) prints 'Hello!'.
  3. Final Answer:

    Package imported Hello! -> Option B
  4. Quick Check:

    Init runs first, then greet() output = A [OK]
Hint: Code in __init__.py runs on package import [OK]
Common Mistakes:
  • Assuming __init__.py code does not run
  • Mixing order of printed lines
  • Expecting ImportError without reason
4. You have a folder named utils with a file helper.py inside. You try to import helper using import utils.helper but get ModuleNotFoundError. What is the most likely cause?
medium
A. The helper.py file has syntax errors
B. The helper.py file is empty
C. You need to run Python with administrator rights
D. The utils folder is missing __init__.py

Solution

  1. Step 1: Understand package import requirements

    Python requires an __init__.py file in a folder to treat it as a package for imports like utils.helper.
  2. Step 2: Identify cause of ModuleNotFoundError

    If __init__.py is missing, Python does not recognize utils as a package, causing the error.
  3. Final Answer:

    The utils folder is missing __init__.py -> Option D
  4. Quick Check:

    Missing __init__.py causes import error = D [OK]
Hint: Always add __init__.py to folders for imports [OK]
Common Mistakes:
  • Blaming syntax errors without checking
  • Thinking admin rights affect imports
  • Assuming empty files cause import failure
5. You want to create a package shapes with subpackage polygons. You want importing shapes to automatically import polygons as well. How should you modify shapes/__init__.py to achieve this?
hard
A. Add import shapes.polygons inside shapes/__init__.py
B. Add import polygons inside shapes/__init__.py
C. Add from . import polygons inside shapes/__init__.py
D. Leave shapes/__init__.py empty; Python imports subpackages automatically

Solution

  1. Step 1: Understand relative imports in packages

    To import a subpackage inside a package's __init__.py, use relative import syntax like from . import polygons.
  2. Step 2: Avoid absolute import inside the package

    Using import polygons or import shapes.polygons may cause errors or circular imports; relative import is preferred.
  3. Step 3: Confirm that empty __init__.py does not import subpackages

    Python does not import subpackages automatically; explicit import is needed.
  4. Final Answer:

    Add from . import polygons inside shapes/__init__.py -> Option C
  5. Quick Check:

    Use relative import from . import polygons = A [OK]
Hint: Use relative import from . import subpackage in __init__.py [OK]
Common Mistakes:
  • Using absolute import inside __init__.py
  • Expecting automatic subpackage import
  • Forgetting to add __init__.py in subpackage