25
25
from spyder .api .widgets .mixins import SpyderWidgetMixin
26
26
from spyder .config .base import _
27
27
from spyder .plugins .pythonpath .utils import check_path
28
+ from spyder .utils .environ import get_user_env , set_user_env
28
29
from spyder .utils .misc import getcwd_or_home
29
30
from spyder .utils .stylesheet import (
30
31
AppStyle ,
@@ -50,7 +51,6 @@ class PathManager(QDialog, SpyderWidgetMixin):
50
51
51
52
redirect_stdio = Signal (bool )
52
53
sig_path_changed = Signal (object , object , bool )
53
- sig_export_pythonpath = Signal (object , object , bool )
54
54
55
55
# This is required for our tests
56
56
CONF_SECTION = 'pythonpath_manager'
@@ -332,6 +332,15 @@ def export_pythonpath(self):
332
332
"""
333
333
Export to PYTHONPATH environment variable
334
334
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.
335
344
"""
336
345
answer = QMessageBox .question (
337
346
self ,
@@ -349,9 +358,24 @@ def export_pythonpath(self):
349
358
if answer == QMessageBox .Cancel :
350
359
return
351
360
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
355
379
)
356
380
357
381
def get_user_paths (self ):
@@ -388,10 +412,10 @@ def get_system_paths(self):
388
412
389
413
def update_paths (
390
414
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
395
419
):
396
420
"""Update path attributes.
397
421
@@ -400,10 +424,14 @@ def update_paths(
400
424
used to compare with what is shown in the listwidget in order to detect
401
425
changes.
402
426
"""
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
407
435
408
436
self .setup ()
409
437
@@ -632,7 +660,8 @@ def test():
632
660
dlg .update_paths (
633
661
user_paths = {p : True for p in sys .path [1 :- 2 ]},
634
662
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
636
665
)
637
666
638
667
def callback (user_paths , system_paths , prioritize ):
0 commit comments