Bird
0
0

You have a package folder analytics with two modules: stats.py and plots.py. You want importing analytics to automatically import both modules. Which __init__.py content achieves this?

hard📝 Application Q8 of 15
Python - Modules and Code Organization
You have a package folder analytics with two modules: stats.py and plots.py. You want importing analytics to automatically import both modules. Which __init__.py content achieves this?
Aimport analytics.stats import analytics.plots
Bimport stats import plots
Cfrom .stats import * from .plots import *
Dfrom stats import * from plots import *
Step-by-Step Solution
Solution:
  1. Step 1: Understand relative imports

    Within a package, relative imports using from .module import * correctly import submodules.
  2. Step 2: Avoid absolute imports inside package

    Using import analytics.stats inside analytics/__init__.py causes circular import errors.
  3. Step 3: Correct syntax

    Using from .stats import * and from .plots import * imports all public names from both modules when the package is imported.
  4. Final Answer:

    from .stats import * from .plots import * -> Option C
  5. Quick Check:

    Use relative imports inside __init__.py for submodules [OK]
Quick Trick: Use relative imports inside __init__.py to import submodules [OK]
Common Mistakes:
  • Using absolute imports causing circular import errors
  • Importing modules without relative dot prefix inside package
  • Assuming import statements outside package work inside __init__.py

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes