Skip to content

🐛 Fix __vsc_ipynb_file__ unaccessible through get_ipython().user_ns in VS Code #287

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

Merged
merged 2 commits into from
Mar 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions nbproject/dev/_jupyter_communicate.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import sys
from itertools import chain
from pathlib import PurePath
from urllib import request
Expand Down Expand Up @@ -90,19 +91,22 @@
logger.warning("Can not import get_ipython.")
return None

# vs code specific
if "__main__" in sys.modules:
main_module = sys.modules["__main__"]
if hasattr(main_module, "__vsc_ipynb_file__"):
nb_path = main_module.__vsc_ipynb_file__
return (

Check warning on line 99 in nbproject/dev/_jupyter_communicate.py

View check run for this annotation

Codecov / codecov/patch

nbproject/dev/_jupyter_communicate.py#L95-L99

Added lines #L95 - L99 were not covered by tests
(nb_path, "vs_code" if env is None else env) if return_env else nb_path
)

ipython_instance = get_ipython()

# not in an ipython kernel
if ipython_instance is None:
logger.warning("The IPython instance is empty.")
return None

# vs code specific
user_ns = ipython_instance.__dict__.get("user_ns", {})
if "__vsc_ipynb_file__" in user_ns:
nb_path = PurePath(user_ns["__vsc_ipynb_file__"])
return (nb_path, "vs_code" if env is None else env) if return_env else nb_path

config = ipython_instance.config
# not in a jupyter notebook
if "IPKernelApp" not in config:
Expand Down
2 changes: 0 additions & 2 deletions noxfile.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import nox
from laminci.nox import build_docs, run_pre_commit, run_pytest

nox.options.default_backend = "none"


@nox.session
def lint(session: nox.Session) -> None:
Expand Down