Package
- A package is a folder containing one or more modules and a special file named
__init__.py. - The
__init__.pyfile can be empty or contain initialization code for the package. - Packages can also contain sub-packages (folders inside folders).
How to create a package:
Suppose you want a package named mytools with two modules, string_ops.py and number_ops.py.
Folder structure:
Example content:
# string_ops.py
def to_upper(text):
return text.upper()
# number_ops.py
def multiply(a, b):
return a * b
How to use the package:
from mytools import string_ops, number_ops
print(string_ops.to_upper("hello")) # Output: HELLO
print(number_ops.multiply(4, 5)) # Output: 20
Or import individual functions:
What does __init__.py do?
- It tells Python that the directory is a package.
- It can be empty.
- It can also be used to specify what is available when you import the package, by defining the
__all__list or importing submodules inside it.
Example:
Now, you can directly import functions from the package:
Summary
- Module = single
.pyfile. - Package = folder with
__init__.py+ modules/sub-packages. - Use packages to organize related modules for cleaner, modular code.