简体   繁体   中英

Python.exe opens in a new console window

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 (ie 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?

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.

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.

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.

is not python.exe that was a resul from command cmd

ex: pip list

dat pip list show but automatic close...dats not problem

problem is when i want to runserver

ex: python manage.py runserver [django] result show in the new window then close made the web cant show at my browser

why this problom? cant solve now

简单地说:程序末尾的最后一行可能是: input("\\nIf you want to end the program press any key ...") ...然后你的程序等待键,你会看到你的结果

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM