473,287 Members | 3,295 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 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 6070

<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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Stian Søiland | last post by:
all examples performed with: Python 2.3+ (#2, Aug 10 2003, 11:09:33) on linux2 (2, 3, 0, 'final', 1) This is a recursive import:
0
by: Vio | last post by:
Hi, I've been trying to embed (statically) wxPy alongside an embedded py interpreter on a linux/gtk box. At one point, for some reason misc.o linking reported "multiple definitions of...
0
by: John Roth | last post by:
I've found a case where it seems that Python is importing two copies of a module without any reason or indication. It took me a while to verify that this is what is occuring: I had to write a...
5
by: Steve Holden | last post by:
This is even stranger: it makes it if I import the module a second time: import dbimp as dbimp import sys if __name__ == "__main__": dbimp.install() #k = sys.modules.keys() #k.sort() #for...
1
by: mark | last post by:
In Access 2000 and 2002, I have created an import specification to import the fixed-width recordset below into an existing table. I am having strange problems with the import of the date and time...
4
by: Bruce W. Roeser | last post by:
All, I'm reading a book by Charles Petzold (Programming VS.Net). Pretty good content but am confused about the difference. From the text: ...
2
by: Jon | last post by:
It appears that (windows) python searches in the current working directory before looking in the local site-packages directory, or that '.' comes first in sys.path? The problem arises when I made...
7
by: Ron Adam | last post by:
from __future__ import absolute_import Is there a way to check if this is working? I get the same results with or without it. Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) on win 32 ...
5
by: W. Watson | last post by:
Is there a single source that explains these statements? ------------------------------ from Tkinter import * from Numeric import * import Image import ImageChops import ImageTk import time...
9
by: rsoh.woodhouse | last post by:
Hi, I'm trying to work out some strange (to me) behaviour that I see when running a python script in two different ways (I've inherited some code that needs to be maintained and integrated with...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.