how to import a module from a arbitraty path?

A

Andy Leszczynski

I have a program which is going to dynamicly load components from some
arbitrary defined paths. How to do that?

A.
 
S

Simon Brunning

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__().
 
P

Peter Hansen

Simon said:
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
 
M

Maksim Kasimov

Andy said:
I have a program which is going to dynamicly load components from some
arbitrary defined paths. How to do that?

A.
import sys
sys.path.append('/yourpath/libs')
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,240
Messages
2,571,205
Members
47,843
Latest member
eicamotu

Latest Threads

Top