mwouts / jupytext

Jupyter Notebooks as Markdown Documents, Julia, Python or R scripts
https://jupytext.readthedocs.io
MIT License
6.6k stars 386 forks source link

Using jupytext in a nested project can result in re-import of the current module #1208

Open kerrickstaley opened 8 months ago

kerrickstaley commented 8 months ago

I'm using Jupytext on a project with a nested structure, something like

__init__.py
util.py
foo/__init__.py
foo/bar.py

I run

PYTHONPATH=$PWD jupyter notebook

to launch Jupyter so that e.g.

import util

works inside foo/__init__.py.

The problem I'm seeing is that if I put the line

from foo.bar import baz

inside foo/__init__.py, then when I run foo/__init__.py in Jupyter, the module is imported twice, once as sys.modules['__main__'] and once as sys.modules['foo']. This is confusing and can lead to weird issues when you haven't saved your notebook, so the .py file on disk differs from the file you have loaded in Jupyter.

Is there a good way to deal with this problem? In this example, what we want is for foo/__init__.py to "know" that it is foo and not __main__ when we load it in Jupyter.

kerrickstaley commented 8 months ago

I created a repo demonstrating this issue. To repro the problem, clone the repo, then run

PYTHONPATH=$PWD jupyter notebook

from the repo root. In the Jupyter UI that appears, open foo/__init__.py and run all cells. You can see that the code in foo/__init__.py is executed twice:

Screenshot 2024-01-29 at 8 52 05 PM
mwouts commented 5 months ago

Thank you @kerrickstaley for reporting this. I am not sure how I can help with this. Would you get the same issue if you were to run foo/__init__.py in a different interpreter? If that issue is specific to Jupyter then maybe you want to make you script aware of whether it runs in Jupyter?