Issues with the "subprocess" module in Python 3.9.1 (or 3.9.2) - "subprocess.Popen" doesn't work
I think that it is because for .py
files Windows uses python.exe
and for .pyw
files Windows uses pythonw.exe
. The problem is that sys.executable
has the file location of the program used to start the main python file (python.exe
when the extension is .py
and pythonw.exe
when the extension is .pyw
). So the problem is that you are trying to start the .py
program using the python executable that is for .pyw
file extensions.
In cmd:
>>> import sys>>> sys.executable'C:\\Program Files\\Python37\\python.exe'
in IDLE (doesn't have the terminal window):
>>> import sys>>> sys.executable'C:\\Program Files\\Python37\\pythonw.exe'
Therefore, you have to decide which program (python.exe
or pythonw.exe
) you want to use. You can do it based on the file extension.
A simple solution would be to open a command prompt from your main_script.py
manually, and execute script.py
in there, if you really have to execute it as an extra process. If you do not have to, just add import script
at the place you want the content of script.py
to be executed, or, even better, wrap the code in script.py
into a function, import script.py
at the top of main_script.py
and call the funcction at the approproate place. For the first solution, there is an example on DataToFish that has almost the same code as you (using tkinter GUI a.o., you have to scroll down a bit to find the example with a GUI). In short, you just have execute cmd /c {sys.executable} script.pyw
from main_script.py
.