I am working on a script (script A), that needs to open a new Python IDLE shell, automatically run another script (script B) in it and then close it. The following code is what I use for this purpose:
import sys
sys.argv=['','-n','-t','My New Shell','-c','execfile("VarLoader.py")']
import idlelib.PyShell
idlelib.PyShell.main()
However I can't get the new shell close automatically. I have tried adding the following to script B but either it doesn't close the new shell or a windows pops up asking whether I want to kill it.
exit()
.
import sys
sys.exit()
Instead of monkeypatching or modifying the IDLE source code to make your program skip the prompt to exit I'd recommend you create a subclass of
PyShell
that overrides theclose
method how you want it to work:The original issue with this was that then using
idlelib.PyShell.main
would not use your subclass, you can in fact create a copy of the function - without modifying the original - by using theFunctionType
constructor that will use your modified class:Then you can run your extra IDLE shell like this:
Now you can leave IDLE the way it is and have your program work the way you want it too. (it is also compatible with other versions of python!)