I have just started with Python. When I execute a python script file on Windows, the output window appears but instantaneously goes away. I need it to stay there so I can analyze my output. How can I keep it open?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
you can combine the answers before: (for Notepad++ User)
press F5 to run current script and type in command:
in this way you stay in interactive mode after executing your Notepad++ python script and you are able to play around with your variables and so on :)
Start the script from already open cmd window or at the end of script add something like this, in Python 2:
Or, in Python 3:
A simple hack to keep the window open:
The counter is so the code won’t repeat itself.
To just keep the window open I agree with Anurag and this is what I did to keep my windows open for short little calculation type programs.
This would just show a cursor with no text:
This next example would give you a clear message that the program is done and not waiting on another input prompt within the program:
In this next example, I use double quotes and it won't work because it thinks there is a break in the quotes between "the" and "function" even though when you read it, your own mind can make perfect sense of it:
Hopefully this helps others who might be starting out and still haven't figured out how the computer thinks yet. It can take a while. :o)
You have a few options:
Run the program from an already-open terminal. Open a command prompt and type:
For that to work you need the python executable in your path. Just check on how to edit environment variables on Windows, and add
C:\PYTHON26
(or whatever directory you installed python to).When the program ends, it'll drop you back to the cmd prompt instead of closing the window.
Add code to wait at the end of your script. For Python2, adding ...
... at the end of the script makes it wait for the Enter key. That method is annoying because you have to modify the script, and have to remember removing it when you're done. Specially annoying when testing other people's scripts. For Python3, use
input()
.Use an editor that pauses for you. Some editors prepared for python will automatically pause for you after execution. Other editors allow you to configure the command line it uses to run your program. I find it particularly useful to configure it as "
python -i myscript.py
" when running. That drops you to a python shell after the end of the program, with the program environment loaded, so you may further play with the variables and call functions and methods.I had a similar problem. With Notepad++ I used to use the command :
C:\Python27\python.exe "$(FULL_CURRENT_PATH)"
which closed the cmd window immediately after the code terminated.Now I am using
cmd /k c:\Python27\python.exe "$(FULL_CURRENT_PATH)"
which keeps the cmd window open.