How to keep a Python script output window open?

前端 未结 23 2464
挽巷
挽巷 2020-11-22 10:54

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 an

相关标签:
23条回答
  • 2020-11-22 10:55

    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.

    0 讨论(0)
  • 2020-11-22 10:55

    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:

    raw_input() 
    

    This next example would give you a clear message that the program is done and not waiting on another input prompt within the program:

    print('You have reached the end and the "raw_input()" function is keeping the window open') 
    raw_input()
    

    Note!
    (1) In python 3, there is no raw_input(), just input().
    (2) Use single quotes to indicate a string; otherwise if you type doubles around anything, such as "raw_input()", it will think it is a function, variable, etc, and not text.

    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:

    print("You have reached the end and the "input()" function is keeping the window open")
    input()
    

    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)

    0 讨论(0)
  • 2020-11-22 10:59

    You can just write

    input()
    

    at the end of your code

    therefore when you run you script it will wait for you to enter something

    {ENTER for example}
    
    0 讨论(0)
  • 2020-11-22 10:59

    I found the solution on my py3 enviroment at win10 is just run cmd or powershell as Administrator,and the output would stay at the same console window,any other type of user run python command would cause python to open a new console window.

    0 讨论(0)
  • 2020-11-22 11:00

    you can combine the answers before: (for Notepad++ User)

    press F5 to run current script and type in command:

    cmd /k python -i "$(FULL_CURRENT_PATH)"
    

    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 :)

    0 讨论(0)
  • 2020-11-22 11:04

    You can open PowerShell and type "python". After Python has been imported, you can copy paste the source code from your favourite text-editor to run the code.

    The window won't close.

    0 讨论(0)
提交回复
热议问题