how to import a module from a arbitraty path?

Peter Hansen peter at engcorp.com
Thu May 26 07:58:16 EDT 2005


Simon Brunning wrote:
> On 5/26/05, Andy Leszczynski
> <leszczynscyATnospam.yahoo.com.nospam at bag.python.org> wrote:
> 
>>I have a program which is going to dynamicly load components from some
>>arbitrary defined paths. How to do that?
> 
> 
> You can locate them with os.walk and fnmatch. Then you can temporarily
> add the directory to sys,path, and import using __import__().

And if you do that, note the difference between adding your temporary 
directories at the start of sys.path and at the end of it...  (this is 
critical to understand if any of the dynamically loaded module names 
might collide with names of any other packages in your sys.path, 
including standard module names).

-Peter



More information about the Python-list mailing list