How to keep a Python script output window open?

前端 未结 23 2584
挽巷
挽巷 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 11:05

    To keep your window open in case of exception (yet, while printing the exception)

    Python 2

    if __name__ == '__main__':
        try:
            ## your code, typically one function call
        except Exception:
            import sys
            print sys.exc_info()[0]
            import traceback
            print traceback.format_exc()
            print "Press Enter to continue ..." 
            raw_input() 
    

    To keep the window open in any case:

    if __name__ == '__main__':
        try:
            ## your code, typically one function call
        except Exception:
            import sys
            print sys.exc_info()[0]
            import traceback
            print traceback.format_exc()
        finally:
            print "Press Enter to continue ..." 
            raw_input()
    

    Python 3

    For Python3 you'll have to use input() in place of raw_input(), and of course adapt the print statements.

    if __name__ == '__main__':
        try:
            ## your code, typically one function call
        except BaseException:
            import sys
            print(sys.exc_info()[0])
            import traceback
            print(traceback.format_exc())
            print("Press Enter to continue ...")
            input() 
    

    To keep the window open in any case:

    if __name__ == '__main__':
        try:
            ## your code, typically one function call
    except BaseException:
        import sys
        print(sys.exc_info()[0])
        import traceback
        print(traceback.format_exc())
    finally:
        print("Press Enter to continue ...")
        input()
    
    0 讨论(0)
  • 2020-11-22 11:05

    Apart from input and raw_input, you could also use an infinite while loop, like this: while True: pass (Python 2.5+/3) or while 1: pass (all versions of Python 2/3). This might use computing power, though.

    You could also run the program from the command line. Type python into the command line (Mac OS X Terminal) and it should say Python 3.?.? (Your Python version) It it does not show your Python version, or says python: command not found, look into changing PATH values (enviromentl values, listed above)/type C:\(Python folder\python.exe. If that is successful, type python or C:\(Python installation)\python.exe and the full directory of your program.

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

    The simplest way:

    import time
    
    #Your code here
    time.sleep(60)
    #end of code (and console shut down)
    

    this will leave the code up for 1 minute then close it.

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

    You have a few options:

    1. Run the program from an already-open terminal. Open a command prompt and type:

      python myscript.py
      

      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.

    2. Add code to wait at the end of your script. For Python2, adding ...

      raw_input()
      

      ... 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().

    3. 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.

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

    Create a Windows batch file with these 2 lines:

    python your-program.py
    
    pause
    
    0 讨论(0)
  • 2020-11-22 11:06

    A very belated answer, but I created a Windows Batch file called pythonbat.bat containing the following:

    python.exe %1
    @echo off
    echo.
    pause
    

    and then specified pythonbat.bat as the default handler for .py files.

    Now, when I double-click a .py file in File Explorer, it opens a new console window, runs the Python script and then pauses (remains open), until I press any key...

    No changes required to any Python scripts.

    I can still open a console window and specify python myscript.py if I want to...

    (I just noticed @maurizio already posted this exact answer)

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