简体   繁体   中英

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 screen and immediately closes.

  • The only way I can view it is to drag the file directly into cmd, in which case it looks perfect.

  • I am doing the first exercise from "Learn Python the Hard Way". I'm doing everything to the letter as in the book, with one exception. See below*. I've read and re-read it.

  • I don't think this matters, but the book tells me to simply write, "python" "notepad" etc. to run those (and other) programs from my terminal. And I figured out I have to write, " start python", " start notepad", etc. for it to actually run.

  • As per suggestions to this problem on other similar questions here, I have attempted to end my code with 1.) main() 2.) raw_input() and 3.) if __name__ == '__main__': main() No success. Here is my file code which I made in Notepad++:

print "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.'

  • So pretty much, the program executes and closes itself. How might I keep it open in python on its own?

It has been hours of trying to figure this out and this seriously seems like it should be so simple. Could someone please, pretty please, pleeeease point me in the right direction?

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

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.

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.

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.

On windows you can try this :

import os
print "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 ..."

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