[Solved] Python: What’s the difference between “import X” and “from X import *”? [duplicate]


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()

2

solved Python: What’s the difference between “import X” and “from X import *”? [duplicate]