[Tutor] Code query..works in IDLE but not DrPython editor

Alan Gauld alan.gauld at btinternet.com
Tue Jul 10 21:55:17 CEST 2007


"Tony Noyeaux" <tnoyeaux at msn.com> wrote

> import randomprint random.choice(['Apple', 'Pear', 'Shrimp', 
> 'Death', 'Life'])

Your formatting is a little strange as seen above but...

> Works in IDLE gui, no problem.

Do you mean the interactive shell in IDLE?
Or are you actually comparing like for like?
In other words are you running a file in IDLE?

> Gives me this error.
> ...snip ...
> File "C:/Python25/random.py", line 1, in <module>    import random

This says that you are importing random from a file called random.py

I suspect this means that you called your own file the same as
the module and when Python tries to import random it sees
your file first - and imports itself.

> AttributeError: 'module' object has no attribute 'choice'

And because your file doesn't define a choice function it gives an 
error.

Try renaming your file to myrandom or somesuch.

Guessing, but it seems a fair guess.

Alan G. 




More information about the Tutor mailing list