Importing
Modules let you organize code into files; packages group modules. Importing makes symbols from modules available in your current namespace.
# import module
import math
print(math.sqrt(2))
# import symbol
from math import sqrt
print(sqrt(2))
# alias
import numpy as np
Module layout (package)
- mypkg/
- init.py
- mod.py
% MATLAB uses the path and packages via +foldername
% Add folder to path or put on MATLAB path
addpath('mypkg')
% Call functions in folder
result = myfunc(1);
Gotchas
- Python: circular imports can cause runtime errors; structure modules to avoid mutual dependencies.
- Use absolute imports in packages to be clear; relative imports are useful for internal modules.
- Matlab package folders (starting with
+) create namespaces; ensure path visibility.