Suppose I have two scripts:
#script1.py
import subprocess
numbers = ['1', '2', '3']
for number in numbers:
p = subprocess.Popen('script2.py', number)
Other Script:
#script2.py
import subprocess
from script1 import *
number = sys.argv[1]
print(number)
Error Message (when I run script1.py):
Traceback (most recent call last):
File "script1.py", line 6, in <module>
p = subprocess.Popen('script2.py', number)
File "C:\Users\hacke\AppData\Local\Programs\Python\Python38-
32\lib\subprocess.py", line 753, in __init__
raise TypeError("bufsize must be an integer")
TypeError: bufsize must be an integer
I want this program to open 3 subprocesses where in 1 subprocess number = '1', another subprocess number = '2', and another number = '3'. I have looked through the python documentation and stackoverflow and am yet to find what I am looking for.
NOTE: I am using this code for a bigger program that uses tkinter GUI and the subprocess.Popen line of code, when executed, opens another tkinter window for every subprocess rather than just running the other script.
All help is appreciated and I thank all that reply.
UPDATE: When I execute the script this way:
numbers = ['1', '2', '3']
for number in numbers:
p = subprocess.Popen(['python', 'script2.py', number])
It does pass the variables correctly to script2.py but will infinitely execute the script resulting in this as an output:
1
2
3
1
2
3(continuing the pattern)
Not quite sure how to fix this. This ends up crashing my pc.
According to the docs, the second parameter to Popen()
is called bufsize
. This is not a parameter to your script you are running. Instead, you pass args
as a list to the first parameter:
number = '1'
p = subprocess.Popen(['python', 'script2.py', number])
Note that you also need to pass the string 'python'
since this is the command that will run your Python script.