By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,908 Members | 1,945 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,908 IT Pros & Developers. It's quick & easy.

import X vs from x import *

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a

<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

P: n/a

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

P: n/a
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.