4

Say that in Python I load a module that changes the sys.path. Would that change the loader's sys.path as well? If it does, is there any way to make sure that I can restore it later?

And what about the opposite scenario? Say the loader makes a change to sys.path before loading the module. Would the module see the regular sys.path (i.e. PYTHONPATH, etc.), or would it see the new sys.path?

Amelio Vazquez-Reina
  • 91,494
  • 132
  • 359
  • 564

1 Answers1

4

yes it will affect sys.path anywhere.... you can save it and restore it later

import sys
_opath = sys.path[:] #get a copy of whatever sys.path is at this point

#do imports

sys.path = _opath

you might be able to convert it to a tuple instead of a list ... depending on how they are adding to the path that may work but it might break some of your imported modules if they are changing paths

you can also access

os.environ["PYTHONPATH"]
Joran Beasley
  • 110,522
  • 12
  • 160
  • 179