python modules as user configuration files

Matthew Dixon Cowles matt at mondoinfo.com
Thu Apr 5 16:17:14 EDT 2001


On Fri, 6 Apr 2001 07:36:22 +1200 (NZST), Graham Guttocksi
<graham_guttocks at yahoo.co.nz> wrote:

>Is there any way to explicitly import a particular file using its full
>path name?

Yex, there is and Fredrik Lundh is responsible for having pointed
out how to this bit of abuse. Since Deja's, um Google's, archive
doesn't go back that far right now, I'll reproduce it here:

# import module from string
# fredrik lundh, may 2000

def do_import(name, source):
  module = imp.new_module(name)
  sys.modules[name] = module
  exec source in vars(module)
  return module

It assumes that you have the text of your module in a string,
but that's not hard to arrange.

Regards,
Matt



More information about the Python-list mailing list