Python Forum
from module import function - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: from module import function (/thread-26121.html)



from module import function - krishanu21 - Apr-21-2020

def fcn_a():
    print('function A module name=' + __name__, ';')
    from test3 import fcn_b

def fcn_b():
    print('function B module name=' + __name__, ';')

fcn_a()
print('End-1', 'module name=' + __name__, ';')
print('Total End', 'module name=', __name__, ';')
print(" ")
fcn_b()
print('End of fn B module name=' + __name__, ';')
Can someone please explain how "from test3 import fcn_b" is importing, The output looks wired


RE: from module import function - Larz60+ - Apr-21-2020

It's all one module import is not required.


RE: from module import function - deanhystad - Apr-21-2020

Your codelooks weird. What is the name of this file? Is it test3.py?

You use "import" to import a module so you can use the resources in that module. Python comes with a bunch of standard libraries (math, intertools, types, pprint, datetime, io, sys, tkinter, …) If my graphical user interface program uses tkinter, the top of the fill will have a line like this:
import tkinter
Then I can use functions from that module like:
window = tkinter.Tk()  # Make top level window for program
You can write your own modules and import them in other modules as well. It looks like this program is trying to import fcn_b from module test3 (file test3.py) However, the following code immediately defines a new fcn_b, so what was the point of the import? It is also unusual to see an import statement that isn't at the top of the file.

What does test3.py look like?


RE: from module import function within the same module - krishanu21 - Apr-22-2020

Thank u for your reply, I do understand that 'import' command will import a module, but if someone can explain how importing a module inside the same module will work.
A detailed answer will help me to understand the subject.
Here the file name is "test3.py"


RE: from module import function - buran - Apr-22-2020

The reason why it looks weird is because when you do from some_module import some_name it doesn't import just that name, but the whole module.
so you start by calling func_a.
it prints function A module name=__main__. Note __main__ is the name when you execute it and not import it (i.e. top level script).
then it imports the module (i.e. goes from the start and then execute fcn_a again. Now the module name is test3 that is because you import in test3 and not execute it.
then you continue execution and module name in the print output is again __main__

Output:
function A module name=__main__ ;
then the output as result of the import in fcn_a. Note that it does not result in recursive import, the module is imported just once, unless you force to reload it.
Output:
function A module name=test3 ; End-1 module name=test3 ; Total End module name= test3 ; function B module name=test3 ; End of fn B module name=test3 ;
then the continued output from top level
Output:
End-1 module name=__main__ ; Total End module name= __main__ ; function B module name=__main__ ; End of fn B module name=__main__ ;