From 7f6d8e719cfa17127120749c01075180216e011e Mon Sep 17 00:00:00 2001 From: Quentin Peter Date: Fri, 15 Mar 2024 03:43:53 +0100 Subject: [PATCH 1/4] syspath --- spyder_kernels/console/kernel.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/spyder_kernels/console/kernel.py b/spyder_kernels/console/kernel.py index 9c837f62..6d36768d 100644 --- a/spyder_kernels/console/kernel.py +++ b/spyder_kernels/console/kernel.py @@ -76,6 +76,9 @@ def __init__(self, *args, **kwargs): # Socket to signal shell_stream locally self.loopback_socket = None + # Save set syspath + self._syspath = [] + @property def kernel_info(self): # Used for checking correct version by spyder @@ -675,6 +678,8 @@ def set_configuration(self, conf): self._load_wurlitzer() elif key == "autoreload_magic": self._autoreload_magic(value) + elif key == "syspath": + self.set_syspath(value) return ret def set_color_scheme(self, color_scheme): @@ -764,8 +769,7 @@ def set_special_kernel(self, special): raise NotImplementedError(f"{special}") - @comm_handler - def update_syspath(self, path_dict, new_path_dict): + def set_syspath(self, new_path_dict): """ Update the PYTHONPATH of the kernel. @@ -776,9 +780,10 @@ def update_syspath(self, path_dict, new_path_dict): `new_path_dict` corresponds to the new state of the PYTHONPATH. """ # Remove old paths - for path in path_dict: + for path in self._syspath: while path in sys.path: sys.path.remove(path) + self._syspath = new_path_dict # Add new paths pypath = [path for path, active in new_path_dict.items() if active] From 91c4e7e9da850d442ec8a77d895aafab00ce3820 Mon Sep 17 00:00:00 2001 From: Quentin Peter Date: Sat, 16 Mar 2024 07:37:29 +0100 Subject: [PATCH 2/4] save initial pythonpath --- spyder_kernels/console/kernel.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/spyder_kernels/console/kernel.py b/spyder_kernels/console/kernel.py index 6d36768d..7e5b6f5c 100644 --- a/spyder_kernels/console/kernel.py +++ b/spyder_kernels/console/kernel.py @@ -78,6 +78,10 @@ def __init__(self, *args, **kwargs): # Save set syspath self._syspath = [] + # This is added to the path in spydercustomize.py + pypath = os.environ.get('SPY_PYTHONPATH') + if pypath: + self._syspath = pypath.split(os.pathsep) @property def kernel_info(self): From 324ed3daa98f6ae18785a1a859265cf93e662bfa Mon Sep 17 00:00:00 2001 From: Quentin Peter Date: Sat, 16 Mar 2024 08:39:10 +0100 Subject: [PATCH 3/4] remove dict from kernel --- spyder_kernels/console/kernel.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/spyder_kernels/console/kernel.py b/spyder_kernels/console/kernel.py index 7e5b6f5c..9f1dd7f4 100644 --- a/spyder_kernels/console/kernel.py +++ b/spyder_kernels/console/kernel.py @@ -773,7 +773,7 @@ def set_special_kernel(self, special): raise NotImplementedError(f"{special}") - def set_syspath(self, new_path_dict): + def set_syspath(self, pypath): """ Update the PYTHONPATH of the kernel. @@ -787,10 +787,9 @@ def set_syspath(self, new_path_dict): for path in self._syspath: while path in sys.path: sys.path.remove(path) - self._syspath = new_path_dict + self._syspath = pypath # Add new paths - pypath = [path for path, active in new_path_dict.items() if active] if pypath: sys.path.extend(pypath) os.environ.update({'PYTHONPATH': os.pathsep.join(pypath)}) From 4199a68dbdf729befb62e9641efda4fc6798d7b1 Mon Sep 17 00:00:00 2001 From: Quentin Peter Date: Sat, 16 Mar 2024 09:32:50 +0100 Subject: [PATCH 4/4] remove SPY_PYTHONPATH --- spyder_kernels/console/kernel.py | 4 ---- spyder_kernels/customize/spydercustomize.py | 14 -------------- 2 files changed, 18 deletions(-) diff --git a/spyder_kernels/console/kernel.py b/spyder_kernels/console/kernel.py index 9f1dd7f4..8a48f48b 100644 --- a/spyder_kernels/console/kernel.py +++ b/spyder_kernels/console/kernel.py @@ -78,10 +78,6 @@ def __init__(self, *args, **kwargs): # Save set syspath self._syspath = [] - # This is added to the path in spydercustomize.py - pypath = os.environ.get('SPY_PYTHONPATH') - if pypath: - self._syspath = pypath.split(os.pathsep) @property def kernel_info(self): diff --git a/spyder_kernels/customize/spydercustomize.py b/spyder_kernels/customize/spydercustomize.py index 99c20898..7efb3bb6 100644 --- a/spyder_kernels/customize/spydercustomize.py +++ b/spyder_kernels/customize/spydercustomize.py @@ -252,17 +252,3 @@ def _patched_get_terminal_size(fd=None): # Pdb adjustments # ============================================================================= pdb.Pdb = SpyderPdb - -# ============================================================================= -# PYTHONPATH and sys.path Adjustments -# ============================================================================= -# PYTHONPATH is not passed to kernel directly, see spyder-ide/spyder#13519 -# This allows the kernel to start without crashing if modules in PYTHONPATH -# shadow standard library modules. -def set_spyder_pythonpath(): - pypath = os.environ.get('SPY_PYTHONPATH') - if pypath: - sys.path.extend(pypath.split(os.pathsep)) - os.environ.update({'PYTHONPATH': pypath}) - -set_spyder_pythonpath()