What is the difference between absolute and relative imports in Python?
An absolute import gives the full path from the project root, like from shop.billing import total. A relative import uses dots to start from the current package, like from .billing import total. Relative imports work only inside packages.
Explain it simply
Absolute is giving a full postal address: "Flat 4, Gandhi Road, Pune". Relative is saying "the house next door" or "one street up", which only makes sense if you know where you are standing now. One dot means this package, two dots mean the parent package.
Example
shop/
__init__.py
billing.py
utils/
__init__.py
money.py# inside shop/billing.py
from shop.utils.money import rupees # absolute
from .utils.money import rupees # relative, same package
from . import utils # the sibling package
# inside shop/utils/money.py
from ..billing import total # one level upRunning a file inside a package directly, like python shop/billing.py, makes relative imports fail with "attempted relative import with no known parent package". Run it as a module instead: python -m shop.billing.
What to say in the interview
"Absolute imports spell out the full package path and are the recommended default because they are clear and survive file moves. Relative imports use dots and are handy inside a package, but only work when the file is imported as part of that package, not when run as a plain script."