@@ -468,7 +468,6 @@ def run_code(code, code_path, ns=None, function_name=None):
468
468
# it can get at its data files, if any. Add its path to sys.path
469
469
# so it can import any helper modules sitting beside it.
470
470
pwd = os .getcwd ()
471
- old_sys_path = sys .path .copy ()
472
471
if setup .config .plot_working_directory is not None :
473
472
try :
474
473
os .chdir (setup .config .plot_working_directory )
@@ -480,13 +479,12 @@ def run_code(code, code_path, ns=None, function_name=None):
480
479
raise TypeError (str (err ) + '\n `plot_working_directory` option in '
481
480
'Sphinx configuration file must be a string or '
482
481
'None' )
483
- sys .path .insert (0 , setup .config .plot_working_directory )
484
482
elif code_path is not None :
485
483
dirname = os .path .abspath (os .path .dirname (code_path ))
486
484
os .chdir (dirname )
487
- sys .path .insert (0 , dirname )
488
485
489
- with cbook ._setattr_cm (sys , argv = [code_path ]), \
486
+ with cbook ._setattr_cm (
487
+ sys , argv = [code_path ], path = [os .getcwd (), * sys .path ]), \
490
488
contextlib .redirect_stdout (StringIO ()):
491
489
try :
492
490
code = unescape_doctest (code )
@@ -507,7 +505,6 @@ def run_code(code, code_path, ns=None, function_name=None):
507
505
raise PlotError (traceback .format_exc ())
508
506
finally :
509
507
os .chdir (pwd )
510
- sys .path [:] = old_sys_path
511
508
return ns
512
509
513
510
0 commit comments