Dynamically importing modules

Steven D. Majewski sdm7g at Virginia.EDU
Wed Jun 20 20:50:33 EDT 2001



> > From: Jared Lee Peterson [mailto:jared at difinium.com]
> > 
> > I have a quick question about importing modules.  I would 
> > like to have a
> > script that in sitting in a directory and in this directory I want to
> > have sub directories that represent modules to that script.  However
> > I do not want this script to have any prior knowledge of what modules
> > may exist.  So I would like for the script to look at it 
> > current working


  You don't want the current working directory -- unless you've changed
 it with os.chdir, that is just the cwd for the user's shell when they
 ran python. You want the path of the module you're importing. 
  Consider making it a package so that you can use the __path__. 
   [ See: <http://www.python.org/doc/essays/packages.html> ]
  That would mean that you couldn't run it as a __main__ module 
 from the command line, but you could make another module that 
 imports the package and serves as a main module. 

> > directory and then try to load each module that does exist through the
> > use of import.  But I can not seem to figure out how to get import to
> > work for me.  Because I need to be able to loop through a list
> > (os.listdir()) of the subdirectories/modules that the script has found
> > and try to import them.  Does anyone have any suggestions?  Does this
> > even make sense?

On Wed, 20 Jun 2001, Bjorn Pettersen wrote:

> 
> you can use the __import__ function to import a module based on it's
> name (as a string), ie. the following two statements are equivalent:
> 
>   mymodule = __import__('mymodule')
> 
> and
> 
>   import mymodule

 If you don't know the value of  mymodule (i.e. it's a variable and
 not a literal) you might have to do something like:

	globals()[mymodule] = __import__( mymodule ) 


 If you use __import__, that path has to be in sys.path ( and also the
 submodule directories. ). You can also use the routines in imp or ihooks
 if you want to customize things on a lower level. ( For example, you 
 can give an explicit path to imp.load_module if you don't want to 
 mess with sys.path. )  [ read up on those modules in the Library 
 reference manual. ] 


-- Steve 

 If you use packages, you might do something like this for the 
 package  __init__ file:

 (Sorry about going overboard with the list comprehension -- I'm 
  trying to use them every chance I get until they seem natural!) 



## __init__.py ##

"Sample __init__ module for dynamic loading of package modules"

import os,sys

sys.path.extend( __path__ )

__modules__ = {}  # keep a list of all the modules

for _module in [ _file[:-3]  for _file in os.listdir( __path__[0] ) if _file[-3:] == '.py' and _file[0] <> '.' and _file[:2] <> '__' ]:
	__modules__[_module] = globals()[_module] = __import__( _module )


for _file in __path__:  sys.path.remove( _file )

del os,sys,_module,_file









More information about the Python-list mailing list