Skip to content

Commit 2cac684

Browse files
authored
🐛 Fix __vsc_ipynb_file__ unaccessible through get_ipython().user_ns in VS Code (#287)
* Fix __vsc_ipynb_file__ unaccessible through user_ns * fix
1 parent 72e9ea3 commit 2cac684

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

nbproject/dev/_jupyter_communicate.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import sys
23
from itertools import chain
34
from pathlib import PurePath
45
from urllib import request
@@ -90,19 +91,22 @@ def notebook_path(return_env=False):
9091
logger.warning("Can not import get_ipython.")
9192
return None
9293

94+
# vs code specific
95+
if "__main__" in sys.modules:
96+
main_module = sys.modules["__main__"]
97+
if hasattr(main_module, "__vsc_ipynb_file__"):
98+
nb_path = main_module.__vsc_ipynb_file__
99+
return (
100+
(nb_path, "vs_code" if env is None else env) if return_env else nb_path
101+
)
102+
93103
ipython_instance = get_ipython()
94104

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

100-
# vs code specific
101-
user_ns = ipython_instance.__dict__.get("user_ns", {})
102-
if "__vsc_ipynb_file__" in user_ns:
103-
nb_path = PurePath(user_ns["__vsc_ipynb_file__"])
104-
return (nb_path, "vs_code" if env is None else env) if return_env else nb_path
105-
106110
config = ipython_instance.config
107111
# not in a jupyter notebook
108112
if "IPKernelApp" not in config:

noxfile.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import nox
22
from laminci.nox import build_docs, run_pre_commit, run_pytest
33

4-
nox.options.default_backend = "none"
5-
64

75
@nox.session
86
def lint(session: nox.Session) -> None:

0 commit comments

Comments
 (0)