[Tutor] import * not allowed at function level - wassup?

Alan Gauld alan.gauld at btinternet.com
Thu Jun 6 14:56:59 CEST 2013


On 06/06/13 10:30, Jim Mooney wrote:
> In the program below, which now works, I tried to use "from
> tkinter.constants import *" to throw up an error message with tkinter,
> and got the error "import * only allowed at module level." So I
> instead imported tkinter.constants, but then I had to prepend every
> constant with tkinter.constants, which is annoying. Is there a way
> around that prohibition (and what's it for, anyway?)

Not the prohibition - although in general imports are done at module 
level. You don't really want to try to import every time you call a 
function....

But you can rename an import:

import foo as f

or you can import explicitly

from tkinter.constants import foo,bar,baz...

importing * is generally a bad idea in real code.

hth
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list