Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
from module import function
#1
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
Reply
#2
It's all one module import is not required.
Reply
#3
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?
Reply
#4
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"
Reply
#5
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__ ;
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  is import cointegration_analysis a recognized module mitcht33 1 425 Nov-06-2023, 09:29 PM
Last Post: deanhystad
  problem in import module from other folder akbarza 5 1,389 Sep-01-2023, 07:48 AM
Last Post: Gribouillis
  can not import anaconda pandas module. PySpark pandas module is imported!! aupres 0 715 Aug-06-2023, 01:09 AM
Last Post: aupres
  import module error tantony 5 3,439 Dec-15-2022, 01:55 PM
Last Post: Lauraburmrs
  Import a module one step back of the path prathampatel9 1 1,070 Sep-21-2022, 01:34 PM
Last Post: snippsat
  How to import file and function in another folder SriRajesh 1 3,150 Dec-18-2021, 08:35 AM
Last Post: Gribouillis
  Import a module for use in type hint? Milosz 0 1,482 Nov-08-2021, 06:49 PM
Last Post: Milosz
  Can't install nor import delorean module Tek 3 2,797 Oct-27-2021, 03:32 AM
Last Post: Tek
  import module with syntax error Skaperen 7 5,269 Jun-22-2021, 10:38 AM
Last Post: Skaperen
  'urllib3' Module not found when import 'requests' spanz 5 10,185 Jan-06-2021, 05:57 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020