Loading samples...
Loading samples...
py-sample-15
Import, create, and manage Python modules
Which keyword imports a module?
How to import specific function from module?
What is __init__.py for?
Which reloads a module after changes?
What does if __name__ == '__main__': do?
How to alias an import?
What is sys.path?
Which imports everything from module?
What is PYTHONPATH?
What is pip used for?
Explain the difference between import module and from module import func.
[3 marks]What is a Python package? How is it different from a module?
[3 marks]Explain absolute vs relative imports with examples.
[3 marks]What happens when a module is imported twice? Why?
[3 marks]Explain the purpose of __all__ in a module.
[3 marks]What are circular imports? How to avoid them?
[3 marks]Explain how Python finds modules during import.
[3 marks]What is site-packages directory? Where is it located?
[3 marks]Explain pip, conda, and poetry as package managers.
[3 marks]What is a virtual environment and why use it?
[3 marks]Create a simple calculator module with add, sub, mul, div functions.
[5 marks]Write a package structure with nested subpackages and demonstrate imports.
[5 marks]Create a configuration module that reads settings from JSON file.
[5 marks]Write a logging utility module with different log levels.
[5 marks]Create a plugin system that dynamically loads modules from a directory.
[5 marks]Write a requirements.txt generator that scans imports in Python files.
[5 marks]Predict output: # mymodule.py print('Module loaded') # main.py import mymodule import mymodule
[2 marks]Explanation:Module is cached in sys.modules after first import. Second import uses cached version, doesn't re-execute.
Find error: from math import sqrt print(math.sqrt(16))
[2 marks]Explanation:Only sqrt imported, not math module. Use 'sqrt(16)' directly or 'import math' to use math.sqrt.
Predict output: # pkg/__init__.py print('Init') x = 10 # main.py from pkg import x print(x)
[2 marks]Explanation:Package __init__.py executes on import. x is imported from pkg namespace.
Find error: # mymod.py def func(): import nonexistent func()
[3 marks]Explanation:Import inside function still needs module to exist. Error raised when func() called.
Predict output: if __name__ == '__main__': print('Running directly') else: print('Imported as module')
[2 marks]Explanation:When file executed directly, __name__ is '__main__'. When imported, __name__ is module name.
Find error: import sys sys.path.append('/my/modules') from mymodule import *
[3 marks]Explanation:from X import * only imports names in __all__ or names without leading underscore. May not include expected names.
Predict output: # main.py import mymod print(mymod.__file__)
[2 marks]Explanation:__file__ attribute shows where module was loaded from. Usually absolute path to .py or cached .pyc.
Find error: # mypackage/module.py from . import sibling
[3 marks]Explanation:Relative imports only work inside packages. Running module.py directly makes it __main__, not a package. Use -m flag or absolute imports.