Python: Whats the difference between import X and from X import *? [duplicate]

2024/11/20 4:43:44

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.

Answer

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 exampleexample.myfucntion()

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

from example import myfunctionmyfunction()
https://en.xdnf.cn/q/119870.html

Related Q&A

Python iterate through pixels of image

Im trying to iterate through pixels of an image. I set the size and then use a for loop, however I get a type error: object not iterable. I have imported PIL and Imagew=100 h=200 im=im.resize((w,h), Im…

Geocoding with Geopy and big data

I have this CSV file which im feeding with this python scriptimport csv from geopy.geocoders import OpenCagegeolocator = OpenCage() #here some parameters are needed with open(/Users/Ian/Desktop/Test02/…

an error in sending json data to flask server [duplicate]

This question already has answers here:How to get POSTed JSON in Flask?(13 answers)Closed 1 year ago.I have a json data as {"age":59.0,"bp":70.0,"sg":1.01,"al":…

terminate a python program when it hanged using subprocess python

I have a main.py which open a new cmd (subprocess) when another program (test.py, in same directory) is hanged. To determining test.py is hanged or not, I used latest modified time (os.path.getmtime(te…

causes of Python IOError: [Errno 13] Permission denied

When attempting to write a file, I can get this same error when any of following conditions applies:The file exists and is marked read-only. I dont have write permission for the folder and therefore ca…

Python Invalid Syntax IF statement

Im trying to make a quiz in python but I keep getting invalid syntax errors.#This is for addition questions.if (question=add) <---- That is where i get the error for i in range(0,10):first_number_a…

record a web page using python [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 3…

returning a string from askopenfilename() to a entry box

I have seen many postings on the use of askopenfilename(), however I still cant seem to find anything to help me display the full file path in an entry box once I have selected said file. below I have…

Overflow error in Python program

Please help me to understand why this code doesnt work. I know there is something very stupid wrong. This should be an implementation of the fourth order Runge kutta algorithm to solve Lorentz system o…

python login 163 mail server

When I use this script to login the 163 mail server,there is something wrong! My python env is python 2.7.8 Please help me!import imaplibdef open_connect(verbose=False):host = imap.163.comport = 993if …