What is a package in Python?
a) A single file containing Python code
b) A collection of modules organized in directories
c) A built-in function
d) A variable that stores multiple values
Answer:
b) A collection of modules organized in directories
Explanation:
A package in Python is a collection of modules organized in directories that allows you to structure your Python code into a hierarchical manner. Packages can contain sub-packages and modules, making it easier to organize large codebases.
# Directory structure
# my_package/
# __init__.py
# module1.py
# module2.py
# Importing a module from a package
from my_package import module1
module1.some_function()
The __init__.py
file in a package directory indicates that the directory should be treated as a package. It can also contain initialization code for the package.
Using packages allows you to group related modules together, making your code more modular, maintainable, and easier to navigate.