Predict the Import Path
Intermediate · Predict the output · from How to Create and Structure a Python Package
From the lesson: How to Create and Structure a Python Package
Imagine you have a filing cabinet. Right now, all your papers are thrown in one big drawer - recipes mixed with homework mixed with tax forms. Finding anything is a nightmare.
Read the lessonRead the code below carefully. It simulates how Python resolves import paths in a nested package. What will it print?
package_tree = {
'myapp': {
'models': ['user', 'product'],
'views': ['home', 'dashboard'],
}
}
def resolve_import(package, subpackage, module):
if package in package_tree:
if subpackage in package_tree[package]:
if module in package_tree[package][subpackage]:
return f'{package}.{subpackage}.{module}'
return 'ImportError'
print(resolve_import('myapp', 'models', 'user'))
print(resolve_import('myapp', 'views', 'dashboard'))
print(resolve_import('myapp', 'utils', 'helpers'))
More Package exercises
- Build a Package Structure DictionaryBeginner
- Fix the Broken __all__ ExportIntermediate
- Refactor Into a Package RegistryAdvanced
All Python exercises · 50 practice problems · Modules & Packaging quiz