I wrote a script in python with a wx.Frame, and it has an exit function that calls sys.exit() when the user has clicked the close button. I'd like to be able to run this script from iPython, but when the user clicks the close button, sys.exit() kills the running python script as well as iPython. What could I use in place of sys.exit() to kill only the python script, not iPython?
Thanks!
I am not familiar with iPython but with a little searching I found a page:
http://ipython.scipy.org/moin/InterupptingThreads, I infer from this that all you might have to do is "raise SystemExit".
Related
I have some Python scripts which I am running in Windows using Command Prompt by entering the following command: python -m scriptname.
I have noticed that quite often, these scripts randomly pause and stop running. I can resume them by typing any key in Command Prompt, but I have no idea why this is happening or how to prevent it all together. Has anybody encountered a similar problem, and does anybody have any suggestions they may be able to offer?
Right click on the cmd window, and click on properties. Check if you have QuickEdit Mode on. If you do, uncheck that box.
This is a known issue for command prompt that if you click on the window, it will enter select mode, which will pause the program.
I've been running some simple python scripts in Pycharm as I develop an app. The below described problem happens with every app within this project, while in other projects I'm not seeing this.
When I run the script, even if it's just a simple
print('Hello World.')
I do not get an exit code. It looks as if I am running the program from the Python Console, but I'm running it by right clicking on the script and choosing run.
I can rerun the script without issue, a new tab just opens in the python console window with an incremental (n) next to the filename.
What I end up doing is clicking on the red stop button which is when I do finally get the exit code. I then close the tab and move on.
In your "Run/Debug Configurations" choose "Emulate terminal ..."
Select emulate terminal in debug configs
I have a python script running in the shell (3.7.4). While it's still running, if I want to close it, a kill prompt pops ups asking me "Your program is still running Do you want to kill it?". If I click OK, it stops executing. But I want to execute a function after pressing OK and before Killing the program. How to implement this?
I'm new to Python. Started using it from last week. So no idea where to begin.
If I click OK on the kill prompt I want to run something like this:
myobj.Uninitialize()
and then close the execution
I've had an issue, more of a nuisance, with python 3.6 specifically that I cant find an answer to after searching for it, and thinking I would be clever by looking for a python config file.
Whenever I run a script and that script quits, my command line pops open with an interactive console for that script, even though I've added exit(); quit() and tried just quit() to my script. I don't want to be forced to type quit() into the CLI every time I run my script. Is there a way to disable this functionality of 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