From Python relative imports for the billionth time:
- For a
from .. importto work, the module's name must have at least as many dots as there are in theimportstatement. - ... if you run the interpreter interactively ... the name of that interactive session is
__main__ - Thus you cannot do relative imports directly from an interactive session
I like to use interactive Jupyter Notebook sessions to explore data and test modules before writing production code. To make things clear and accessible to teammates, I like to place the notebooks in an interactive package located alongside the packages and modules I am testing.
package/
__init__.py
subpackage1/
__init__.py
moduleX.py
moduleY.py
moduleZ.py
subpackage2/
__init__.py
moduleZ.py
interactive/
__init__.py
my_notebook.ipynb
During an interactive session in interactive.my_notebook.ipynb, how would you import other modules like subpackage1.moduleX and subpackage2.moduleZ?