0

I have a python script named script_A. I need a function present in another script named script_B located in the same directory as script_A.

When I execute script_A, everything goes well until the commands importing the function are executed: script_B is executed.

I just want to benefit from the use of the function in script_A and do not want script_B to be executed.

Here is my code :

from script_B import my_function
Basile
  • 575
  • 1
  • 6
  • 13
  • 1
    Does this answer your question? [Why is Python running my module when I import it, and how do I stop it?](https://stackoverflow.com/questions/6523791/why-is-python-running-my-module-when-i-import-it-and-how-do-i-stop-it) – Moshe perez Mar 17 '20 at 16:44

2 Answers2

1

Bro this happens internally see understand when you call a function from another module you just pass the necessary arguments so the function in that particular script has to run in order to provide you the output.

0

I found out that the answer relies on using the command:

if __name__ == '__main__' :
    my_function()

When importing the script, my_function() will not be executed and I will benefit the use of the functions defined in the script.

Basile
  • 575
  • 1
  • 6
  • 13