How to stop Python program execution in IDLE - python

I have a python script that uses plt.show() as it's last instruction. When it runs, IDLE just hangs after the last instruction. I get the image but I don't get the prompt back.
On other scripts I typically use ctrl-c to break the program (sometimes doesn't work immediately) but how do I get the prompt back with the plt.show()? Ctrl-c doesn't work...
Are there other ways to stop the program?
This is IDLE on Windows, if it makes any difference.

I have seen this problem with IDLE and matplotlib when using them on Windows. I don't know the exact cause, but Ctrl-c a couple times has typically worked for me. If that doesn't work for you, you can use the normal interpreter instead of write your plot directly to a file instead of the screen.
This is one of those (plentiful) times when IDLE doesn't behave like a normal Python script or interpreter session. Because of this, I usually avoid IDLE.

Ctrl+F6
(Restart shell)
or Shell->Restart Shell

When you use plt.show(), the python subprocess enters the GUI toolkit's event loop and blocks until the event loop exits. When it exits, you get the prompt back.
If you are using the TkAgg backend, you'll need to move your mouse over a figure after you press Ctrl+C. That will cause the event loop to stop. (Tkinter has its quirks)
Alternatively, IdleX offers Matplotlib support with IDLE using the EventLoop.py extension. You can display and interact with figures without using plt.show(). Just be sure to set plt.interactive(True) before generating figures.

I had same issue in Canopy Python Editor, and I was able to interrupt python session with CTRL+. ("dot" button). Hope that helps, or they probably do things in a similar ways

Related

How do you pause a python script on input at any moment while it's running?

I'm using selenium to do some webdriver stuff and I'd like to pause / resume it at any time on input of the enter key while I'm running it in anaconda prompt.
I know you can use
input()
at a certain point to make it wait for you to input but I can't figure out how to make it so that anytime I input it pauses and then again input to resume.
Instead of trying to achieve this via Python changes, I'd suggest doing it through whatever command prompt / terminal you're using the run the script.
On Linux or Mac systems, you can press ctrl+Z in the terminal while your program is running to make it pause. Then you could type fg when you want to resume.
Sounds like you're using Windows, which is too bad because I don't know such an easy fix. I did a bit of Googling and saw some people talking about using "Process Explorer" to suspend processes, e.g. here, so you could try looking there if you want.
Anyway, I'll still leave this answer up because it could directly solve the problem for a future searcher who's running Linux or Max, but it seems like you might need to wait for help from someone with more Windows experience than me. Good luck :/

Prevent Python shell close

How can I prevent a Python shell/commandline/terminal window from beeing closed, if the user clicks the close button of the Window? I've tried to catch SystemExit exceptions and overriding sys.exit(), no success.
I know, I can do this with a GUI toolkit/framework like PyQt, but importing that (and including it in the final binary of my program) would be no option while it's too big.
It is not possible to do this in the general case.
The terminal window is (generally speaking) a separate process from Python itself (YMMV with IPython and other such things, but this is true if you're running regular Python inside a regular terminal). When the terminal window decides to go away, it is not possible (under most reasonable operating systems and window managers) to prevent it from doing so, because it's a totally different program which just happens to be displaying your input and output.
You can, however, keep your program running after the terminal closes. The terminal will still go away, but the program will keep running in the background. On Unix systems, this is generally done by catching or ignoring the SIGHUP signal using signal.
Depending on your operating system and choice of terminal emulator, it may or may not be possible to configure the terminal to not close. Some terminal emulators under Linux will, by default, warn the user that there is a process running under the terminal when the user tries to close it. This is highly platform-dependent and can't be configured (portably or perhaps at all) from within Python.

pydev interactive console always disappearing and other console questions

I've just started using pydev in Eclipse and I have a lot of questions about the way the interactive console works.
I have found out how to launch an interactive console and use it to run functions. My questions are this:
Every time I change my code and re-run it, my interactive console disappears. This is annoying as I have to reopen a console and I can't see/rerun my previous history. If I pin it, it stays, but then I can't run any code. Is there any way to keep the interactive console open all the time, but also be able to run your code? I currently spend a significant amount of my day closing and opening consoles!
How can I run a function from the interactive console, but still get the debugger to stop at breakpoints. I can use the debugger if I am running the code from a main function, but if I run the code from the console, it just skips right over break points.
thanks
Niall
Instead of clicking in "Python Run", you may press Ctrl+Alt+Enter with the desired Python file active, and the console will call execfile on it. All your previous story stays there. You can also select some part of your code and run only it.
As far as I know, you can't. Check the pdb module instead.
Not sure if the source of the problem is the same, but I encountered a similar issue in which the PyDev console kept disappearing as soon as I clicked in the script editor, even if the console's Pin Console button was clicked.
I solved this simply by double-clicking on the script's tab, which allowed the console to stay visible at all times.

Running a python app alongside the live interpereter

I'm making a drawing program with python and pygame. I am trying to incorporate a script-fu thing in which the program opens a python live interpreter upon startup and allows the user to execute commands in the interpreter alongside the graphical interface.
My current strategy is to run the main loop inside its own thread, then have the application opened using a bash script that does 'python -i main.py'
Is this a safe/effective/ideal way of doing this? How can I use locks to ensure that commands coming in from the interpreter are executed between main loop iterations?
This is my first time using threads, so please explain to me like I am 7.
Thanks :)
The interpreter won't cooperate with locks you set (since it doesn't know about them). Thus, you cannot guarantee when the code entered by the user will execute.
Consider using the code module to build your own interactive console (it's really easy!). Then you can do the locking every time you go to execute user input.
Why are you using a third party live interpreter? Do you realize that pygame comes with one built in? The documentation is here. This will eliminate all of your problems quite easily.

python sleep == IDE lock up

When my script sleeps for 50sec my IDE locks up which is very annoying. I cant switch tabs, look through my source, type code, etc. It happens in pylde and pyscripter, i havent tried other IDEs. What can i do to fix this? i'm actually doing
for i in range(0, timeInSeconds): time.sleep(1)
hoping the IDE will update once per second but it doesnt look that way. What can i do to fix this?
I'm assuming you are running your code from within the IDE?
Your IDE is probably blocking while running your code. Look for a setting of some sort which might control that behaviour, otherwise I think your only choice would be to change IDE. (Or, run your code from outside the IDE)
Can you configure to run your script externally? I don't know about the specific IDEs, but I would try to spawn a different process for the debugged script and not run them under the IDE. If that doesn't help, then it is a problem of the IDEs.
The problem is your IDE not python. I don't use sleep that often, I've just tried it on the Eric IDE and you can use your IDE while your code is running, and sleeping. If can't set your IDE to do so and you need it then consider to change IDE or to run your code from console.
Personally, I think you should never ever ever execute code in the same loop as your IDE. Since most IDEs run a GUI mainloop, blocking this will cause complete freeze of the user interface. It is just asking for trouble, and I would take out bug reports against both those IDEs.
I suspect the problem the IDE is sitting in a loop waiting for the script to finish.
That in itself is not a problem, provided any user generated messages are still processed while the IDE is in this loop.
But what I suspect is going wrong in this case is the IDE is just running the loop without processing and messages and hence the user interface appears to be locked.
The IDE would need to be changed to either process GUI messages while in the loop or alternatively it needs to create a thread to run the the script. The thread would then run in the background and the GUI would remain responsive.
For example the Zeus for Windows IDE uses the background thread approach and it does not have this problem.

Categories

Resources