473,830 Members | 2,022 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 6122

<no**********@y ahoo.sewrote in message
news:c8******** *************** ***********@m73 g2000hsh.google groups.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**********@ya hoo.se wrote:
import Tkinter
With this you call functions from Tkinter as

Tkinter.functio n()
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**********@ya hoo.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
6916
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
2588
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 wxGetFreeMemory()". Since wxGetFreeMemory() seemed effectively unused in gtk, I just commented it out in misc_wrap.cpp (didn't want to patch SWIG to regenerate the wrapper code, so hacked the generated cpp source), and replaced...
0
1998
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 __import__ hook to trace the actual activity. The source code for the hook is below. There are several examples of the problem in the trace; this is one particularly juicy one. -----------Part 1 ---------------- '0059' Import 'TypeAdapter'...
5
2488
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 kk in k:
1
8185
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 fields. 177 102003 16:43:12 102003 18:43:12 6OAG0ADP Y 0000 0000 0000 0000 61930 4HGA800 165 102003 17:43:12 102003 18:44:12 6OAG0ADP Y 0000 0000 0000 0000 61930 4HGA800 177 102003 16:41:18 102003 18:45:12 6OAG0ADP Y 0000 0000 0000 0000 61930...
4
6114
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: ---------------------------------------------------------------------------------------------------------------------------------------------------------- The @ Import Directive Next to @ Page, the directive that ASP.NET programmers use the most is @ Import. The @ Import directive is ASP.NET's...
2
2583
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 the mistake of running a test program from the same directory where I built and installed my package. Python uses the package from the current directory, which misses the pyd files, instead of the site-packages package which has them installed. ...
7
20582
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 _Ron
5
1746
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 import binascii import tkMessageBox
9
2128
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 another lump of code). The sample script is: # Sample script, simply create a new thread and run a # regular expression match in it. import re
0
9642
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10774
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10491
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10526
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10206
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6951
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5617
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4411
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3959
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.