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 ? :)