Search code examples
pythonpython-3.xsubprocesseoferror

EOFError when starting with subproccess


When i start my python3 script with subproccess from an other script i get the following error:

Select the keyword preset you want to use:Traceback (most recent call last):
  File "test2.py", line 9, in <module>
    keywordselect=input("Select the keyword preset you want to use:")
EOFError

But when i start the script normally with python3 generate.py it works totally fine with no error.

script1:

import subprocess
p = subprocess.Popen(["python3", "test2.py"])

script2:

print("Keyword")
print("1. Preset1")
print("2. Preset2")
print("3. Preset3")
print("4. Preset4")
print("You can edit the presets in /presets/keywords/.")
selecting = 1
while selecting == 1:
    keywordselect=input("Select the keyword preset you want to use:")
    if keywordselect == "1":
        print("You selected keyword preset 1.")
        selectedkeywordlist = "presets/keywords/preset1.txt"
    elif keywordselect == "2":
        print("You selected keyword preset 2.")
        selectedkeywordlist = "presets/keywords/preset2.txt"
    elif keywordselect == "3":
        print("You selected keyword preset 3.")
        selectedkeywordlist = "presets/keywords/preset3.txt"
    elif keywordselect == "4":
        print("You selected keyword preset 4.")
        selectedkeywordlist = "presets/keywords/preset4.txt"
    else:
        print("You didn't select a valid option, please try again.")

Solution

  • You are using subprocess.Popen which is a by default non blocking, piece of code, hence You program cursor moves rather than waiting for the input from the user. But what you need is a code which is blocking your program cursor. subprocess.call exactly does it. It would be waiting for the other command to execute completely.

    You need to use subprocess.call to solve your problem. Just change your script2 file with this

    import subprocess
    p = subprocess.call(["python", "abca.py"])
    

    You can read more about the difference between subprocess.Popen and subprocess.call in this answer. Which describes when to use which command and the difference between them