I am running a python project in pycharm. In the code we have a main "try-catch-finally" block e.g.
try:
# Some stuff like opening files and video streams
except SomePossibleExceptions:
# Handle possible exception
finally:
# Save, close and tidy up unfinished files / videos / output streams
If I run the program in the terminal it will reach the finally block when I press our quit button or "ctrl-c" and perform the post processing required. However, after pressing "stop" when using the run tool in PyCharm it just quits and does not reach the finally block.
The obvious answer is to just run in the terminal but is there a way to get PyCharm to run the finally block after pressing "stop" in the run tool?
No, using the red stop button terminates the process immediately.
I think you alluded to this, but the only workaround I have found is to edit the run configuration:
run > edit configurations > tick the box for 'Emulate terminal in output console' or 'Run with Python Console'.
With either of those selections ticked, the run window will accept ctrl-C inputs which will allow your finally block to execute.
Related
I want to create a program that does something in which someone terminates the script by clicking the stop button in PyCharm. I tried
from sys import exit
def handler(signal_received, frame):
# Handle any cleanup here
print('SIGINT or CTRL-C detected. Exiting gracefully')
exit(0)
if __name__ == '__main__':
signal(SIGINT, handler)
print('Running. Press CTRL-C to exit.')
while True:
# Do nothing and hog CPU forever until SIGINT received.
pass
from https://www.devdungeon.com/content/python-catch-sigint-ctrl-c.
I tried on both Mac and Windows. On the Mac, PyCharm behaved as expected, when I click the stop button it catches the SIGINT. But on Windows, I did exactly the same thing, but it just straightly returns to me a
Process finished with exit code -1. Is there something I can do to change to make the Windows behave like what on Mac?
Any help is appreciated!
I don't think it's a strange question at all. On unix systems, pycham sends a SIGTERM, waits one second, then send a SIGKILL. On windows, it does something else to end the process, something that seems untrappable. Even during development you need a way to cleanly shut down a process that uses native resources. In my case, there is a CAN controller that, if not shut down properly, can't ever be opened again. My work around was to build a simple UI with a stop button that shuts the process down cleanly. The problem is, out of habit, from using pycharm, goland, and intellij, is to just hit the red, square button. Every time I do that I have to reboot the development system. So I think it is clearly also a development time question.
This actually isnt a simple thing, because PyCharm sends SIGKILL with the stop button. Check the discussion here https://youtrack.jetbrains.com/issue/PY-13316
There is a comment that you can enable "kill windows process softly", however it didnt work for me. The one that does work is emulate terminal in the debug config, then use control c when you select the console window
I am building a command line tools using Python script. it's a loop to check data and print out some stuff after some delay seconds. It works fine until I click anything or selecting text by mouse on the terminal without keyboard event. it doesn't do anything after that, doesn't print and recheck
import time
import sys
print('some thing')
for remaining in range(10, 0, -1):
sys.stdout.write("\r")
sys.stdout.write("recheck in {:2d}.".format(remaining))
sys.stdout.flush()
time.sleep(1)
sys.stdout.write("\rComplete! \n")
input()
My environment is anaconda 64bit on windows 10
The console is blocking in the Windows SDK function WriteConsole because the console window is in a mode called QuickEdit mode.
To fix the issue, go to the properties option in the upper left corner menu of the console.
Then uncheck QuickEdit mode.
QuickEdit mode is there to help with copying and pasting text from the console. So when the console is in that mode, it stops all writing to the console so that the text isn't moving while you are trying to select and copy/paste.
Python significantly changed its system signal handling in Python 3.5. https://www.python.org/dev/peps/pep-0475/
It used to throw an InterruptedError whenever a signal interrupted a system call. Now the system call wrapper code upon signal interruption will recall the system call recalculating any timeouts if necessary. A bug at this level could recall the system call with an absurdly long value.
Attach a debugger and see where the process is at when it is stuck.
EDIT: after attaching windbg to stuck console. I discovered that this isn't the problem. I posted the real solution in a new answer.
I wrote this piece of code and tried to debug it:
from time import *
for i in range (100):
sleep(1)
print(i)
I first run this script in debug mode, and try to pause it by clicking the pause button, but the pause button doesn't work at all, it just keep printing new numbers.
Then I directly run this script (not in the debug mode), the pause did stop pycharm from printing new numbers, but the script is actually still running in the background, when I resume the script, it prints a lot of numbers all of a sudden.
So how can I correctly pause the script execution?
I installed pycharm and python in a whole new windows 7, it still behaves like this.
The stop and rerun button works perfectly, breakpoints too. But the pause button never works.
The pause ("Pause Output") button only temporarily suspends output to the terminal - it has no effect on the script execution. You may wish to use debug mode with breakpoints instead.
You can add breakpoints into your program by clicking in the space to the left of the text editor (the "Left Gutter", where line numbers appear, if you have them enabled).
See the Pycharm documentation for more information.
Update 2022
We now have a pause button in debug mode :
While debugging my Python 3.5 pogram in Pycharm 5.0.4, I am trying to hit the pause button to find how why/where the program is hanging (as can be done in Visual Studio).
However, nothing happens: the pause button does not become grey and the resume button stays grey, and in the debugger tool window, "Frames are not available".
I tried with different basic programs, on Linux and on Windows, to no avail.
Is this a bug or am I missing something in how Pycharm debugging is supposed to work?
I also noticed that when a breakpoint is hit, only one thread is suspended and I could see no way to suspend other threads to inspect their stack frames. I would be interested to know how to achieve this thread-specific suspension as well.
Sounds like your program is hanging on a sleep or something of that sort, or maybe on some native code.
If it was a regular python loop the pause python would work.
I believe the problem is with python itself and not the debug tool you are using.
When you pause a python program you pause the interperter and so all threads that are running in the context of the interperter are paused and you can see the them in the frames window. Any thread that show the message "Frames not available in non-suspended state" is not suspended because it is was sleeping when you paused the program.
see this for how to debug c code
Not working python breakpoints in C thread in pycharm or eclipse+pydev
Within PyCharm, there is an option for debugging, that will allow you to step through your code, which may be of more use, rather than trying to pause the program.
You need to insert a break point in the code initially; just click in the grey bar at the line you want to break at:
Then you can press Alt+Shift+F9, or click Run > Debug in the menu to start stepping through the code from that point:
Once you have started the debug mode, click the button highlighted by the red circle - this will enable you to step through the code, looking at the variables, their assignments and if you receive any errors.
If you need to stop at any point, just hit the red Stop button on the left of the debug window.
The console tab will allow you to see what is being printed to the screen (if anything), and at what point, save you having loads of print statements like you would if debugging using Idle or similar IDEs
HTH
I have been writing a program in python using OpenCV. Up to this point, I had not set the mouse callback (cv2.setMouseCallback). To exit the program (which is in fullscreen), I would press the ESC key (Line 70).
I recently added a mouse callback (Line 11) which works as it should, however, now when I press the ESC key, the program does not terminate as it had previously. The while loop finishes, and cv2.destroyAllWindows() and sys.exit(0) are called. The window does close, and no python code after sys.exit(0) is executed, however no prompt is returned in Command Prompt (in which the python program was started).
My first thought was that there was a thread running that had not been stopped, however I have no threads in my code, and the thread that calls the onMouse function (Line 50) is the same as the main loop thread (i.e. it would not appear that opencv has a seperate thread for mouse callbacks).
My code can be found here: http://pastie.org/9246511
I am stumped, and any help is greatly appreciated.
Please note: You will need a webcam plugged in to test the code
Your code seems to exit correctly running it as a script from a prompt and running in Pycharm when using sys.exit().
If you are running it in Ipython you need to use exit() to return to the command prompt:
Just use exit() after cv2.destroyAllWindows() and it will terminate.
In [1]: type(exit)
Out[1]: IPython.core.autocall.ExitAutocall
Just incase you never got the answer...I just had the same issue. Turns out it was because in my my config file (Run/Debug Configurations) I had checked the "Show command line afterwards" box. Once I unchecked that the window was killed correctly.