471,092 Members | 1,606 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,092 software developers and data experts.

import X vs from x import *

import Tkinter
from Tkinter import *

i have a program where if i comment out either of those import-
statements i get an error.

i thought they meant the same thing and from was supposed to be just
to imort just a specific function and the * imports everything in the
module.
but aparently the above statements have diffrent meaning and i cant
figure it out fromt he tutorials.
Jun 27 '08 #1
3 5894

<no**********@yahoo.sewrote in message
news:c8**********************************@m73g2000 hsh.googlegroups.com...
import Tkinter
from Tkinter import *

i have a program where if i comment out either of those import-
statements i get an error.

i thought they meant the same thing and from was supposed to be just
to imort just a specific function and the * imports everything in the
module.
but aparently the above statements have diffrent meaning and i cant
figure it out fromt he tutorials.
the first statement imports tkinter but everything inside of tkinter in
stored in the tkinter namespace, in other words
if tkinter has a class tk, then tk would be accessed as tkinter.tk. for
example
import tkinter
blah = tkinter.tk()
if you do from tkinter import *, then everything in tkinter is imported at
the top level, so it would be
from tkinter import *
blah = tk()


Jun 27 '08 #2

On May 22, 2008, at 1:44 PM, no**********@yahoo.se wrote:
import Tkinter
With this you call functions from Tkinter as

Tkinter.function()
from Tkinter import *
Here you just call

function()
>
i have a program where if i comment out either of those import-
statements i get an error.

i thought they meant the same thing and from was supposed to be just
to imort just a specific function and the * imports everything in the
module.
but aparently the above statements have diffrent meaning and i cant
figure it out fromt he tutorials.
The first creates the Tkinter namespace and all of its functions remain
in that namespace. The latter all functions are imported into the local
namespace.

Cheers
Tommy (actually norwegian)
Jun 27 '08 #3
On Thu, 2008-05-22 at 10:44 -0700, no**********@yahoo.se wrote:
import Tkinter
from Tkinter import *

i have a program where if i comment out either of those import-
statements i get an error.

i thought they meant the same thing and from was supposed to be just
to imort just a specific function and the * imports everything in the
module.
but aparently the above statements have diffrent meaning and i cant
figure it out fromt he tutorials.
--
http://mail.python.org/mailman/listinfo/python-list
Others have explained what one statement does as opposed to the other.

In general, you do not want to use from x import *. Your namespace gets
polluted with all sorts of variables, and you don't know if you're going
to overwrite something important. It's much better to specify which
names you want to import (for the sake of those reading your code later,
and for the sake of future-proofing your code against new variables
finding their way into the module being imported from), or just to
import the package (and optionally assign it to a shorter alias)

import Tkinter as tk

tk.function()

That said, there are occasional packages where it is common practice to
import *. Tkinter might be one of those. I don't have any experience
with it. But don't make a habit of it. :)

Cheers,
Cliff
Jun 27 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Stian Søiland | last post: by
reply views Thread by Vio | last post: by
reply views Thread by John Roth | last post: by
5 posts views Thread by Steve Holden | last post: by
4 posts views Thread by Bruce W. Roeser | last post: by
7 posts views Thread by Ron Adam | last post: by
5 posts views Thread by W. Watson | last post: by
9 posts views Thread by rsoh.woodhouse | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.