How to stop Python closing immediately when executed in Microsoft Windows

后端 未结 15 2208
北荒
北荒 2020-12-02 22:43

I have just started college and we are going to be using python. We really have done nothing so I have downloaded the program and done some print commands, and that\'s it.

相关标签:
15条回答
  • 2020-12-02 23:06

    The only thing that worked for me -i command line argument.

    Just put all your python code inside a .py file and then run the following command;

    python -i script.py
    

    It means that if you set -i variable and run your module then python doesn't exit on SystemExit. Read more at the this link.

    0 讨论(0)
  • 2020-12-02 23:08

    I couldn't find anywhere on the internet a true non-script specific, double click and the window doesn't close solution. I guess I'm too lazy to drag and drop or type when I don't need to so after some experimentation I came up with a solution.

    The basic idea is to reassociate .py files so they run a separate initial script before running the intended script. The initial script launches a new command prompt window with the /k parameter which keeps the command prompt open after completion and runs your intended script in the new window.

    Maybe there are good reasons not to do this, those with more knowledge please comment if so, but I figure if I run into any it is easy to revert back if needed. One possibly undesirable side effect is dragging and dropping or typing and running from a command prompt now opens a second command prompt rather than running in the command prompt you dragged or typed in.

    Now, for the implementation, I call the initial python script python_cmd_k.pyw. I'm using Python 3.7. The code required may differ for other versions. Change the path C:\Python37\python.exe to the location of your python installation. Associate .pyw files to pythonw.exe (not python.exe) through Windows if they aren't already.

    import subprocess
    import sys
    
    #Run a python script in a new command prompt that does not close
    command = 'start cmd /k C:\Python37\python.exe "' + sys.argv[1] + '"'
    subprocess.run(command, shell=True)
    

    This runs every time you double click any .py script and launches a new command prompt to run the script you double clicked. Running through pythonw.exe suppresses the command prompt window when this initial script runs. Otherwise if you run it through python.exe an annoying blink of a command prompt appear as a result of the first window showing briefly each time. The intended script displays because the code in the initial script above runs the intended script with python.exe.

    Now associate .py files with python.exe (not pythonw.exe) through Windows if they are not already and edit the registry entry for this association (Disclaimer: Always back up your registry before editing it if you are unsure of what you are doing). I do not know if there are different paths in the registry for file association for different versions of Windows but for me it is at:

    HKEY_CURRENT_USER\Software\Classes\Applications\python.exe\shell\open\command
    

    Change the data to the pythonw.exe path (not python.exe) and add the path to the ptyhon script above and "%1" as arguments ("%1" passes the full path of the doubled clicked file). For example if pythonw.exe and python_cmd_k.pyw are at C:\Python37\ then:

    "C:\Python37\pythonw.exe" "C:\Python37\python_cmd_k.pyw" "%1"
    

    It is not necessary to put python_cmd_k.pyw in the same directory as pythonw.exe as long as you provide the correct path for both. You can put these in .reg files for easy switching back and forth between using the script and the default behavior. Change the paths as needed in the examples below (location in the registry, your installation of python, the location you put your python_cmd_k.pyw script).

    With ptyhon_cmd_k.pyw (change paths as needed):

    Windows Registry Editor Version 5.00
    
    [HKEY_CURRENT_USER\Software\Classes\Applications\python.exe\shell\open\command]
    @="\"C:\\Python37\\pythonw.exe\" \"C:\\Python37\\python_cmd_k.pyw\" \"%1\""
    

    Default version (change paths as needed):

    Windows Registry Editor Version 5.00
    
    [HKEY_CURRENT_USER\Software\Classes\Applications\python.exe\shell\open\command]
    @="\"C:\\Python37\\python.exe\" \"%1\""
    
    0 讨论(0)
  • 2020-12-02 23:09

    Open your cmd (command prompt) and run Python commmands from there. (on Windows go to run or search and type cmd) It should look like this:

    python yourprogram.py 
    

    This will execute your code in cmd and it will be left open. However to use python command, Python has to be properly installed so cmd recognizes it as a command. Checkout proper installation and variable registration for your OS if this does not happen

    0 讨论(0)
  • 2020-12-02 23:09

    Run the command using the windows command prompt from your main Python library source. Example.

    C:\Python27\python.exe directoryToFile\yourprogram.py
    
    0 讨论(0)
  • 2020-12-02 23:09

    Put

    input('Press ENTER to exit') 
    

    at the end to have it stop.

    0 讨论(0)
  • 2020-12-02 23:10

    I think I am too late to answer this question but anyways here goes nothing.

    I have run in to the same problem before and I think there are two alternative solutions you can choose from.

    1. using sleep(_sometime)

    from time import * sleep(10)

    1. using a prompt message (note that I am using python 2.7)

    exit_now = raw_input("Do you like to exit now (Y)es (N)o ? ")'

    if exit_now.lower() = 'n'

    //more processing here

    Alternatively you can use a hybrid of those two methods as well where you can prompt for a message and use sleep(sometime) to delay the window closing as well. choice is yours.

    please note the above are just ideas and if you want to use any of those in practice you might have to think about your application logic a bit.

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