Exit Python script run from Eclipse - python

I know that the way to terminate a Python script is with raise SystemExit or with sys.exit() which is the same eventually.
However when I run it in Eclipse-Pydev (Ctrl+F11) and I exit from it, the program seems to be exiting, all windows are disappearing, put the Python process is still there in the task manager. The Terminate and Terminate All buttons remain active in Eclipse too. Pressing one of those kills it properly.
My script is terminating just fine if it has been run from the windows command prompt.
What could be the problem here?

Related

How to run a function before closing the shell after pressing OK on the Kill prompt in Python 3.7.4?

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

"Unable to find thread for evaluation." while running an external program in Visual Studio Code

I want to remote control 'my_program' using os.
import os
os.system(my_program)
While in debug mode in VS-Code i can start the 'my_program', but as soon as it opens, i've no available thread in VS-Code to work with. Or atleast that's what i'm interpreting by the message 'Unable to find thread for evaluation.'. I can not execute any commands in the debug console anymore, for instance 3+3, which should output 6.
As soon as i manually kill 'my_program' by simply closing it in the GUI, i can continue debugging.
In Short:
What i want: Open, use and Close 'my_program' with VSCode using os.system
What i get: Program opens, but VS debugging is somewhat offline, since it is 'Unable to find thread for evaluation.' and i can not continue debugging/closing the program via os.system
os.system("TASKKILL /F /IM my_program.exe")
EDIT: i can reproduce the same behaviour (freezed debugging console) using
subprocess.call(my_program)
I had the same error message. The introspection just works when the debugger is stopped at a breakpoint. I've put a breakpoint in the code, stopped in it and everything works fine.
It works with
subprocess.Popen(my_program)
subprocess.call and os.system block the terminal as long as the process being called is finished, thus 'freezing' the thread and debug window. subprocess.popen on the other hand is an asynchronous call which let's you interact with the terminal while the called process keeps running in the background

function that stop the script in python like stop() in R

I am writing a script in python and I would like to stop the scipt in the middle to see the objects and things. I used sys.exit() in pyscripter and everything is fine, but when I switch to spyder, the python interpreter is killed and I cannot read anything after the script is stopped.
How to fix it?

os.system('exit') in python

My friend is in a macOS environment and he wanted to call os.system('exit') at the end of his python script to make the terminal close. It doesn't. This doesn't surprise me but I would like to know what exactly is going on between the python script and the terminal when this call is made.
In my mental simulation the terminal should have to tell you that there are still running jobs, but that doesn't happen either.
As a side question : will some less common terminals close when a process calls this?
read the help:
Execute the command (a string) in a subshell.
A subshell is launched, and exit is run in that subshell.
To exit the enclosing terminal, you have to kill the parent. One way to do it is:
os.system("kill -9 %d"%(os.getppid())
The system function starts another shell to execute a command. So in this case your Python scripts starts a shell and runs "exit" command in there, which makes that process exit. However, the Python script itself, including a terminal where it is running, continues to run. If the intent is to kill the terminal, you have to get the parent process ID and send a signal requesting it to stop. That will kill both Python script and a terminal.
Remember that system first spawns/forks a sub-shell to execute its commands. In effect, you are asking only the sub-shell to exit.

How does one kill a python script without killing iPython?

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

Categories

Resources