[Tutor] temporarily modifying sys.path

Evert Rol evert.rol at gmail.com
Sun Nov 28 17:53:33 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.

Just curious, but could the imp module help you? imp.find_module appears to look on a specific path if you specify it, and it seems you have the path available here. From there imp.load_module perhaps.
I have very little experience with imp, but I'm wondering if it could help you without the necessity of modifying sys.path.

Though I do think you can temporarily make a copy of sys.path, then modify sys.path, then after the import reassign sys.path to your temp variable. But try the above route first.

  Evert


> 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
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list