Need clue with import hackery

Richard Jones richard at bizarsoftware.com.au
Thu Aug 2 20:08:56 EDT 2001


In the roundup project, I (possibly foolishly) decided to make the roundup 
instances python packages. A roundup instance consists of a database store, 
interface specifications (cgi, mail, ...) and other configuration 
information. This is quite separate from the roundup library code, which is 
just a package called "roundup" in site-packages and does most of the actual 
work. This allows a great deal of flexibility in customising the individual 
roundup instances (adding/removing CGI fnuctionality, altering the database 
schema, ...)

The instances therefore consist of:

/home/roundup/issue_tracker/
   __init__.py
   instance_config.py
   select_db.py
   dbinit.py
   interfaces.py
   db/
   html/
   detectors/

The roundup library code, when asked to do something, is passed the 
instance_home "/home/roundup/issue_tracker". It does:

   path, instance = os.path.split(instance_home)
   sys.path.insert(0, path)
   try:
       instance = __import__(instance)
   finally:
       del sys.path[0]

Of course, the fatal flaw with all this is that if "instance" (the string) is 
the same name as an existing imported module, we're stuffed. An early adopter 
tried "roundup" with obvious consequences ;)

So what I need to know is if there is some way to import the instance_home 
package as "instance" rather than as "roundup".

Any advice appreciated...


    Richard




More information about the Python-list mailing list