How to stop Python closing immediately when executed in Microsoft WindowsPython 3.5.1 Press enter to exitHow to keep a Python script output window open?Difference between “inspect” and “interactive” command line flags in PythonProject Converting Hex to Base64No errors but wont runHow can you find out which process is listening on a port on Windows?How do I copy a file in Python?How can I safely create a nested directory in Python?How do I parse a string to a float or int in Python?How to get the current time in PythonHow can I make a time delay in Python?How do I get the number of elements in a list in Python?How do I concatenate two lists in Python?How do I install pip on Windows?How do I lowercase a string in Python?

How to manage monthly salary

Is domain driven design an anti-SQL pattern?

Could a US political party gain complete control over the government by removing checks & balances?

Extreme, but not acceptable situation and I can't start the work tomorrow morning

Is this food a bread or a loaf?

aging parents with no investments

Can the Produce Flame cantrip be used to grapple, or as an unarmed strike, in the right circumstances?

LWC and complex parameters

I’m planning on buying a laser printer but concerned about the life cycle of toner in the machine

Prime joint compound before latex paint?

Lied on resume at previous job

Why doesn't a const reference extend the life of a temporary object passed via a function?

Why airport relocation isn't done gradually?

Can I legally use front facing blue light in the UK?

Denied boarding due to overcrowding, Sparpreis ticket. What are my rights?

Why do we use polarized capacitors?

Why is my log file so massive? 22gb. I am running log backups

What is the offset in a seaplane's hull?

Is there a familial term for apples and pears?

Is there a name of the flying bionic bird?

If a centaur druid Wild Shapes into a Giant Elk, do their Charge features stack?

Is Social Media Science Fiction?

What is GPS' 19 year rollover and does it present a cybersecurity issue?

Is Fable (1996) connected in any way to the Fable franchise from Lionhead Studios?



How to stop Python closing immediately when executed in Microsoft Windows


Python 3.5.1 Press enter to exitHow to keep a Python script output window open?Difference between “inspect” and “interactive” command line flags in PythonProject Converting Hex to Base64No errors but wont runHow can you find out which process is listening on a port on Windows?How do I copy a file in Python?How can I safely create a nested directory in Python?How do I parse a string to a float or int in Python?How to get the current time in PythonHow can I make a time delay in Python?How do I get the number of elements in a list in Python?How do I concatenate two lists in Python?How do I install pip on Windows?How do I lowercase a string in Python?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








26















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.



When I run my .py file (a print command) it immediately closes after appearing. I understand why it does this - it's given the output, so it's done what it needs to do - but I also understand that you can stop this from happening.



I looked around this website and none of the solutions given to this question worked, either that or I didn't understand them.



Is there a simple command I can input to my IDLE editor that will put the program on hold or something? I have tried input("prompt: ") as suggested by someone, and that made no difference.



If there isn't a command for this, is there a way to change the settings on the computer so that programs don't auto close?










share|improve this question



















  • 1





    are you working on a windows machine? If you are, are you double clicking on a .py file? Try Opening up a command prompt in the folder that your .py is in and then run python. your_file.py then your editor will display the output but stay open afterwards.

    – Ctrlspc
    Sep 11 '12 at 17:50







  • 2





    if input isnt working you have an error that is killing the file before you get there... run it from "cmd" instead of clicking it as Ctrlspc said :)

    – Joran Beasley
    Sep 11 '12 at 17:55






  • 2





    sorry just noticed a typo and can't edit my comment now, you should run python your_file.py (ignore the dot after python in my last comment.)

    – Ctrlspc
    Sep 11 '12 at 17:59











  • Nice on starting college and using Python! When I started college back in 99 the primary language was Cobol and Java. sucks :(

    – Brandon Poole
    Sep 11 '12 at 18:30






  • 1





    drag-drop the file into a cmd windows and press enter. The cmd windows will remain open (even if it contains an error. input() only work if you don't trigger an error before this line)

    – JinSnow
    Mar 24 '17 at 14:26


















26















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.



When I run my .py file (a print command) it immediately closes after appearing. I understand why it does this - it's given the output, so it's done what it needs to do - but I also understand that you can stop this from happening.



I looked around this website and none of the solutions given to this question worked, either that or I didn't understand them.



Is there a simple command I can input to my IDLE editor that will put the program on hold or something? I have tried input("prompt: ") as suggested by someone, and that made no difference.



If there isn't a command for this, is there a way to change the settings on the computer so that programs don't auto close?










share|improve this question



















  • 1





    are you working on a windows machine? If you are, are you double clicking on a .py file? Try Opening up a command prompt in the folder that your .py is in and then run python. your_file.py then your editor will display the output but stay open afterwards.

    – Ctrlspc
    Sep 11 '12 at 17:50







  • 2





    if input isnt working you have an error that is killing the file before you get there... run it from "cmd" instead of clicking it as Ctrlspc said :)

    – Joran Beasley
    Sep 11 '12 at 17:55






  • 2





    sorry just noticed a typo and can't edit my comment now, you should run python your_file.py (ignore the dot after python in my last comment.)

    – Ctrlspc
    Sep 11 '12 at 17:59











  • Nice on starting college and using Python! When I started college back in 99 the primary language was Cobol and Java. sucks :(

    – Brandon Poole
    Sep 11 '12 at 18:30






  • 1





    drag-drop the file into a cmd windows and press enter. The cmd windows will remain open (even if it contains an error. input() only work if you don't trigger an error before this line)

    – JinSnow
    Mar 24 '17 at 14:26














26












26








26


4






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.



When I run my .py file (a print command) it immediately closes after appearing. I understand why it does this - it's given the output, so it's done what it needs to do - but I also understand that you can stop this from happening.



I looked around this website and none of the solutions given to this question worked, either that or I didn't understand them.



Is there a simple command I can input to my IDLE editor that will put the program on hold or something? I have tried input("prompt: ") as suggested by someone, and that made no difference.



If there isn't a command for this, is there a way to change the settings on the computer so that programs don't auto close?










share|improve this question
















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.



When I run my .py file (a print command) it immediately closes after appearing. I understand why it does this - it's given the output, so it's done what it needs to do - but I also understand that you can stop this from happening.



I looked around this website and none of the solutions given to this question worked, either that or I didn't understand them.



Is there a simple command I can input to my IDLE editor that will put the program on hold or something? I have tried input("prompt: ") as suggested by someone, and that made no difference.



If there isn't a command for this, is there a way to change the settings on the computer so that programs don't auto close?







python windows






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jul 13 '14 at 12:34









Cristian Ciupitu

14.9k54264




14.9k54264










asked Sep 11 '12 at 17:48









keirbtrekeirbtre

54351014




54351014







  • 1





    are you working on a windows machine? If you are, are you double clicking on a .py file? Try Opening up a command prompt in the folder that your .py is in and then run python. your_file.py then your editor will display the output but stay open afterwards.

    – Ctrlspc
    Sep 11 '12 at 17:50







  • 2





    if input isnt working you have an error that is killing the file before you get there... run it from "cmd" instead of clicking it as Ctrlspc said :)

    – Joran Beasley
    Sep 11 '12 at 17:55






  • 2





    sorry just noticed a typo and can't edit my comment now, you should run python your_file.py (ignore the dot after python in my last comment.)

    – Ctrlspc
    Sep 11 '12 at 17:59











  • Nice on starting college and using Python! When I started college back in 99 the primary language was Cobol and Java. sucks :(

    – Brandon Poole
    Sep 11 '12 at 18:30






  • 1





    drag-drop the file into a cmd windows and press enter. The cmd windows will remain open (even if it contains an error. input() only work if you don't trigger an error before this line)

    – JinSnow
    Mar 24 '17 at 14:26













  • 1





    are you working on a windows machine? If you are, are you double clicking on a .py file? Try Opening up a command prompt in the folder that your .py is in and then run python. your_file.py then your editor will display the output but stay open afterwards.

    – Ctrlspc
    Sep 11 '12 at 17:50







  • 2





    if input isnt working you have an error that is killing the file before you get there... run it from "cmd" instead of clicking it as Ctrlspc said :)

    – Joran Beasley
    Sep 11 '12 at 17:55






  • 2





    sorry just noticed a typo and can't edit my comment now, you should run python your_file.py (ignore the dot after python in my last comment.)

    – Ctrlspc
    Sep 11 '12 at 17:59











  • Nice on starting college and using Python! When I started college back in 99 the primary language was Cobol and Java. sucks :(

    – Brandon Poole
    Sep 11 '12 at 18:30






  • 1





    drag-drop the file into a cmd windows and press enter. The cmd windows will remain open (even if it contains an error. input() only work if you don't trigger an error before this line)

    – JinSnow
    Mar 24 '17 at 14:26








1




1





are you working on a windows machine? If you are, are you double clicking on a .py file? Try Opening up a command prompt in the folder that your .py is in and then run python. your_file.py then your editor will display the output but stay open afterwards.

– Ctrlspc
Sep 11 '12 at 17:50






are you working on a windows machine? If you are, are you double clicking on a .py file? Try Opening up a command prompt in the folder that your .py is in and then run python. your_file.py then your editor will display the output but stay open afterwards.

– Ctrlspc
Sep 11 '12 at 17:50





2




2





if input isnt working you have an error that is killing the file before you get there... run it from "cmd" instead of clicking it as Ctrlspc said :)

– Joran Beasley
Sep 11 '12 at 17:55





if input isnt working you have an error that is killing the file before you get there... run it from "cmd" instead of clicking it as Ctrlspc said :)

– Joran Beasley
Sep 11 '12 at 17:55




2




2





sorry just noticed a typo and can't edit my comment now, you should run python your_file.py (ignore the dot after python in my last comment.)

– Ctrlspc
Sep 11 '12 at 17:59





sorry just noticed a typo and can't edit my comment now, you should run python your_file.py (ignore the dot after python in my last comment.)

– Ctrlspc
Sep 11 '12 at 17:59













Nice on starting college and using Python! When I started college back in 99 the primary language was Cobol and Java. sucks :(

– Brandon Poole
Sep 11 '12 at 18:30





Nice on starting college and using Python! When I started college back in 99 the primary language was Cobol and Java. sucks :(

– Brandon Poole
Sep 11 '12 at 18:30




1




1





drag-drop the file into a cmd windows and press enter. The cmd windows will remain open (even if it contains an error. input() only work if you don't trigger an error before this line)

– JinSnow
Mar 24 '17 at 14:26






drag-drop the file into a cmd windows and press enter. The cmd windows will remain open (even if it contains an error. input() only work if you don't trigger an error before this line)

– JinSnow
Mar 24 '17 at 14:26













13 Answers
13






active

oldest

votes


















17














In Python 3, add the following to the end of your code:



input('Press ENTER to exit')


This will cause the program to wait for user input, with pressing ENTER causing the program to finish.



You can double click on your script.py file in Windows conveniently this way.






share|improve this answer


















  • 2





    This will cause the program to wait for user input except if you trigger an error in the line before your input()

    – JinSnow
    Mar 24 '17 at 14:25






  • 2





    In python 2.7.x use raw_input() instead of input().

    – G4mo
    May 31 '17 at 12:51


















11














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.






share|improve this answer

























  • The -i options says to switch to interactive mode (with >>> prompt) after the program finishes. One can then exit or interact with glocal objects. Running code from an IDLE editor has the same effect.

    – Terry Jan Reedy
    Jan 1 '17 at 23:35











  • Upvoted, but this would only be a solution if someone can explain how to turn on -i mode automatically when the user double-clicks on a Python script. This is the use case of the question; with command line usage, the window doesn't close anyway.

    – alexis
    Apr 14 '17 at 22:07


















6














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






share|improve this answer























  • I have done as you said and it just says "'python' is not recognized as an internal or external command, operable program or batch file".

    – keirbtre
    Sep 11 '12 at 18:27











  • You didnt do as mentioned because you you didnt register global variables, how to do it explained here docs.python.org/using/windows.html Especially look at 3.3.1

    – rodling
    Sep 11 '12 at 18:30






  • 1





    What? Yes I did do as mentioned. If he missed something out, and I follow the instructions, I'm not "not doing" what I've been told to do. I've found the Environment Variables but don't know what I'm meant to do with them now, as that link doesn't, as far as I can see, tell you what to do. Thanks for more responses btw :)

    – keirbtre
    Sep 11 '12 at 18:37











  • are you on windows? you might have to set the path environment variable to the folder that has got python.exe, for example if python.exe is in c:python27 then you can add ;c:python27 at the end of your path string.

    – Ctrlspc
    Sep 11 '12 at 18:45











  • Yeah I'm on windows. I don't see how to do your instructions Ctrlspc, because when I type that into cmd it just comes up with an error because there's a space in the location of "Program Files (x86)"

    – keirbtre
    Sep 11 '12 at 18:55


















5














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



C:Python27python.exe directoryToFileyourprogram.py





share|improve this answer























  • or just drag drop your .py into the cmd windows (and press enter)

    – JinSnow
    Mar 24 '17 at 14:29


















2














If you just want a delay



from time import *

sleep(20)





share|improve this answer
































    2














    Depending on what I'm using it for, or if I'm doing something that others will use, I typically just input("Do eighteen backflips to continue") if it's just for me, if others will be using I just create a batch file and pause it after



    cd '/file/path/here'
    python yourfile.py
    pause


    I use the above if there is going to be files renamed, moved, copied, etc. and my cmd needs to be in the particular folder for things to fall where I want them, otherwise - just



    python '/path/to/file/yourfile.py'
    pause





    share|improve this answer






























      1














      In Python 2.7 adding this to the end of my py file (if __name__ == '__main__':) works:



      closeInput = raw_input("Press ENTER to exit")
      print "Closing..."





      share|improve this answer






























        1














        Put



        input('Press ENTER to exit') 


        at the end to have it stop.






        share|improve this answer
































          0














          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.






          share|improve this answer






























            0














            Late in here, but in case someone comes here from google---



            Go the the location of your .py file. Press SHIFT then right click anywhere and choose open command prompt from here. Once it's up, Just add



            "python NameOfTheProg.py" to the cmd line






            share|improve this answer






























              0














              The reason why it is closing is because the program is not running anymore, simply add any sort of loop or input to fix this (or you could just run it through idle.)






              share|improve this answer






























                0














                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:Python37python.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:Python37python.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_USERSoftwareClassesApplicationspython.exeshellopencommand


                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:Python37pythonw.exe" "C:Python37python_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_USERSoftwareClassesApplicationspython.exeshellopencommand]
                @=""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_USERSoftwareClassesApplicationspython.exeshellopencommand]
                @=""C:\Python37\python.exe" "%1""





                share|improve this answer























                • After going through all of that I discovered there's a much easier way. Assuming .py files are associated with python.exe through windows, just edit HKEY_CURRENT_USERSoftwareClassesApplicationspython.exeshellopencommand to C:WindowsSystem32cmd.exe /k python "%1". No extra python script needed.

                  – BrianZ111
                  Mar 8 at 23:55



















                -1














                Well I got similar issue,
                It is solved by adding Environment Variable.



                Add System Variables in Window



                Name : PYTHONPATH



                Value : C:Python27;



                Your Python path.






                share|improve this answer























                  Your Answer






                  StackExchange.ifUsing("editor", function ()
                  StackExchange.using("externalEditor", function ()
                  StackExchange.using("snippets", function ()
                  StackExchange.snippets.init();
                  );
                  );
                  , "code-snippets");

                  StackExchange.ready(function()
                  var channelOptions =
                  tags: "".split(" "),
                  id: "1"
                  ;
                  initTagRenderer("".split(" "), "".split(" "), channelOptions);

                  StackExchange.using("externalEditor", function()
                  // Have to fire editor after snippets, if snippets enabled
                  if (StackExchange.settings.snippets.snippetsEnabled)
                  StackExchange.using("snippets", function()
                  createEditor();
                  );

                  else
                  createEditor();

                  );

                  function createEditor()
                  StackExchange.prepareEditor(
                  heartbeatType: 'answer',
                  autoActivateHeartbeat: false,
                  convertImagesToLinks: true,
                  noModals: true,
                  showLowRepImageUploadWarning: true,
                  reputationToPostImages: 10,
                  bindNavPrevention: true,
                  postfix: "",
                  imageUploader:
                  brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                  contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                  allowUrls: true
                  ,
                  onDemand: true,
                  discardSelector: ".discard-answer"
                  ,immediatelyShowMarkdownHelp:true
                  );



                  );













                  draft saved

                  draft discarded


















                  StackExchange.ready(
                  function ()
                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f12375173%2fhow-to-stop-python-closing-immediately-when-executed-in-microsoft-windows%23new-answer', 'question_page');

                  );

                  Post as a guest















                  Required, but never shown

























                  13 Answers
                  13






                  active

                  oldest

                  votes








                  13 Answers
                  13






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes









                  17














                  In Python 3, add the following to the end of your code:



                  input('Press ENTER to exit')


                  This will cause the program to wait for user input, with pressing ENTER causing the program to finish.



                  You can double click on your script.py file in Windows conveniently this way.






                  share|improve this answer


















                  • 2





                    This will cause the program to wait for user input except if you trigger an error in the line before your input()

                    – JinSnow
                    Mar 24 '17 at 14:25






                  • 2





                    In python 2.7.x use raw_input() instead of input().

                    – G4mo
                    May 31 '17 at 12:51















                  17














                  In Python 3, add the following to the end of your code:



                  input('Press ENTER to exit')


                  This will cause the program to wait for user input, with pressing ENTER causing the program to finish.



                  You can double click on your script.py file in Windows conveniently this way.






                  share|improve this answer


















                  • 2





                    This will cause the program to wait for user input except if you trigger an error in the line before your input()

                    – JinSnow
                    Mar 24 '17 at 14:25






                  • 2





                    In python 2.7.x use raw_input() instead of input().

                    – G4mo
                    May 31 '17 at 12:51













                  17












                  17








                  17







                  In Python 3, add the following to the end of your code:



                  input('Press ENTER to exit')


                  This will cause the program to wait for user input, with pressing ENTER causing the program to finish.



                  You can double click on your script.py file in Windows conveniently this way.






                  share|improve this answer













                  In Python 3, add the following to the end of your code:



                  input('Press ENTER to exit')


                  This will cause the program to wait for user input, with pressing ENTER causing the program to finish.



                  You can double click on your script.py file in Windows conveniently this way.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Apr 4 '13 at 20:26









                  MikeMike

                  27122




                  27122







                  • 2





                    This will cause the program to wait for user input except if you trigger an error in the line before your input()

                    – JinSnow
                    Mar 24 '17 at 14:25






                  • 2





                    In python 2.7.x use raw_input() instead of input().

                    – G4mo
                    May 31 '17 at 12:51












                  • 2





                    This will cause the program to wait for user input except if you trigger an error in the line before your input()

                    – JinSnow
                    Mar 24 '17 at 14:25






                  • 2





                    In python 2.7.x use raw_input() instead of input().

                    – G4mo
                    May 31 '17 at 12:51







                  2




                  2





                  This will cause the program to wait for user input except if you trigger an error in the line before your input()

                  – JinSnow
                  Mar 24 '17 at 14:25





                  This will cause the program to wait for user input except if you trigger an error in the line before your input()

                  – JinSnow
                  Mar 24 '17 at 14:25




                  2




                  2





                  In python 2.7.x use raw_input() instead of input().

                  – G4mo
                  May 31 '17 at 12:51





                  In python 2.7.x use raw_input() instead of input().

                  – G4mo
                  May 31 '17 at 12:51













                  11














                  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.






                  share|improve this answer

























                  • The -i options says to switch to interactive mode (with >>> prompt) after the program finishes. One can then exit or interact with glocal objects. Running code from an IDLE editor has the same effect.

                    – Terry Jan Reedy
                    Jan 1 '17 at 23:35











                  • Upvoted, but this would only be a solution if someone can explain how to turn on -i mode automatically when the user double-clicks on a Python script. This is the use case of the question; with command line usage, the window doesn't close anyway.

                    – alexis
                    Apr 14 '17 at 22:07















                  11














                  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.






                  share|improve this answer

























                  • The -i options says to switch to interactive mode (with >>> prompt) after the program finishes. One can then exit or interact with glocal objects. Running code from an IDLE editor has the same effect.

                    – Terry Jan Reedy
                    Jan 1 '17 at 23:35











                  • Upvoted, but this would only be a solution if someone can explain how to turn on -i mode automatically when the user double-clicks on a Python script. This is the use case of the question; with command line usage, the window doesn't close anyway.

                    – alexis
                    Apr 14 '17 at 22:07













                  11












                  11








                  11







                  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.






                  share|improve this answer















                  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.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited May 23 '17 at 12:17









                  Community

                  11




                  11










                  answered May 5 '16 at 13:22









                  AnandShanbhagAnandShanbhag

                  3,47511213




                  3,47511213












                  • The -i options says to switch to interactive mode (with >>> prompt) after the program finishes. One can then exit or interact with glocal objects. Running code from an IDLE editor has the same effect.

                    – Terry Jan Reedy
                    Jan 1 '17 at 23:35











                  • Upvoted, but this would only be a solution if someone can explain how to turn on -i mode automatically when the user double-clicks on a Python script. This is the use case of the question; with command line usage, the window doesn't close anyway.

                    – alexis
                    Apr 14 '17 at 22:07

















                  • The -i options says to switch to interactive mode (with >>> prompt) after the program finishes. One can then exit or interact with glocal objects. Running code from an IDLE editor has the same effect.

                    – Terry Jan Reedy
                    Jan 1 '17 at 23:35











                  • Upvoted, but this would only be a solution if someone can explain how to turn on -i mode automatically when the user double-clicks on a Python script. This is the use case of the question; with command line usage, the window doesn't close anyway.

                    – alexis
                    Apr 14 '17 at 22:07
















                  The -i options says to switch to interactive mode (with >>> prompt) after the program finishes. One can then exit or interact with glocal objects. Running code from an IDLE editor has the same effect.

                  – Terry Jan Reedy
                  Jan 1 '17 at 23:35





                  The -i options says to switch to interactive mode (with >>> prompt) after the program finishes. One can then exit or interact with glocal objects. Running code from an IDLE editor has the same effect.

                  – Terry Jan Reedy
                  Jan 1 '17 at 23:35













                  Upvoted, but this would only be a solution if someone can explain how to turn on -i mode automatically when the user double-clicks on a Python script. This is the use case of the question; with command line usage, the window doesn't close anyway.

                  – alexis
                  Apr 14 '17 at 22:07





                  Upvoted, but this would only be a solution if someone can explain how to turn on -i mode automatically when the user double-clicks on a Python script. This is the use case of the question; with command line usage, the window doesn't close anyway.

                  – alexis
                  Apr 14 '17 at 22:07











                  6














                  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






                  share|improve this answer























                  • I have done as you said and it just says "'python' is not recognized as an internal or external command, operable program or batch file".

                    – keirbtre
                    Sep 11 '12 at 18:27











                  • You didnt do as mentioned because you you didnt register global variables, how to do it explained here docs.python.org/using/windows.html Especially look at 3.3.1

                    – rodling
                    Sep 11 '12 at 18:30






                  • 1





                    What? Yes I did do as mentioned. If he missed something out, and I follow the instructions, I'm not "not doing" what I've been told to do. I've found the Environment Variables but don't know what I'm meant to do with them now, as that link doesn't, as far as I can see, tell you what to do. Thanks for more responses btw :)

                    – keirbtre
                    Sep 11 '12 at 18:37











                  • are you on windows? you might have to set the path environment variable to the folder that has got python.exe, for example if python.exe is in c:python27 then you can add ;c:python27 at the end of your path string.

                    – Ctrlspc
                    Sep 11 '12 at 18:45











                  • Yeah I'm on windows. I don't see how to do your instructions Ctrlspc, because when I type that into cmd it just comes up with an error because there's a space in the location of "Program Files (x86)"

                    – keirbtre
                    Sep 11 '12 at 18:55















                  6














                  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






                  share|improve this answer























                  • I have done as you said and it just says "'python' is not recognized as an internal or external command, operable program or batch file".

                    – keirbtre
                    Sep 11 '12 at 18:27











                  • You didnt do as mentioned because you you didnt register global variables, how to do it explained here docs.python.org/using/windows.html Especially look at 3.3.1

                    – rodling
                    Sep 11 '12 at 18:30






                  • 1





                    What? Yes I did do as mentioned. If he missed something out, and I follow the instructions, I'm not "not doing" what I've been told to do. I've found the Environment Variables but don't know what I'm meant to do with them now, as that link doesn't, as far as I can see, tell you what to do. Thanks for more responses btw :)

                    – keirbtre
                    Sep 11 '12 at 18:37











                  • are you on windows? you might have to set the path environment variable to the folder that has got python.exe, for example if python.exe is in c:python27 then you can add ;c:python27 at the end of your path string.

                    – Ctrlspc
                    Sep 11 '12 at 18:45











                  • Yeah I'm on windows. I don't see how to do your instructions Ctrlspc, because when I type that into cmd it just comes up with an error because there's a space in the location of "Program Files (x86)"

                    – keirbtre
                    Sep 11 '12 at 18:55













                  6












                  6








                  6







                  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






                  share|improve this answer













                  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







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Sep 11 '12 at 18:13









                  rodlingrodling

                  42521439




                  42521439












                  • I have done as you said and it just says "'python' is not recognized as an internal or external command, operable program or batch file".

                    – keirbtre
                    Sep 11 '12 at 18:27











                  • You didnt do as mentioned because you you didnt register global variables, how to do it explained here docs.python.org/using/windows.html Especially look at 3.3.1

                    – rodling
                    Sep 11 '12 at 18:30






                  • 1





                    What? Yes I did do as mentioned. If he missed something out, and I follow the instructions, I'm not "not doing" what I've been told to do. I've found the Environment Variables but don't know what I'm meant to do with them now, as that link doesn't, as far as I can see, tell you what to do. Thanks for more responses btw :)

                    – keirbtre
                    Sep 11 '12 at 18:37











                  • are you on windows? you might have to set the path environment variable to the folder that has got python.exe, for example if python.exe is in c:python27 then you can add ;c:python27 at the end of your path string.

                    – Ctrlspc
                    Sep 11 '12 at 18:45











                  • Yeah I'm on windows. I don't see how to do your instructions Ctrlspc, because when I type that into cmd it just comes up with an error because there's a space in the location of "Program Files (x86)"

                    – keirbtre
                    Sep 11 '12 at 18:55

















                  • I have done as you said and it just says "'python' is not recognized as an internal or external command, operable program or batch file".

                    – keirbtre
                    Sep 11 '12 at 18:27











                  • You didnt do as mentioned because you you didnt register global variables, how to do it explained here docs.python.org/using/windows.html Especially look at 3.3.1

                    – rodling
                    Sep 11 '12 at 18:30






                  • 1





                    What? Yes I did do as mentioned. If he missed something out, and I follow the instructions, I'm not "not doing" what I've been told to do. I've found the Environment Variables but don't know what I'm meant to do with them now, as that link doesn't, as far as I can see, tell you what to do. Thanks for more responses btw :)

                    – keirbtre
                    Sep 11 '12 at 18:37











                  • are you on windows? you might have to set the path environment variable to the folder that has got python.exe, for example if python.exe is in c:python27 then you can add ;c:python27 at the end of your path string.

                    – Ctrlspc
                    Sep 11 '12 at 18:45











                  • Yeah I'm on windows. I don't see how to do your instructions Ctrlspc, because when I type that into cmd it just comes up with an error because there's a space in the location of "Program Files (x86)"

                    – keirbtre
                    Sep 11 '12 at 18:55
















                  I have done as you said and it just says "'python' is not recognized as an internal or external command, operable program or batch file".

                  – keirbtre
                  Sep 11 '12 at 18:27





                  I have done as you said and it just says "'python' is not recognized as an internal or external command, operable program or batch file".

                  – keirbtre
                  Sep 11 '12 at 18:27













                  You didnt do as mentioned because you you didnt register global variables, how to do it explained here docs.python.org/using/windows.html Especially look at 3.3.1

                  – rodling
                  Sep 11 '12 at 18:30





                  You didnt do as mentioned because you you didnt register global variables, how to do it explained here docs.python.org/using/windows.html Especially look at 3.3.1

                  – rodling
                  Sep 11 '12 at 18:30




                  1




                  1





                  What? Yes I did do as mentioned. If he missed something out, and I follow the instructions, I'm not "not doing" what I've been told to do. I've found the Environment Variables but don't know what I'm meant to do with them now, as that link doesn't, as far as I can see, tell you what to do. Thanks for more responses btw :)

                  – keirbtre
                  Sep 11 '12 at 18:37





                  What? Yes I did do as mentioned. If he missed something out, and I follow the instructions, I'm not "not doing" what I've been told to do. I've found the Environment Variables but don't know what I'm meant to do with them now, as that link doesn't, as far as I can see, tell you what to do. Thanks for more responses btw :)

                  – keirbtre
                  Sep 11 '12 at 18:37













                  are you on windows? you might have to set the path environment variable to the folder that has got python.exe, for example if python.exe is in c:python27 then you can add ;c:python27 at the end of your path string.

                  – Ctrlspc
                  Sep 11 '12 at 18:45





                  are you on windows? you might have to set the path environment variable to the folder that has got python.exe, for example if python.exe is in c:python27 then you can add ;c:python27 at the end of your path string.

                  – Ctrlspc
                  Sep 11 '12 at 18:45













                  Yeah I'm on windows. I don't see how to do your instructions Ctrlspc, because when I type that into cmd it just comes up with an error because there's a space in the location of "Program Files (x86)"

                  – keirbtre
                  Sep 11 '12 at 18:55





                  Yeah I'm on windows. I don't see how to do your instructions Ctrlspc, because when I type that into cmd it just comes up with an error because there's a space in the location of "Program Files (x86)"

                  – keirbtre
                  Sep 11 '12 at 18:55











                  5














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



                  C:Python27python.exe directoryToFileyourprogram.py





                  share|improve this answer























                  • or just drag drop your .py into the cmd windows (and press enter)

                    – JinSnow
                    Mar 24 '17 at 14:29















                  5














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



                  C:Python27python.exe directoryToFileyourprogram.py





                  share|improve this answer























                  • or just drag drop your .py into the cmd windows (and press enter)

                    – JinSnow
                    Mar 24 '17 at 14:29













                  5












                  5








                  5







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



                  C:Python27python.exe directoryToFileyourprogram.py





                  share|improve this answer













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



                  C:Python27python.exe directoryToFileyourprogram.py






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Sep 11 '12 at 18:35









                  Brandon PooleBrandon Poole

                  36127




                  36127












                  • or just drag drop your .py into the cmd windows (and press enter)

                    – JinSnow
                    Mar 24 '17 at 14:29

















                  • or just drag drop your .py into the cmd windows (and press enter)

                    – JinSnow
                    Mar 24 '17 at 14:29
















                  or just drag drop your .py into the cmd windows (and press enter)

                  – JinSnow
                  Mar 24 '17 at 14:29





                  or just drag drop your .py into the cmd windows (and press enter)

                  – JinSnow
                  Mar 24 '17 at 14:29











                  2














                  If you just want a delay



                  from time import *

                  sleep(20)





                  share|improve this answer





























                    2














                    If you just want a delay



                    from time import *

                    sleep(20)





                    share|improve this answer



























                      2












                      2








                      2







                      If you just want a delay



                      from time import *

                      sleep(20)





                      share|improve this answer















                      If you just want a delay



                      from time import *

                      sleep(20)






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Jul 13 '14 at 21:01









                      Cristian Ciupitu

                      14.9k54264




                      14.9k54264










                      answered Jul 31 '13 at 20:56









                      photonsphotons

                      212




                      212





















                          2














                          Depending on what I'm using it for, or if I'm doing something that others will use, I typically just input("Do eighteen backflips to continue") if it's just for me, if others will be using I just create a batch file and pause it after



                          cd '/file/path/here'
                          python yourfile.py
                          pause


                          I use the above if there is going to be files renamed, moved, copied, etc. and my cmd needs to be in the particular folder for things to fall where I want them, otherwise - just



                          python '/path/to/file/yourfile.py'
                          pause





                          share|improve this answer



























                            2














                            Depending on what I'm using it for, or if I'm doing something that others will use, I typically just input("Do eighteen backflips to continue") if it's just for me, if others will be using I just create a batch file and pause it after



                            cd '/file/path/here'
                            python yourfile.py
                            pause


                            I use the above if there is going to be files renamed, moved, copied, etc. and my cmd needs to be in the particular folder for things to fall where I want them, otherwise - just



                            python '/path/to/file/yourfile.py'
                            pause





                            share|improve this answer

























                              2












                              2








                              2







                              Depending on what I'm using it for, or if I'm doing something that others will use, I typically just input("Do eighteen backflips to continue") if it's just for me, if others will be using I just create a batch file and pause it after



                              cd '/file/path/here'
                              python yourfile.py
                              pause


                              I use the above if there is going to be files renamed, moved, copied, etc. and my cmd needs to be in the particular folder for things to fall where I want them, otherwise - just



                              python '/path/to/file/yourfile.py'
                              pause





                              share|improve this answer













                              Depending on what I'm using it for, or if I'm doing something that others will use, I typically just input("Do eighteen backflips to continue") if it's just for me, if others will be using I just create a batch file and pause it after



                              cd '/file/path/here'
                              python yourfile.py
                              pause


                              I use the above if there is going to be files renamed, moved, copied, etc. and my cmd needs to be in the particular folder for things to fall where I want them, otherwise - just



                              python '/path/to/file/yourfile.py'
                              pause






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Sep 25 '17 at 17:04









                              JMB2KJMB2K

                              232




                              232





















                                  1














                                  In Python 2.7 adding this to the end of my py file (if __name__ == '__main__':) works:



                                  closeInput = raw_input("Press ENTER to exit")
                                  print "Closing..."





                                  share|improve this answer



























                                    1














                                    In Python 2.7 adding this to the end of my py file (if __name__ == '__main__':) works:



                                    closeInput = raw_input("Press ENTER to exit")
                                    print "Closing..."





                                    share|improve this answer

























                                      1












                                      1








                                      1







                                      In Python 2.7 adding this to the end of my py file (if __name__ == '__main__':) works:



                                      closeInput = raw_input("Press ENTER to exit")
                                      print "Closing..."





                                      share|improve this answer













                                      In Python 2.7 adding this to the end of my py file (if __name__ == '__main__':) works:



                                      closeInput = raw_input("Press ENTER to exit")
                                      print "Closing..."






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Nov 14 '13 at 8:44









                                      SamuelSamuel

                                      655819




                                      655819





















                                          1














                                          Put



                                          input('Press ENTER to exit') 


                                          at the end to have it stop.






                                          share|improve this answer





























                                            1














                                            Put



                                            input('Press ENTER to exit') 


                                            at the end to have it stop.






                                            share|improve this answer



























                                              1












                                              1








                                              1







                                              Put



                                              input('Press ENTER to exit') 


                                              at the end to have it stop.






                                              share|improve this answer















                                              Put



                                              input('Press ENTER to exit') 


                                              at the end to have it stop.







                                              share|improve this answer














                                              share|improve this answer



                                              share|improve this answer








                                              edited Sep 14 '18 at 22:30









                                              zx485

                                              15.3k133248




                                              15.3k133248










                                              answered Sep 14 '18 at 21:40









                                              codercoder

                                              115




                                              115





















                                                  0














                                                  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.






                                                  share|improve this answer



























                                                    0














                                                    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.






                                                    share|improve this answer

























                                                      0












                                                      0








                                                      0







                                                      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.






                                                      share|improve this answer













                                                      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.







                                                      share|improve this answer












                                                      share|improve this answer



                                                      share|improve this answer










                                                      answered Feb 27 '17 at 4:41









                                                      Pissu PusaPissu Pusa

                                                      1661418




                                                      1661418





















                                                          0














                                                          Late in here, but in case someone comes here from google---



                                                          Go the the location of your .py file. Press SHIFT then right click anywhere and choose open command prompt from here. Once it's up, Just add



                                                          "python NameOfTheProg.py" to the cmd line






                                                          share|improve this answer



























                                                            0














                                                            Late in here, but in case someone comes here from google---



                                                            Go the the location of your .py file. Press SHIFT then right click anywhere and choose open command prompt from here. Once it's up, Just add



                                                            "python NameOfTheProg.py" to the cmd line






                                                            share|improve this answer

























                                                              0












                                                              0








                                                              0







                                                              Late in here, but in case someone comes here from google---



                                                              Go the the location of your .py file. Press SHIFT then right click anywhere and choose open command prompt from here. Once it's up, Just add



                                                              "python NameOfTheProg.py" to the cmd line






                                                              share|improve this answer













                                                              Late in here, but in case someone comes here from google---



                                                              Go the the location of your .py file. Press SHIFT then right click anywhere and choose open command prompt from here. Once it's up, Just add



                                                              "python NameOfTheProg.py" to the cmd line







                                                              share|improve this answer












                                                              share|improve this answer



                                                              share|improve this answer










                                                              answered Jun 2 '18 at 12:01









                                                              user3454450user3454450

                                                              1




                                                              1





















                                                                  0














                                                                  The reason why it is closing is because the program is not running anymore, simply add any sort of loop or input to fix this (or you could just run it through idle.)






                                                                  share|improve this answer



























                                                                    0














                                                                    The reason why it is closing is because the program is not running anymore, simply add any sort of loop or input to fix this (or you could just run it through idle.)






                                                                    share|improve this answer

























                                                                      0












                                                                      0








                                                                      0







                                                                      The reason why it is closing is because the program is not running anymore, simply add any sort of loop or input to fix this (or you could just run it through idle.)






                                                                      share|improve this answer













                                                                      The reason why it is closing is because the program is not running anymore, simply add any sort of loop or input to fix this (or you could just run it through idle.)







                                                                      share|improve this answer












                                                                      share|improve this answer



                                                                      share|improve this answer










                                                                      answered Jul 26 '18 at 2:38









                                                                      John. BJohn. B

                                                                      214




                                                                      214





















                                                                          0














                                                                          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:Python37python.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:Python37python.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_USERSoftwareClassesApplicationspython.exeshellopencommand


                                                                          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:Python37pythonw.exe" "C:Python37python_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_USERSoftwareClassesApplicationspython.exeshellopencommand]
                                                                          @=""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_USERSoftwareClassesApplicationspython.exeshellopencommand]
                                                                          @=""C:\Python37\python.exe" "%1""





                                                                          share|improve this answer























                                                                          • After going through all of that I discovered there's a much easier way. Assuming .py files are associated with python.exe through windows, just edit HKEY_CURRENT_USERSoftwareClassesApplicationspython.exeshellopencommand to C:WindowsSystem32cmd.exe /k python "%1". No extra python script needed.

                                                                            – BrianZ111
                                                                            Mar 8 at 23:55
















                                                                          0














                                                                          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:Python37python.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:Python37python.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_USERSoftwareClassesApplicationspython.exeshellopencommand


                                                                          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:Python37pythonw.exe" "C:Python37python_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_USERSoftwareClassesApplicationspython.exeshellopencommand]
                                                                          @=""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_USERSoftwareClassesApplicationspython.exeshellopencommand]
                                                                          @=""C:\Python37\python.exe" "%1""





                                                                          share|improve this answer























                                                                          • After going through all of that I discovered there's a much easier way. Assuming .py files are associated with python.exe through windows, just edit HKEY_CURRENT_USERSoftwareClassesApplicationspython.exeshellopencommand to C:WindowsSystem32cmd.exe /k python "%1". No extra python script needed.

                                                                            – BrianZ111
                                                                            Mar 8 at 23:55














                                                                          0












                                                                          0








                                                                          0







                                                                          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:Python37python.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:Python37python.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_USERSoftwareClassesApplicationspython.exeshellopencommand


                                                                          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:Python37pythonw.exe" "C:Python37python_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_USERSoftwareClassesApplicationspython.exeshellopencommand]
                                                                          @=""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_USERSoftwareClassesApplicationspython.exeshellopencommand]
                                                                          @=""C:\Python37\python.exe" "%1""





                                                                          share|improve this answer













                                                                          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:Python37python.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:Python37python.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_USERSoftwareClassesApplicationspython.exeshellopencommand


                                                                          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:Python37pythonw.exe" "C:Python37python_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_USERSoftwareClassesApplicationspython.exeshellopencommand]
                                                                          @=""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_USERSoftwareClassesApplicationspython.exeshellopencommand]
                                                                          @=""C:\Python37\python.exe" "%1""






                                                                          share|improve this answer












                                                                          share|improve this answer



                                                                          share|improve this answer










                                                                          answered Mar 8 at 7:03









                                                                          BrianZ111BrianZ111

                                                                          1




                                                                          1












                                                                          • After going through all of that I discovered there's a much easier way. Assuming .py files are associated with python.exe through windows, just edit HKEY_CURRENT_USERSoftwareClassesApplicationspython.exeshellopencommand to C:WindowsSystem32cmd.exe /k python "%1". No extra python script needed.

                                                                            – BrianZ111
                                                                            Mar 8 at 23:55


















                                                                          • After going through all of that I discovered there's a much easier way. Assuming .py files are associated with python.exe through windows, just edit HKEY_CURRENT_USERSoftwareClassesApplicationspython.exeshellopencommand to C:WindowsSystem32cmd.exe /k python "%1". No extra python script needed.

                                                                            – BrianZ111
                                                                            Mar 8 at 23:55

















                                                                          After going through all of that I discovered there's a much easier way. Assuming .py files are associated with python.exe through windows, just edit HKEY_CURRENT_USERSoftwareClassesApplicationspython.exeshellopencommand to C:WindowsSystem32cmd.exe /k python "%1". No extra python script needed.

                                                                          – BrianZ111
                                                                          Mar 8 at 23:55






                                                                          After going through all of that I discovered there's a much easier way. Assuming .py files are associated with python.exe through windows, just edit HKEY_CURRENT_USERSoftwareClassesApplicationspython.exeshellopencommand to C:WindowsSystem32cmd.exe /k python "%1". No extra python script needed.

                                                                          – BrianZ111
                                                                          Mar 8 at 23:55












                                                                          -1














                                                                          Well I got similar issue,
                                                                          It is solved by adding Environment Variable.



                                                                          Add System Variables in Window



                                                                          Name : PYTHONPATH



                                                                          Value : C:Python27;



                                                                          Your Python path.






                                                                          share|improve this answer



























                                                                            -1














                                                                            Well I got similar issue,
                                                                            It is solved by adding Environment Variable.



                                                                            Add System Variables in Window



                                                                            Name : PYTHONPATH



                                                                            Value : C:Python27;



                                                                            Your Python path.






                                                                            share|improve this answer

























                                                                              -1












                                                                              -1








                                                                              -1







                                                                              Well I got similar issue,
                                                                              It is solved by adding Environment Variable.



                                                                              Add System Variables in Window



                                                                              Name : PYTHONPATH



                                                                              Value : C:Python27;



                                                                              Your Python path.






                                                                              share|improve this answer













                                                                              Well I got similar issue,
                                                                              It is solved by adding Environment Variable.



                                                                              Add System Variables in Window



                                                                              Name : PYTHONPATH



                                                                              Value : C:Python27;



                                                                              Your Python path.







                                                                              share|improve this answer












                                                                              share|improve this answer



                                                                              share|improve this answer










                                                                              answered Dec 7 '16 at 12:12









                                                                              Krunal KapadiyaKrunal Kapadiya

                                                                              740831




                                                                              740831



























                                                                                  draft saved

                                                                                  draft discarded
















































                                                                                  Thanks for contributing an answer to Stack Overflow!


                                                                                  • Please be sure to answer the question. Provide details and share your research!

                                                                                  But avoid


                                                                                  • Asking for help, clarification, or responding to other answers.

                                                                                  • Making statements based on opinion; back them up with references or personal experience.

                                                                                  To learn more, see our tips on writing great answers.




                                                                                  draft saved


                                                                                  draft discarded














                                                                                  StackExchange.ready(
                                                                                  function ()
                                                                                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f12375173%2fhow-to-stop-python-closing-immediately-when-executed-in-microsoft-windows%23new-answer', 'question_page');

                                                                                  );

                                                                                  Post as a guest















                                                                                  Required, but never shown





















































                                                                                  Required, but never shown














                                                                                  Required, but never shown












                                                                                  Required, but never shown







                                                                                  Required, but never shown

































                                                                                  Required, but never shown














                                                                                  Required, but never shown












                                                                                  Required, but never shown







                                                                                  Required, but never shown







                                                                                  Popular posts from this blog

                                                                                  AWS Lex not identifying response if by a variable The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) The Ask Question Wizard is Live! Data science time! April 2019 and salary with experienceEnforcing custom enumeration in AWS LEX for slot valuesHow to give response based on user response in Amazon Lex?Intercepting AWS Lambda Response to a AWS Lex QueryLex chat bot error: Reached second execution of fulfillment lambda on the same utteranceamazon lex showing invalid responseLambda response send back to Lex slot?Response card in Amazon lexAmazon Lex - Lambda response return HTML to botHow can I solve 424 (Failed Dependency) (python) obtained from Amazon lex?

                                                                                  Алба-Юлія

                                                                                  Захаров Федір Захарович