AnsweredAssumed Answered

Calling a Python Script From Another Python Script

Question asked by jborgion Champion on Apr 9, 2019
Latest reply on Apr 12, 2019 by Dan_Patterson

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?

Outcomes