importing a class thru a variable?

Chris Rebert clp at rebertia.com
Tue Oct 21 19:19:57 EDT 2008


On Tue, Oct 21, 2008 at 4:07 PM, john <osborne6 at gmail.com> wrote:
> 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

Python import modules, not classes, but I believe you're looking for
the __import__() function.
See the first entry on http://www.python.org/doc/2.5.2/lib/built-in-funcs.html

Cheers,
Chris
-- 
Follow the path of the Iguana...
http://rebertia.com

>
> 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...
> --
> http://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list