Main program variables in side programs (Python)
Question: How do I use variables that exist in the main program in the side program? For
example, if I were to have Var1 in the main program, how would I use it in the
side program, how would I for example, print it?

Here's what I have right now:

    
    
    #Main program
    Var1 = 1
    
    #Side program
    from folder import mainprogram
    print(mainprogram.Var1)
    

This I think would work, if it didn't run the main program when it imports it,
because I have other functions being executed in it. How would I import all
the main program data, but not have it execute? The only thing I thought of
was to import that specific variable from the program, but I don't know how to
do it. What I have in my head is:

    
    
    from folder import mainprogram 
    from mainprogram import Var1
    

But it still excecutes mainprogram.


Answer: Your approach is basically correct (except for `from folder import
mainprogram` \- that looks a bit strange, unless you want to import a function
named `mainprogram` from a Python script named `folder.py`). You have also
noticed that an imported module is executed on import. This is usually what
you want.

But if there are parts of the module that you only want executed when it's run
directy (as in `python.exe mainprogram.py`) but not when doing `import
mainprogram`, then wrap those parts of the program in an `if` block like this:

    
    
    if __name__ == "__main__":
        # this code will not be run on import
    

