[Python] Why does my Python program run twice when I execute it by double-clicking the .py...

Discussão em 'Python' iniciado por Stack, Novembro 29, 2025.

  1. Stack

    Stack Membro Participativo

    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!

    Continue reading...

Compartilhe esta Página