ARGV and import ?

Ype Kingma ykingma at accessforall.nl
Tue Oct 9 16:00:28 EDT 2001


Steve Holden wrote:
> 
> "J" <geek_girl at mailcity.com> wrote ...
> > Hi all,
> >
> > This is my first post to this group & I am also fairly new to python,
> > so please forgive me if this isn't the brightest question or the
> > appropriate venue.
> >
> > I would like to do the following:
> > import a module based on what is typed on the command line.
> >
> > I thought this would work:
> > import sys
> > recipes = sys.argv[1]
> > import recipes
> >
> > But, I get this error:
> > >> ImportError: No module named recipes
> >
> > Is there any way to pass the module name to import through an argv?
> >
[snip]
> 
> Try
> 
>     __import__(sys.argv[1])
> 
> but also be aware that's a potentially dangerous program in someone else's
> hands ... they can import any module they like, and the module is EXECUTED
> the first time it's imported.

One can do:

recipes = __import__(sys.argv[1])

to have recipes refer to a module.

Have fun,
Ype

-- 
email at xs4all.nl



More information about the Python-list mailing list