Practice: Python Packages
Q1. Build a tiny package
Create a package named pets with an empty __init__.py and a module dog.py containing the function bark() that returns 'Woof'. Import it with from-import and print bark().
Goal: Create a package
Q2. Import a name directly
Create a package calc with module ops.py that defines add(a, b). Import add directly using from calc.ops import add and print add(2, 3).
Goal: Import from a submodule
Q3. Re-export in __init__
Create a package text whose __init__.py re-exports shout from text/words.py (shout(s) returns s.upper()). Then import shout from text and print shout('hey').
Goal: Design a public interface
Q4. Relative import in action
Create package box with modules a.py (defines VALUE = 10) and b.py that uses from .a import VALUE and defines double() returning VALUE * 2. Import box.b and print box.b.double().
Goal: Write a relative import
Q5. Subpackage import
Create package top with subpackage top/sub (with __init__.py) and module top/sub/leaf.py that defines NAME = 'leaf'. Import it and print top.sub.leaf.NAME.
Goal: Use a subpackage
Q6. List the modules
Create package kit with __init__.py and modules one.py and two.py. Use pkgutil.iter_modules on kit.__path__ and print the sorted module names as a list.
Goal: Discover modules
Q7. Package init runs once
What does this print?
import sys
sys.path.insert(0, ".")
from pathlib import Path
def make(files):
for name, text in files.items():
p = Path(name)
p.parent.mkdir(parents=True, exist_ok=True)
p.write_text(text)
make({"p1/__init__.py": "print('hello from init')\n", "p1/m.py": "X = 3\n"})
import p1
import p1.m
print(p1.m.X)Goal: Know when __init__ runs
Q8. Module name inside a package
What does this print?
import sys
sys.path.insert(0, ".")
from pathlib import Path
def make(files):
for name, text in files.items():
p = Path(name)
p.parent.mkdir(parents=True, exist_ok=True)
p.write_text(text)
make({"pk/__init__.py": "", "pk/mod.py": "A = 1\n"})
import pk.mod
print(pk.mod.__name__)Goal: See the dotted name
Q9. Re-exported name
What does this print?
import sys
sys.path.insert(0, ".")
from pathlib import Path
def make(files):
for name, text in files.items():
p = Path(name)
p.parent.mkdir(parents=True, exist_ok=True)
p.write_text(text)
make({"rx/__init__.py": "from .inner import f\n", "rx/inner.py": "def f():\n return 7\n"})
import rx
import rx.inner
print(rx.f())
print(rx.f is rx.inner.f)Goal: Understand re-exports
Q10. Two dots
What does this print?
import sys
sys.path.insert(0, ".")
from pathlib import Path
def make(files):
for name, text in files.items():
p = Path(name)
p.parent.mkdir(parents=True, exist_ok=True)
p.write_text(text)
make({"pkg/__init__.py": "", "pkg/helper.py": "WHO = 'helper'\n", "pkg/sub/__init__.py": "", "pkg/sub/use.py": "from ..helper import WHO\n"})
from pkg.sub import use
print(use.WHO)Goal: Read a relative import
Q11. Is it a regular package?
What does this print?
import sys
sys.path.insert(0, ".")
from pathlib import Path
def make(files):
for name, text in files.items():
p = Path(name)
p.parent.mkdir(parents=True, exist_ok=True)
p.write_text(text)
make({"reg/__init__.py": "", "ns/m.py": "A = 1\n"})
import reg
import ns.m
print(hasattr(reg, '__file__'))
print(ns.__file__ is None)Goal: Namespace packages
Q12. Fix the import path
The code below fails because the import path is wrong. Fix it so it prints 9.
from ops import triple
print(triple(3))Goal: Use dotted paths
Q13. Fix the relative import
Module reader.py inside package docs wrongly says: from docs import fmt ... but fmt.py is its sibling and the package should not depend on its own top name. Rewrite it with a relative import so the program prints [x].
from docs import fmt
def show(s):
return fmt.wrap(s)Goal: Use relative imports
Q14. Package or module?
Explain the difference between a module and a package, and say what happens when a package is imported.
Goal: Module vs package
Q15. Why not run a package file directly?
A file inside a package uses a relative import. You run it with python app/core/engine.py and see an error. Explain why and how to run it correctly.
Goal: Understand -m
Q16. Plan a reports package
You are writing a program that makes sales and stock reports and needs shared date helpers. Sketch a package layout and say which names you would re-export from __init__.py.
Goal: Design a package