Ways of Importing a module in python | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Ways of Importing a module in python

Recently I was going through the tkinter tutorial, I was learning from 2 resources and found some difference, one is telling to use, from tkinter import * Another Import tkinter I was told both doesn't have any difference but I had noticed that the first one doesn't require to tkinter. Before every command but also some resources told me that the first type importing will import all but will make program lag or crash whereas second one won't but program's size will be increased, I got confused, can anyone tell me the major differences, which one to use and which one is used popularly and why?

28th Jun 2020, 5:37 AM
Mouhurtik Ray
Mouhurtik Ray - avatar
5 Answers
+ 7
Every code has a dictionary 'globals' , where all the names you defined can be found. If you write x = 42, then globals has a key 'x' with the value 42 in it. When you write 'import tkinter', only one more name is put to globals - tkinter. That has the advantage, that the globals dict only gets one small change, and the disadvantage that you have to write tkinter.whatever each time. The method 'from tkinter import *' puts all names of the module one by one in your globals menu. The advantage is that you don't have to write tkinter. in front of everything, but the disadvantage is that many new variables appear in your globals, and if you had defined something with the same name before the import, it would be overwritten.
28th Jun 2020, 7:37 AM
HonFu
HonFu - avatar
+ 3
Hm, it shouldn't be that much of a difference. No matter how you import, it means that the module is executed and run from top to bottom. The only difference is, how many names you have available as your globals after that. So it should take up a bit more time assigning all these extra names, but well, that happens once in the beginning. Since modules usually are not too huge (or shouldn't be), it won't take too long. The app shouldn't become bigger any way. Maybe more advanced people will add more info.
28th Jun 2020, 8:36 AM
HonFu
HonFu - avatar
+ 3
Basically, you just have to watch out if you already have something in your code that has the same name. Using the packaged import, you only need to watch out for one name. Using *, you need to check for *all* the names from the module. I think there's 100+ in tkinter. If names collide anyway, you can pick an alias anytime: m = 42 import m as secondM
28th Jun 2020, 10:15 AM
HonFu
HonFu - avatar
+ 2
HonFu will both of them have any lag in perfomance or have increased app size? Or that's the only advantage and disadvantage?
28th Jun 2020, 8:12 AM
Mouhurtik Ray
Mouhurtik Ray - avatar
+ 1
Yeah, the frowning stems from the confusion and strange bugs a * import can cause. If you define a function f, and then import everything from module m, and m also has a function f, then your definition will be overwritten. Also, if you define your f after the import, the f from m will be overwritten. By importing the whole module m, you keep all the names in the box (namespace) m, which means you can have f AND m.f - minus the confusion. Well, unless you had defined your own variable m before and then import m - then your m would be overwritten. 😉
28th Jun 2020, 9:59 AM
HonFu
HonFu - avatar