2

I’m learning Python and I noticed something strange.
When I double-click my Python file (.py) on Windows to run it, the program seems to run twice.

Here is a simple example:

print("Program started")

name = input("Enter your name: ")
print("Hello,", name)

When I run the file by double-clicking it:

  • The console opens

  • Prints “Program started”

  • Immediately closes and reopens again, asking for input

  • Sometimes the console closes before I can read the output

But when I run the same code from VS Code or CMD, it runs normally.

My questions:

  1. Why does double-clicking a .py file cause the console to open/close so quickly or run twice?

  2. Is there a correct way to run Python scripts by double-clicking so they don’t auto-close?

  3. Should beginners always use CMD/VS Code instead of double-clicking .py files?

I tried searching, but most answers are about loops or input errors, not about double-running on Windows.

Thanks!

New contributor
Aviral Harsh is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
3
  • There is no more program code after the print() statement, so the program is finished at at point, and the application window closes. Why did you expect it to wait? Commented 4 hours ago
  • @JohnGordon: I would expect it to wait at the input statement rather than exit immediately. Commented 4 hours ago
  • @paxdiablo They said "Sometimes the console closes before I can read the output", which means they already typed the input. Commented 2 hours ago

2 Answers 2

0

Make sure to disable Single-click to open items option in File explorer setting.

Sign up to request clarification or add additional context in comments.

Comments

-2

The reason it closes so quick is because the program has been run the way it was meant to be and has no reason to continue, an easy solution for this is to import time and do time.sleep(5) (in this case 5 is the number of seconds it waits)

so if you implement this in your code it would be:

import time

print("Program started")

name = input("Enter your name: ")
print("Hello,", name)
time.sleep(5)

take in note that there are also multiple ways to keep the program running such as running until the user clicks a key to stop.

New contributor
Nouh Aghel is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

2 Comments

The statement name = input("Enter your name: ") seems to be an example of what you refer to as "running until the user clicks a key to stop". The program whould not exit until you enter something.
that statement is not what I meant by clicking a key to stop, you could simply make an input prompting the user to click enter to stop or you could use msvcrt so you could click any key

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.