5

I used to run Python scripts from my Windows command line, and all the prints were printed in the same console. Now something happened on my machine (Windows 10), and when I launch a Python script from the command line (i.e. open a Command Prompt and run python <my_script.py>), Windows opens a new window (titled with the absolute path of python.exe). This windows closes automatically at the end of the execution, so that I can't see the output.

How do I go back to printing output in the same command prompt window from which I run the script?

Eryk Sun
  • 33,190
  • 5
  • 92
  • 111
  • Does it only happen with certain programs, or all of them? – iz_ Dec 05 '18 at 00:18
  • You could try some of the answers here: https://stackoverflow.com/questions/1000900/how-to-keep-a-python-script-output-window-open . Also, Windows actually has two Python executables, `python.exe` and `pythonw.exe`. I think the terminal pops up when one of these is used, but not the other. So maybe the command "python" just needs to be associated with the other. – Bill M. Dec 05 '18 at 00:31
  • Do you get a UAC prompt? If so, maybe you accidentally configured Python to run with admin access. Try running `python script.py` from an elevated command prompt. If it's still the same, run `where python` to ensure CMD is finding the right version of python.exe, and not a batch script or shortcut. – Eryk Sun Dec 05 '18 at 01:05

4 Answers4

2

Not sure how useful this will be but I had this same problem, found this thread, and realized that the new console window was opening up when I omitted 'python' from the command.

>python myscript.py

shows the output right in the terminal where I typed the command, but

>myscript.py

opens the new console window and closes it immediately after the script runs.

  • I have a similar issue but when opening it via python it says that it cannot find the file (it's been installed via pip and the scripts folder is in the path – My1 Oct 17 '20 at 22:03
  • 2
    This is the problem I'm having. Anyone know how to fix this so that I don't have to say `python` (or `py`)? – Ezra Jul 12 '21 at 04:54
1

It's odd but it very likely a windows setup issue as python is an exe. If memory serves windows will spawn on a > run command so checking the way python is booting will help.

Unfortunately it could be a range of issues, so some steps towards victory:

What happen when you just type python into the cmd? If it simply starts the input >>> - it means your python setup is fine. If a cmd window spawns and disappears it may be a windows permissions issue.

Try running your script with -i flag: python -i script.py. This drops you into the repl when the app completes - displaying your output.

Ensure you're using the native flavour of the cmd to test. Ensuring any command app or IDE isn't injecting a start command or weird /K (spawn new window) flag.

Hope it helps.

Glycerine
  • 7,157
  • 4
  • 39
  • 65
  • It's not a CMD window. It's a console window hosted by conhost.exe. CMD is an interpreter and shell that uses a console, no different from python.exe. `cmd.exe /k` spawns a new shell (but not a new console; that's only `start`), runs a command, waits for it to exit, and then returns to the interactive shell, much like `python -i` runs a script and returns to Python's shell (i.e. REPL). – Eryk Sun Dec 05 '18 at 02:04
  • @Glycerine , typing python in the cmd opens the interactive python prompt (>>>) in a new window, with the Python logo on the top left corner. – Francesco Pistelli Dec 08 '18 at 00:28
0

In my computer this was caused by Windows not knowing what program a .py file was associated with. I solved this by going to: Control Panel -> Programs -> Default Programs -> Associate a file type or protocol with a program (Scroll down) and choose "Choose default apps by file type" Scroll down until you see ".py" and choose the correct Python interpreter.

-2

Simply: last row on the end of your program maybe this: input("\nIf you whish end the program press any key ...") ...and your program wait for the key and you see your outcome