user modules
Juho Schultz
juho.schultz at pp.inet.fi
Thu Oct 5 15:23:47 EDT 2006
Juho Schultz wrote:
> Cameron Walsh wrote:
> > Hi,
> >
> > I'm writing a python program to analyse and export volumetric data. To
> > make development and extension easier, and to make it more useful to the
> > public when it is released (LGPL), I would like to enable users to place
> > their own python files in a "user_extensions" directory. These files
> > would implement a common interface in order for the main program to be
> > able to read them and execute the necessary code.
> >
> > My question is what is the best way of implementing this?
> >
> > I have investigated importing them as modules, but unless the user
> > modifies the main program I cannot see how the main program can learn of
> > the existence of specific modules.
> >
>
> One simple solution would be a shell script that adds user_extensions
> (or whatever) to $PYTHONPATH and then starts your main program.
Sorry... I was typing faster than reading or thinking.
You could have a __init__.py file within user_extensions with
__all__ = ["package1", "package2"]
If you want every python file within some directory in here, you can
auto-generate the __init__.py file in user_extension before importing.
(Or you could have some sort of tester for new .py files detected and
only after you are sure it works, add it.)
from user_extensions import *
would import everything mentioned in __all__. You also have access to
their names through
user_extensions.__all__
The last step would be to put the modules into a list. After the
import,
user_ext_list = [eval(elem) for elem in user_extensions.__all__ ]
for ext in user_ext_list:
ext.initialize()
ext.get_tab_window()
More information about the Python-list
mailing list