I'm experimenting with something new to me; calling one python script from another. The end objective is to have several small 'calling scripts' make a call to the same single big script passing it variables, rather than maintaining several individual 'big scripts'. I'm running into a snag with the 'big script when I have def main(): in it.
Basic premise:
"""
script_ 1
playing the role of little calling script
"""
var1 = 'A'
var2 = 'B'
var3 = 'C'
import Script_2
"""
script_2
playing the role of the single big script
"""
from __main__ import *
print(var1)
print(var2)
print(var3)
This initial approach works just fine: all the variables get passed and then printed But if I change script_2 to wrapping it in a def main(): nothing happens. No errors, but more importantly, no prints:
from __main__ import *
def main():
print(var1)
print(var2)
print(var3)
if __name__== "__main__":
main()
It's as if the lines 8 & 9 are overruled by line 1?
Is there a way to use a def main(): wrapper in a script being called by another?
One solution I just found here is:
from __main__ import *
def main1():
print(var1)
print(var2)
print(var3)
if __name__== "__main__":
main1()
This works for me in my simple mini sample script.
# script_1.py
from helper import func1, func2, func3
val1 = func1()
val2 = func2()
val3 = func3(5)
print("stuff")
# end of script
----------------------------------------------
# helper.py
def func1():
"""Do amazing things"""
return 5
def func2():
"""Do more amazing things"""
return 2
def func3(val):
"""Do amazing things"""
return val + 5
Two scripts, the main script is called script_1.py.
line 2 import from helper.py (lines 13 onward)
you can call functions that do stuff or not.
func1 and func2 just return stuff, although they could do amazing things inside like return date time information, check for where it was called etc.
Now on line 6, we called func3 sending it a value (5), func3 (line 22), gets this value and does some math.
Examine my arraytools stuff on github and you will see that modularization is commonplace and most of the scripts call the functions of other scripts... you then don't need to repeat code, but organize functionality by function type
I think the fog is lifting for me with respect to __name__ as I've been able to come up with a solution to this that I like. Consequently when calling a script from another I have two options: Option 1 is to not use:
if __name__== "__main__":
main()
at the end of the script. But if I really want to use it, I make a slight adjustment to Script_2.py:
"""
script_2
playing the role of the single big script
"""
from __main__ import *
def main()
print(var1)
print(var2)
print(var3)
if __name__ = 'Script_2':
main()
Line 13 is the crucible. As I understand it, when a script is executed as itself,
__name__ = '__main_'
But since I'm calling Script_2.py from another script, that goes out the window because now:
__name__ = 'Script_2'
Remember, print() is always your friend: I put one in both Script_1 ad Script_2 in the appropriate places and things came together.....
Joe, the example I give is for when you have functionality that you want to use often and not repeat it. You put it into a separate script and import the functions from the script as you need them. In fact your main script need not contain any 'def' s at all, you simply import them from other scripts, use them in calculations and so on. When you run a script, it's name is __main__. If you want to do something when you run a script you can put it below that line, it need not be a function called main()
# -*- coding: utf-8 -*-
"""
main_script.py
=======
documentation
"""
import sys
from helper import func1, func2, func3
val1 = func1()
val2 = func2()
val3 = func3(5)
print("magic {}\nhappens {}\non Fridays\n{}".format(val1, val2, val3))
# ===========================================================================
#
if __name__ == "__main__":
"""optional location for parameters"""
print(sys.argv[0])
now the helper
# -*- coding: utf-8 -*-
"""
helper.py
=======
"""
import sys
def func1():
"""Do amazing things"""
return 5
def func2():
"""Do more amazing things"""
return 2
def func3(val):
"""Do amazing things"""
return val + 5
# ==== Processing finished ====
# ===========================================================================
#
if __name__ == "__main__":
"""optional location for parameters"""
print(sys.argv[0])
put both in the same folder
run main_script
compare to running just helper.py