Thread: import vs. from

    #1
  1. onCsdfeu
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Canada
    Posts
    100
    Rep Power
    12

    import vs. from


    I was reading a Tkinter tutorial and I've noticed they use
    Code:
    from Tkinter import *
    instead of the usual
    Code:
    import Tkinter
    Is there any difference, or is it equivalent ?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    133
    Rep Power
    12
    When you do 'import Tkinter' you will access all the data objects through the Tkinter namespace. When you do a 'from Tkinter import *', you import all non-private data objects into your current namespace. Normally you shouldn't use 'from <something> import *', because it pollutes the namespace; but practicality beats purity...
  4. #3
  5. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,638
    Rep Power
    4247
    I haven't hacked in python for a while, but IIRC, the difference between this statement:
    from somemodule import *
    and this statement:
    import somemodule

    is that the first one imports all the functions implicitly within the namespace, whereas with the second one only public functions/objects are imported, so you may still have to qualify functions and objects with the module name, when calling them. For example:
    Code:
    #!/usr/bin/python
    from sys import *
    
    print "Hello world"
    exit(1)
    and
    Code:
    #!/usr/bin/python
    import sys
    
    print "Hello world"
    sys.exit(1)
    Notice that in the first case, I don't have to qualify exit(), but in the second case, I do.
    Last edited by Scorpions4ever; July 21st, 2003 at 01:39 PM.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  6. #4
  7. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    where they both do roughly the same thing (program wise) its really just a personal choice.

    from module import functions - imports the module and allows access to its functions/classes without the use of the module name prefix.

    import module - import the module for use. My personal favourate simply because its easier to read.

    Hope this helps,
    Mark.
  8. #5
  9. Wacky hack
    Devshed Novice (500 - 999 posts)

    Join Date
    Apr 2001
    Location
    London, England
    Posts
    513
    Rep Power
    14
    Just thought I'd add my own opinion here

    I'd always go with "import module", since it makes reading the code easier (because you always know that "sys.exit()" is a sys method, not a builtin or one of your own), and because it means you don't accidentally overwrite your own functions/methods when importing lots of modules.

    Keeps namespace nice and tidy

    And if you do use "from module import *", try to explictly import particular functions/methods instead, since it will take up less memory and again be safer/nicer (i.e. "from module import x, y, z").
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    133
    Rep Power
    12
    Personally, I would do:
    Code:
    import Tkinter as tk
    to make the the code a little less excessively expressive.

IMN logo majestic logo threadwatch logo seochat tools logo