[Tutor] Tkinter: no module named messagebox (brandon w)

Robert Sjoblom robert.sjoblom at gmail.com
Sun Aug 14 09:10:22 CEST 2011


> I have tried to follow the tutorial I found here:
>
> Python 2.7 Tutorial
> http://www.youtube.com/watch?v=uh6AdDX7K7U
>
> This is what I have done so far:
>
> #!/usr/bin/python
>
> from Tkinter import *
> import Tkinter.MessageBox

I figured I might as well, given how I recently had to learn about
this, give you a heads up on imports. The format "from module import *
" is not a very good idea: it makes your code harder to read (as the
reader has to inspect each and every barename to check whether it's
actually assigned locally OR comes from the deuced *). To quote the
Zen of Python; "namespaces are a honking great idea -- let's do more
of those!".

You can import modules with several different syntaxes:
import importable
import importable1, importable2, ..., importableN
import importable as preferred_name

Here importable is usually a module such as collections, but could be
a package or module in a package, in which case each part is separated
with a dot, for example os.path. The first two syntaxes are the
simplest  and also the safest because they avoid the possibility of
having name conflicts, since they force us to always use fully
qualified names.

The third syntax allows us to give a name of our choice to the package
or module we are importing. Theoretically, this could lead to name
clashes, but in practice the "import importable as" syntax is used to
avoid them.

There are some other import syntaxes:
from importable import object as preferred_name
from importable import object1, object2, ..., objectN
from importable import (object1, object2, ..., objectN)
from importable import *

In the last syntax, the * means "import everything that is not
private", which in practical terms means either that every object in
the module is imported except for those whose names begin with a
leading underscore, or, if the module has a global __all__ variable
that holds a list of names, that all the objects in the __all__
variable are imported.

The from importable import * syntax imports all the objects from the
module (or all the modules from the package) -- this could be hundreds
of names. In the case of from os.path import *, almost 40 names are
imported, including "dirname", "exists", and "split", any of which
might be names we would prefer to use for our own variables or
functions.

For example, if we write
from os.path import dirname
we can conveniently call dirname() without qualification. But if
further on in our code we write
dirname = "."
the object reference dirname will now be bound to the string "."
instead of to the dirname() function, so if we try calling dirname()
we will get a TypeError exception because dirname now refers to a
string, and we can't call strings.

However, given that Tkinter is such a huge package to begin with, I'd
say that you should continue to use from Tkinter import *, but be
aware of what you're doing when you type that, and that there is a
certain risk of conflicts.

best regards,
Robert S.


More information about the Tutor mailing list