i have a python 3 script('script1') on windows, and i try to pass a variable('var') to another script('script2').
script1:
import subprocess
from tkinter import *
w=Tk()
def script2(var):
subprocess.Popen('script2.py',stdout=subprocess.PIPE,shell=True)
b1=Button(w,text='var 1',command=lambda:script2('var 1'))
b1.pack()
b2=Button(w,text='var 2',command=lambda:script2('var 2'))
b2.pack()
w.mainloop()
script2:
print(var)
nothing happens...thx all
subprocess.Popen('script2.py',...)does not work in Windows. It should besubprocess.Popen([sys.executable, 'script2.py'], ...). However usingsubprocessto run a Python script, the script will be executed as a new process and so it cannot access any variable in current process. You need to pass it using command line argument and usesys.argvto access it insidescript2.py.python sys.argvand you will find examples on usingsys.argv.