Jethro
Jethro

Reputation: 505

Separate Console windows with subprocess and Popen

I have recently changed from python 2.7 to python 3.4 and anaconda.

I had to make some changes for my code to work in 3.4 but I have come across one problem which I'm not sure if it's caused by the new version of Python or Anaconda itself.

Before my transition, I had the command

p=subprocess.Popen('Location of .exe file')

This worked fine as it opened my .exe file in a different window/console where I had to provide some inputs while the python program was running in the background.

Now when I try to run the same command there is on separate console and I have to provide my input in the same console that my python program is running.

Any way to make it go back to the way it was? And can someone explain to me what caused this change in the first place? There have been some suggestions for this problem on this forum but nothing so far worked.

I appreciate your help

Upvotes: 0

Views: 367

Answers (1)

Serge Ballesta
Serge Ballesta

Reputation: 149185

Simply passing from version 2 to 3 of Python should not make such a difference.

I think that in your old Python2.7 environnment, you were using Pythonw.exe (note the ending w) to execute the initial script and that in your new Python3 you are using Python.exe.

Both flavours exist in both version. The difference is that Python.exe is a console application, while Pythonw.exe is a GUI one:

  • when launched from a console, Python.exe uses its parent console, and give it to its (console) subprocesses - if launched from a GUI program (such as the explorer), it will allocate a new console
  • when launched from a console, Pythonw.exe ignores its parent console, and a new console will allocated for any (console) subprocess. But if you try to execute it without a script, it will end immediately because it has no standard input.

So just use Pythonw to start you script, and every subprocess will have its one console

Upvotes: 2

Related Questions