50

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?

7
  • 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. Commented Sep 11, 2012 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 :) Commented Sep 11, 2012 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.) Commented Sep 11, 2012 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 :( Commented Sep 11, 2012 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) Commented Mar 24, 2017 at 14:26

18 Answers 18

32

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.

Sign up to request clarification or add additional context in comments.

2 Comments

This will cause the program to wait for user input except if you trigger an error in the line before your input()
In python 2.7.x use raw_input() instead of input().
27

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.

2 Comments

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

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

8 Comments

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".
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
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 :)
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.
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)"
|
6

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

C:\Python27\python.exe directoryToFile\yourprogram.py

1 Comment

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

If you just want a delay

from time import *

sleep(20)

Comments

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

Comments

2

I know a simple answer! Open your cmd, the type in: cd C:\directory your file is in and then type python your progam.py

1 Comment

Already explained in JMB2K's 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..."

Comments

1

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

Comments

0

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.

Comments

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.

Comments

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:\Python37\python.exe to the location of your python installation. Associate .pyw files to pythonw.exe (not python.exe) through Windows if they aren't already.

import subprocess
import sys

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

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

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

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

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

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

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

With ptyhon_cmd_k.pyw (change paths as needed):

Windows Registry Editor Version 5.00

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

Default version (change paths as needed):

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Software\Classes\Applications\python.exe\shell\open\command]
@="\"C:\\Python37\\python.exe\" \"%1\""

1 Comment

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_USER\Software\Classes\Applications\python.exe\shell\open\command to C:\Windows\System32\cmd.exe /k python "%1". No extra python script needed.
0

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

Go to 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

Comments

0

Just add an line of code in idle "input()"

3 Comments

What exactly do you mean? How would that work? Why would that solve the problem?
Generally, answers are much more helpful if they include an explanation of what the code is intended to do, and why that solves the problem without introducing others.
This duplicates Mike's answer from 2013.
0

Well I'm late but I will still answer. Most of the answers doesn't make any sense (to me)

First of all, there is no settings to make the file not close automatically. But, you can write a script to not allow the file to close automatically!

I usually do this(at the end of your script or print() ) :

notClose = "close"
while True:
    notClose = "dontClose"

This script makes the file keeps doing stuff(in this case, changing a variable string), so it doesn't close. You can change the variable name and string to something else, BUT don't make both of the string the same.

Or, if you want to, just use VS Code(Visual Studio Code). It allows you to run the file in the program and.. the program doesn't close, even when you get an error! (it will tell you what is the error, normally it instantly closes)

Comments

0

If you running from batch script, add pause to end of your cmd or bat file, something like:

python your_script.py
pause
  1. Add cmd /k before, it will say to run command line without closing it afterwards:

cmd /k python your_script.py

Comments

0

Using pythonw.exe instead of python.exe to launch works for me.

Comments

-3

Very simple:

  1. Open command prompt as an administrator.
  2. Type python.exe (provided you have given path of it in environmental variables)

Then, In the same command prompt window the python interpreter will start with >>>

This worked for me.

1 Comment

This is an answer to the wrong question; and the suggestion to use administrator privileges for no good reason is bad advice.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.