-2

I use to think both are equal until I tried this:

$python
Python 2.7.13 (default, Dec 17 2016, 23:03:43)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import Tkinter
>>> root=Tk()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'Tk' is not defined
>>> from Tkinter import *
>>> root=Tk()

So what's the core difference between these 2 kinds of import that import everything from a module?

Thanks.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
Troskyvs
  • 7,537
  • 7
  • 47
  • 115
  • 2
    Although "star" imports are often used in Tkinter example code this is **not** a good practice. It dumps all those Tkinter names into your name space (175 names in Python 2, 135 names in Python 3). This creates needless clutter, and the potential for name collisions, especially if you star-import another module. It also makes the code harder to read because you have to remember which names are Tkinter names, rather than being able to see it explicitly. The preferred practice is to do `import Tkinter as tk`, then do stuff like `root = tk.Tk(); button = tk.Button(root, ...)`. – PM 2Ring Apr 21 '17 at 08:50
  • Note that all forms of `import` _always_ import (and execute) the entire module, the difference lies in which names they make available in the importing module. – PM 2Ring Apr 21 '17 at 09:00

1 Answers1

1

when you import x , it binds the name x to x object , It doesn't give you the direct access to any object that is your module, If you want access any object you need to specify like this

x.myfunction()

On the other side when you import using from x import * , It brings all the functionalities into your module, so instead of x.myfunction() you can access it directly

myfunction ()

for example lets suppose we have module example.py

def myfunction ():
    print "foo"

Now we have the main script main.py , which make use of this module .

if you use simple import then you need to call myfunction() like this

import example

example.myfucntion()

if you use from, you dont need to use module name to refer function , you can call directly like this

from example import myfunction

myfunction()
Christian W.
  • 2,532
  • 1
  • 19
  • 31
pushpendra chauhan
  • 2,205
  • 3
  • 20
  • 29
  • 1
    You make it sound like using star imports in a normal user script is a good thing. It is _not_. Star imports are handy for quick experiments in the interactive shell, but they should **not** be used in normal scripts. There is a legitimate use for star import: it's useful for unifying the names from multiple modules in a package to make them behave as if they were all defined in one module, but it's rarely a good idea for user code to do that, that's the job of the package writer. – PM 2Ring Apr 21 '17 at 08:58
  • As stated in [How to Answer](http://stackoverflow.com/questions/how-to-answer), please avoid answering unclear, broad, SW rec, typo, opinion-based, unreproducible, or duplicate questions. Answering inappropriate questions harms the site by making it more difficult to navigate and encouraging further such questions, which can drive away other users who volunteer their time and expertise. – Arya McCarthy Apr 21 '17 at 09:19