importing a class thru a variable?

john osborne6 at gmail.com
Tue Oct 21 19:07:14 EDT 2008


Hi,
This is probably a question of questionable sanity, due to the fact I
don't think I can explain this well. I'd like to have a script set up
such that it imports a class that is named in the command line
arguments as the first argument to the script.

Let's say I have a script, command.py, and I'd like to run it like
this:
command.py class_id_like_to_import --option1 value1 --option2 value2

where 'class_id_like_to_import' is a class name and --option1/value1
and so on are arguments that get passed to that class.

I know that trying to do something like:

  classname = sys.argv[1]
  import classname

doesn't exactly work. I've tried to google for something like this,
and search the old posts to this newsgroup and haven't quite found
anything resembling what I'm looking for.

Thanks in advance, and apologies if this has been answered before...



More information about the Python-list mailing list