Am 24.11.13 04:41, schrieb Chris Angelico:
As part of a post on python-ideas, I wanted to knock together a quick
little script that "imports" a file based on its name, in the same way
that the Python interpreter will happily take an absolute pathname for
the main script.

Is it imp.load_source() that you are looking for?

I'm using a similar thing for a plugin system, the plugins are stored with a fixed filename in a folder structure. The code looks like this(stripped down):

import imp

# folder is the path to the file, under which pluginmain.py
# contains the code

namespace = 'plugin{0}'.format(plugincount)
plugincount = plugincount + 1

# try to import the file and exec the init function
# if anything fails, set success=False, errorpos and errorinfo

# add path to the plugin into import path
oldsyspath=sys.path[:]
sys.path.insert(0, folder)

# load module
module = imp.load_source(namespace,os.path.join(folder,'pluginmain.py'))

Modifying sys.path is only necessary because the file could further import modules from the same path.

        Christian
--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to