[Tutor] temporarily modifying sys.path

Tim Johnson tim at johnsons-web.com
Sun Nov 28 17:33:32 CET 2010


I need a function that will import a module (using __import__) from
only one specific location on my filesystem. Name collisions are
possible. To avoid this I could *temporarily* modify sys.path for
the operation so that it contains only the path that I want
to work from.

console example:
>>> sys_path = sys.path
>>> sys.path = ["/cgi-bin/libraries/python"]
>>> sys.path
['/cgi-bin/libraries/python']
>>> sys.path = sys_path
>>> sys.path
## original sys.path

I don't want to create something that could bite me later.
Documentation on sys.path that I have found does not address
*removing* items from sys.path or completely changing it.

Any caveats or comments on this procedure? 
TIA
-- 
Tim 
tim at johnsons-web.com or akwebsoft.com
http://www.akwebsoft.com


More information about the Tutor mailing list