from (some module) import *
Michael Hudson
mwh21 at cam.ac.uk
Wed Oct 27 13:06:00 EDT 1999
Ed <elb at cfdrc.com> writes:
> I'm new to Python, I hope I'm not trying to do the wrong thing here, but
> pls correct me if I am:
>
> I want to prompt for a module name, then import it. How do I make the
> module name in the "from" or "import" operators a variable? I was
> trying
>
> print "enter the file name"
> filename = raw_input()
> from filename import *
well, you can do this:
module = __import__(filename)
to mimic the effect of `import filename'.
I'm struggling to think of why you might want to do `import *' in this
situation.
You could do this:
exec "from %s import *"%raw_input()
but you'd need to watch for people typing in things like
"sys import exit; import os; os.system('rm -rf ~'); from sys"
depending on trust arrangements.
> This "module" I'm hoping to import contains only a list, Data = [...],
> and I have lots of such modules to process - each with different values
> in the Data list - and it changes all the time. I thought I could
> change my script to prompt for the file/module name rather than edit the
> script each time I wanted to process a different file/module.
Ahh, then do
module = __import__(raw_input())
and manipulate module.Data.
HTH,
Michael
More information about the Python-list
mailing list