Skip to content Skip to sidebar Skip to footer

Python File Opens And Immediately Closes

Tried running this code first through powershell and then through cmd and even simply clicking it. I am typing 'start python myfile.py' to run it. In each case, the file blinks on

Solution 1:

input("Press Enter to continue...")

Solution 2:

I think it's nothing to do with Python specifically. The screen splashes due to some error in your code and the nature of "start" command.

You code might run into error and exit before it reaches time.sleep and raw_input as other people suggested.

Can you try this in your windows cmd:

start python -i myfile.py

this will start python, then execute your myfile.py. Python will continue to run no matter whether there is bug in your script or not.

Solution 3:

Of course it's going to open and close instantly, it just prints and exits.

You can try doing the following:

import time

<your code>

time.sleep(15)

and it will keep it open for 15 seconds.

Solution 4:

The python file closes because the interpreter encounters an error in the script. To overcome this challenge, simply use Python IDLE program to run your script instead of running from the CLI. Start Idle, create a new file then open your script and click RUN module from the Run menu.

Hope this is useful.

Solution 5:

On windows you can try this :

import osprint"Hello World!"print"Hello again"print"I like typing this."print"This is fun."print'Yay! Printing.'print"I'd much rather you 'not'."print'I "said" do not touch this.'os.system "pause"

Or just put at end:

input "Press any key to continue ..."

Post a Comment for "Python File Opens And Immediately Closes"