Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
kerrickstaley opened this issue Jan 30, 2024 · 2 comments

Comments

@kerrickstaley
Copy link

kerrickstaley commented Jan 30, 2024

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 kerrickstaley changed the title Using jupytext on Using jupytext in a nested project can result in re-import of the current module Jan 30, 2024
@kerrickstaley
Copy link
Author

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
Copy link
Owner

mwouts commented Apr 27, 2024

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants