normally when we encounter an ImportError if we attempt to use a relative import
in an interative notebook
>>> from .relative import script
Traceback (most recent call last):
...
ImportError: attempted relative import with no known parent package
%%wecanmaketheinteractiveshellimitateapackagethatwouldhaverelativeimportsbyincluding`__path__`and`__package__`variables.__path__,__package__=[""],"__main__"thestatementaboveunlocksrelativeimports,andwedon't fail the following statements.>>>from.relativeimportscript>>>script<module'__main__.relative.script'=""...=""from="">
we can make the interactive shell imitate a package that would have relative imports
by including __path__ and __package__ variables.
__path__, __package__ = [""], "__main__"
the statement above unlocks relative imports, and we don't fail the following statements.
>>> from .relative import script
>>> script
<module '__main__.relative.script' from ...>