I am new to python and I am trying to import some values from one file to another.
I want my “program” to go from the main_file.py, to other files, like calculator.py, and then comme back to the main_file.py
To do so, I decided I’ll make a variable, previously_runned, which has the value ‘s’.
When the calculator would be activated, through the command, it would pick this variable
( and then give it back to the main_file.py, which will check the value of the variable, and then skip some lines of its own script, if the variable previously_runned has the value ‘s’)…:
import os
file_path = "calc.py"
os.system(file_path)
…The calc.py would then pick the variables from main_file.py…:
import previously_runned from main_file
….Which returns:
File "C:\Users\Kevin\Prog\Calc.py", line 72, in <module>
import main_file
ImportError: No module named main_file
Thanks for any help!
When you do
os.system(), it’s a totally separate Python process; it’s not going to share any variables with the Python instance that’s actually running theos.system()call. Meaning, if you change previously_runned from calc.py, it wouldn’t get reflected in your main file.What you really want to use here are functions; functions are much better units of control flow than modules. It sounds like you may not have gotten there with Python yet, so I think for now you should sit tight – what you’re trying is a good sign that you’re doing well, but it’s not really going to make sense until you’re comfortable defining and calling functions.
Keep at it! Python is pretty awesome.