Loading modules that change sys.path

Let's say that in Python I load a module that modifies sys.path . Will this bootloader sys.path ? If so, is there a way to make sure that I can recover it later?

What about the opposite scenario? Say the loader performs a change to sys.path before loading the module. Will the module see normal sys.path (i.e. PYTHONPATH , etc.), or will it see the new sys.path ?

+4
source share
1 answer

yes, this 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 can convert it to a tuple rather than a list ... depending on how they add to the path, which may work, but it may break some of your imported modules if they change paths

you can also access

 os.environ["PYTHONPATH"] 
+4
source

Source: https://habr.com/ru/post/1494178/


All Articles