Skip to content

Commit 5f68078

Browse files
committed
Update export_pythonpath
1 parent b6f8da4 commit 5f68078

File tree

2 files changed

+44
-14
lines changed

2 files changed

+44
-14
lines changed

spyder/plugins/pythonpath/widgets/pathmanager.py

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from spyder.api.widgets.mixins import SpyderWidgetMixin
2626
from spyder.config.base import _
2727
from spyder.plugins.pythonpath.utils import check_path
28+
from spyder.utils.environ import get_user_env, set_user_env
2829
from spyder.utils.misc import getcwd_or_home
2930
from spyder.utils.stylesheet import (
3031
AppStyle,
@@ -50,7 +51,6 @@ class PathManager(QDialog, SpyderWidgetMixin):
5051

5152
redirect_stdio = Signal(bool)
5253
sig_path_changed = Signal(object, object, bool)
53-
sig_export_pythonpath = Signal(object, object, bool)
5454

5555
# This is required for our tests
5656
CONF_SECTION = 'pythonpath_manager'
@@ -332,6 +332,15 @@ def export_pythonpath(self):
332332
"""
333333
Export to PYTHONPATH environment variable
334334
Only apply to: current user.
335+
336+
If the user chooses to clear the contents of the system PYTHONPATH,
337+
then the active user paths are prepended to active system paths and
338+
the resulting list is saved to the system PYTHONPATH. Inactive system
339+
paths are discarded. If the user chooses not to clear the contents of
340+
the system PYTHONPATH, then the new system PYTHONPATH comprises the
341+
inactive system paths + active user paths + active system paths, and
342+
inactive system paths remain inactive. With either choice, inactive
343+
user paths are retained in the user paths and remain inactive.
335344
"""
336345
answer = QMessageBox.question(
337346
self,
@@ -349,9 +358,24 @@ def export_pythonpath(self):
349358
if answer == QMessageBox.Cancel:
350359
return
351360

352-
self.sig_export_pythonpath(
353-
self.get_user_paths(), self.get_system_paths(),
354-
answer == QMessageBox.Yes
361+
user_paths = self.get_user_paths()
362+
active_user_paths = OrderedDict({p: v for p, v in user_paths.items() if v})
363+
new_user_paths = OrderedDict({p: v for p, v in user_paths.items() if not v})
364+
365+
system_paths = self.get_system_paths()
366+
active_system_paths = OrderedDict({p: v for p, v in system_paths.items() if v})
367+
inactive_system_paths = OrderedDict({p: v for p, v in system_paths.items() if not v})
368+
369+
new_system_paths = active_user_paths | active_system_paths
370+
if answer == QMessageBox.No:
371+
new_system_paths = inactive_system_paths | new_system_paths
372+
373+
env = get_user_env()
374+
env['PYTHONPATH'] = list(new_system_paths.keys())
375+
set_user_env(env, parent=self)
376+
377+
self.update_paths(
378+
user_paths=new_user_paths, system_paths=new_system_paths
355379
)
356380

357381
def get_user_paths(self):
@@ -388,10 +412,10 @@ def get_system_paths(self):
388412

389413
def update_paths(
390414
self,
391-
project_paths=OrderedDict(),
392-
user_paths=OrderedDict(),
393-
system_paths=OrderedDict(),
394-
prioritize=False
415+
project_paths=None,
416+
user_paths=None,
417+
system_paths=None,
418+
prioritize=None
395419
):
396420
"""Update path attributes.
397421
@@ -400,10 +424,14 @@ def update_paths(
400424
used to compare with what is shown in the listwidget in order to detect
401425
changes.
402426
"""
403-
self.project_paths = project_paths
404-
self.user_paths = user_paths
405-
self.system_paths = system_paths
406-
self.prioritize = prioritize
427+
if project_paths is not None:
428+
self.project_paths = project_paths
429+
if user_paths is not None:
430+
self.user_paths = user_paths
431+
if system_paths is not None:
432+
self.system_paths = system_paths
433+
if prioritize is not None:
434+
self.prioritize = prioritize
407435

408436
self.setup()
409437

@@ -632,7 +660,8 @@ def test():
632660
dlg.update_paths(
633661
user_paths={p: True for p in sys.path[1:-2]},
634662
project_paths={p: True for p in sys.path[:1]},
635-
system_paths={p: True for p in sys.path[-2:]}
663+
system_paths={p: True for p in sys.path[-2:]},
664+
prioritize=False
636665
)
637666

638667
def callback(user_paths, system_paths, prioritize):

spyder/plugins/pythonpath/widgets/tests/test_pathmanager.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ def pathmanager(qtbot, request):
3232
widget.update_paths(
3333
user_paths=OrderedDict({p: True for p in user_paths}),
3434
project_paths=OrderedDict({p: True for p in project_paths}),
35-
system_paths=OrderedDict({p: True for p in system_paths})
35+
system_paths=OrderedDict({p: True for p in system_paths}),
36+
prioritize=False
3637
)
3738
widget.show()
3839
qtbot.addWidget(widget)

0 commit comments

Comments
 (0)