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:
<SPAN class="string token">"""
script_ 1
playing the role of little calling script
"""</SPAN>
var1 <SPAN class="operator token">=</SPAN> <SPAN class="string token">'A'</SPAN>
var2 <SPAN class="operator token">=</SPAN> <SPAN class="string token">'B'</SPAN>
var3 <SPAN class="operator token">=</SPAN> <SPAN class="string token">'C'</SPAN>
<SPAN class="keyword token">import</SPAN> Script_2
<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
"""
script_2
playing the role of the single big script
"""
from __main__ import *
print(var1)
print(var2)
print(var3)
<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
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:
<SPAN class="keyword token">from</SPAN> __main__ <SPAN class="keyword token">import</SPAN> <SPAN class="operator token">*</SPAN>
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">main</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN>var1<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN>var2<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN>var3<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">if</SPAN> __name__<SPAN class="operator token">==</SPAN> <SPAN class="string token">"__main__"</SPAN><SPAN class="punctuation token">:</SPAN>
main<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>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?