Best way to handle changing list of class imports

greg greg at
Mon Oct 12 09:20:31 CEST 2009

Dave Angel wrote:

> For that, I'd suggest reserving a directory at a known location, doing 
> an os.path.dirname() on that directory, and building a list of module 
> names.  Then use __import__() to load them, and build a list of module 
> objects, and a list of classes in those modules.  Suggest classname to 
> be simply formed by uppercasing the first letter of the module file.

Another possibility would be to require each student's module
to define a factory function with a standard name, such as
'create_strategy', so you wouldn't have to deal with a different
class name in each module.

Also, an alternative to using __import__() would be to use
execfile() to load and execute the student's code. Then you
wouldn't have to put the directory containing the students'
modules on the import path, and they wouldn't get put into
sys.modules, which would help to isolate them from each other.
(Otherwise a student could cheat by importing someone else's
stragegy class and passing it off as their own!)

However, each student's submission would more or less be
restricted to a single file in that case, whereas using
__import__() would allow it to be a package with submodules.
Whether that's a serious disadvantage depends on how big and
complicated you expect the submissions to be.


More information about the Python-list mailing list