0

I am not new to Python, but somehow, I have always just created everything in one script, and then just ran that as is.

However, now I find myself having to create a script, and then creating different files for different scenarios which contains different variables etc., that are then used in the other script. So what I would like to do is import this main script into the other different scripts (containing primarily different variables), and then just run everything as intended.

So basically what I have now is:

    import matplotlib.pyplot as plt


    VAR1 = [1, 3, 5]
    VAR2 = "Foo Bar"
    VAR3 = {"foo": 1, "bar": 2}


    def my_func(arg):
        something something


    def my_func2(arg2):
        something something
        if VAR2 == "Foo Bar":
            print("Awesome var name")
        else:
            print("Not awesome var name")


    if __name__ = '__main__':
        something something
        
        my_stuff = [my_func(my_argument) for my_argument in some_list]

        print(VAR3["foo"])
        plt.hist(VAR1)
    
        plt.show()

So this is just an example of course, but as you can see I use the functions, and the variables in the if __name__ = '__main__', as well as some of the variables in the functions.

What I would like to do is to create multiple files containing different variables in each, so myself, or co-workers, can just find the right script, and run it when needed.

But again, I have never tried to combine multiple scripts, so I am really not sure how this works. If I try to just import what I would think was the main script, i.e.:

    import matplotlib.pyplot as plt


    def my_func(arg):
        something something


    def my_func2(arg2):
        something something
        if VAR2 == "Foo Bar":
            print("Awesome var name")
        else:
            print("Not awesome var name")


    if __name__ = '__main__':
        something something
        
        my_stuff = [my_func(my_argument) for my_argument in some_list]

        print(VAR3["foo"])
        plt.hist(VAR1)
    
        plt.show()

Into the script I would like to create multiple files with, i.e.:

    import main_script


    VAR1 = [1, 3, 5]
    VAR2 = "Foo Bar"
    VAR3 = {"foo": 1, "bar": 2}

Then it just run the variables, and the other script is not even loaded. If I try to remove the if __name__ = '__main__' which (if I'm not mistaken) makes sure that the stuff inside it will not run if the script is loaded (which is probably what i would like it to do, or...?), then I think it runs the script, but now it is missing the variables from the variables script.

So basically, how do I fix this ? :)

Shiverz
  • 663
  • 1
  • 8
  • 23
Denver Dang
  • 2,433
  • 3
  • 38
  • 68
  • 1
    Call the imported functions in the script you actually run. – Thomas Sablik Nov 26 '20 at 10:18
  • Maybe [this](https://stackoverflow.com/questions/41858147/how-to-modify-imported-source-code-on-the-fly)? (How to modify imported source code on-the-fly?) – Brambor Nov 26 '20 at 10:24
  • That was my first idea actually. But the stuff in the `if __name__` section is quite a bit of code. Ideally, I would like the variable files, i.e. the files my co-workers should be able to run, and change variables in, to be as clean as possible. And if all the `if __name__` content is in that file as well, it would be as nice imo. But maybe one can just put everything from the `if __name__` section into one function ? – Denver Dang Nov 26 '20 at 10:25
  • Yes you can. See my answer below – Niels Henkens Nov 26 '20 at 10:26

1 Answers1

2

Try changing the part under if __name__ = '__main__': into a function.

Main script:

import matplotlib.pyplot as plt


def my_func(arg):
    something something


def my_func2(arg2):
    something something
    if VAR2 == "Foo Bar":
        print("Awesome var name")
    else:
        print("Not awesome var name")


def executeMainFunction(VAR1, VAR2, VAR3):
    something something
    
    my_stuff = [my_func(my_argument) for my_argument in some_list]

    print(VAR3["foo"])
    plt.hist(VAR1)

    plt.show()

Seperate script

import main_script

VAR1 = [1, 3, 5]
VAR2 = "Foo Bar"
VAR3 = {"foo": 1, "bar": 2}

main_script.executeMainFunction(VAR1, VAR2, VAR3)

Explanation The part under if __name__ = '__main__' is (as you thought) not executed when you import the script into another script. But if you just put that part of the code inside the main script, it will be executed directly when importing that script into another script (so even before declaring your variables VAR1, VAR2, etc.). So that will not work either. Puting the part you want to use inside a function, will make it possible to execute that from within the seperate script, using the variables from the seperate script.

Niels Henkens
  • 2,553
  • 1
  • 12
  • 27
  • But would I need to give all my variables as arguments ? I have about 20 variables. It would maybe look a bit silly to put 20 names into the function. But maybe that is the way to do it? – Denver Dang Nov 26 '20 at 10:32
  • If you want to make in cleaner, you could also put the 20 variables in a dictionary and use that dictionary as the argument. But then you need to get the variables from the dictionary in the main script (like e.g. `plt.hist(var_dict['VAR1'])`) – Niels Henkens Nov 26 '20 at 10:35