1

Edit is done because my question was identified as a possible duplicate of another question asking difference between the two methods.


I'm new to python so was reading various opinions upon import and from import to cure my lack of knowledge. EDIT: I understood the difference it makes using either methods. To put it simply, import sth.sth_else method creates a reference to sth.sth_else, so we can access anything inside __init__().py of sth_else using sth.sth_else.func() or sth.sth_else.var. However, if from sth import sth_else is used, then we need not use sth with the [dot] opperator for scope resolution along with sth_else. This time we can simply use sth_else.func() and sth_else.var. I got to know all this from the most voted answer of a question on stackoverflow on the difference between the two import methods. But even using these two different methods I was able to find a way using as that the difference I got to know from the answer got insignificant/inappropriate. Meaning I was quite getting everything, until these two import methods started providing same invoking (which is different according to that answer), hence I was wondering if there is more to it than meets the eye..


import Method:

>>> import tkinter.ttk as my
>>> my.Button()
<tkinter.ttk.Button object .!button>
>>> 

from import Method:

>>> from tkinter import ttk as my
>>> my.Button()
<tkinter.ttk.Button object .!button>
>>> 

Now with the knowledge I have currently, I'd say these two importing methods are exactly the same in all aspects. But I'm not 100% sure of it being certain, so my question is: Are these two methods really exactly the same?

  • 3
    Does this answer your question? [Use 'import module' or 'from module import'?](https://stackoverflow.com/questions/710551/use-import-module-or-from-module-import) – Divyesh patel Nov 09 '19 at 05:16
  • Thanks for your suggestion buddy... but I already read this one before asking. I'll edit my question to describe as why this doesn't answer my question. ^_^ – RisingUnderDog Nov 09 '19 at 06:50

2 Answers2

4

No. You can not say that they are exactly the same.

The dot('.') used in 'import' is to travel through the packages. When importing using import xyz.abc, you can only import modules or packages i.e. 'abc' here needs to be a module or a package, while, when you use from xyz import abc, you can also import any functions, variable or class present inside that module 'xyz' i.e. 'abc' doesn't necessarily need to be a module, it can be a function, variable or a class declared inside this module.

For example, you can import sqrt function by using - from math import sqrt but you cannot import it by import math.sqrt.

The 'from import' method is to save python from compiling extra code and wasting time and resources. Hence it's used to easily import simple functions and subprograms created inside another python file(module) rather than importing the whole module.

Pulkit Jatav
  • 114
  • 5
  • 1
    Yes I just had a quick run on IDLE, you're right. So from now on I'll use import always for packages and modules (since it provides clarity, math.sqrt() is better imo) and for functions and variables I'll be using import from method. ^_^ – RisingUnderDog Nov 09 '19 at 08:38
1

They are the same. However, choose one of these methods that you like better and keep using that to be more consistent.