Open kerrickstaley opened 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:
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?
I'm using Jupytext on a project with a nested structure, something like
I run
to launch Jupyter so that e.g.
works inside
foo/__init__.py
.The problem I'm seeing is that if I put the line
inside
foo/__init__.py
, then when I runfoo/__init__.py
in Jupyter, the module is imported twice, once assys.modules['__main__']
and once assys.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 isfoo
and not__main__
when we load it in Jupyter.