<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://entorb.net//wiki/index.php?action=history&amp;feed=atom&amp;title=Python_-_ipHelp.py</id>
	<title>Python - ipHelp.py - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://entorb.net//wiki/index.php?action=history&amp;feed=atom&amp;title=Python_-_ipHelp.py"/>
	<link rel="alternate" type="text/html" href="https://entorb.net//wiki/index.php?title=Python_-_ipHelp.py&amp;action=history"/>
	<updated>2026-05-06T10:27:28Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.1</generator>
	<entry>
		<id>https://entorb.net//wiki/index.php?title=Python_-_ipHelp.py&amp;diff=4841&amp;oldid=prev</id>
		<title>Torben at 20:21, 30 October 2024</title>
		<link rel="alternate" type="text/html" href="https://entorb.net//wiki/index.php?title=Python_-_ipHelp.py&amp;diff=4841&amp;oldid=prev"/>
		<updated>2024-10-30T20:21:12Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;[[Category:Coding]][[Category:Python]] &lt;br /&gt;
Attention: today this is easier:&lt;br /&gt;
 pip install ipython&lt;br /&gt;
 ...&lt;br /&gt;
 from IPython import embed  &lt;br /&gt;
 ...&lt;br /&gt;
 embed()  # to drop into iPython Shell from within the code&lt;br /&gt;
&lt;br /&gt;
Carsten Knoll wrote a nice module that improves the display of execeptions and allows to open an ipython shell at a certain point of the program code: See [https://github.com/cknoll/ipydex/tree/master]&lt;br /&gt;
 # ipHelp.py&lt;br /&gt;
 # by Carsten Knoll&lt;br /&gt;
 import sys&lt;br /&gt;
 import new&lt;br /&gt;
 __version__ = &amp;quot;0.2&amp;quot;&lt;br /&gt;
 try:&lt;br /&gt;
     from IPython.Shell import IPShellEmbed&lt;br /&gt;
     args = [&amp;#039;-pi1&amp;#039;,&amp;#039;In &amp;lt;\\#&amp;gt;: &amp;#039;,&amp;#039;-pi2&amp;#039;,&amp;#039;   .\\D.: &amp;#039;,&lt;br /&gt;
                 &amp;#039;-po&amp;#039;,&amp;#039;Out&amp;lt;\\#&amp;gt;: &amp;#039;,&amp;#039;-nosep&amp;#039;]&lt;br /&gt;
     class AdaptedIPSE(IPShellEmbed):&lt;br /&gt;
         def __init__(self, *args, **kwargs):&lt;br /&gt;
 &lt;br /&gt;
             IPShellEmbed.__init__(self, *args, **kwargs)&lt;br /&gt;
             old_interact = self.IP.interact # save the real method&lt;br /&gt;
             def new_interact(self, *args):&lt;br /&gt;
                 self.IP.user_ns.update({&amp;#039;_ips_exit&amp;#039;:False})&lt;br /&gt;
                 old_interact(*args) # call the real interact method&lt;br /&gt;
                 if self.IP.user_ns[&amp;#039;_ips_exit&amp;#039;]:&lt;br /&gt;
                     def do_nothing(*args, **kwargs):&lt;br /&gt;
                         pass&lt;br /&gt;
                     self.IP.interact = do_nothing&lt;br /&gt;
             self.IP.interact = new.instancemethod(new_interact, self,&lt;br /&gt;
                                                                   type(self))&lt;br /&gt;
     IPS= AdaptedIPSE(args,&lt;br /&gt;
                            banner = &amp;#039;Dropping into IPython&amp;#039;,&lt;br /&gt;
                            exit_msg = &amp;#039;Leaving Interpreter, back to program.&amp;#039;)&lt;br /&gt;
     def ip_syshook(pdb=0, mode=2):&lt;br /&gt;
         import IPython.ultraTB&lt;br /&gt;
         modus = [&amp;#039;Plain&amp;#039;, &amp;#039;Context&amp;#039;, &amp;#039;Verbose&amp;#039;][mode] # select the mode&lt;br /&gt;
         sys.excepthook = IPython.ultraTB.FormattedTB(mode=modus,&lt;br /&gt;
                                         color_scheme=&amp;#039;Linux&amp;#039;, call_pdb=pdb)&lt;br /&gt;
 &lt;br /&gt;
     def ip_extra_syshook(fnc, pdb=0, filename=None):&lt;br /&gt;
         assert callable(fnc)&lt;br /&gt;
         import IPython.ultraTB&lt;br /&gt;
         import time&lt;br /&gt;
         if not filename == None:&lt;br /&gt;
             assert isinstance(filename, str)&lt;br /&gt;
             pdb = 0&lt;br /&gt;
         ip_excepthook = IPython.ultraTB.FormattedTB(mode=&amp;#039;Verbose&amp;#039;,&lt;br /&gt;
                                         color_scheme=&amp;#039;Linux&amp;#039;, call_pdb=pdb)&lt;br /&gt;
         fileTraceback = IPython.ultraTB.FormattedTB(mode=&amp;#039;Verbose&amp;#039;,&lt;br /&gt;
                                         color_scheme=&amp;#039;NoColor&amp;#039;, call_pdb=0)&lt;br /&gt;
         def theexecphook (type, value, traceback):&lt;br /&gt;
             fnc()&lt;br /&gt;
             ip_excepthook(type, value, traceback)&lt;br /&gt;
             # write this to a File without Colors&lt;br /&gt;
             if not filename == None:&lt;br /&gt;
                 outFile = open(filename, &amp;quot;a&amp;quot;)&lt;br /&gt;
                 outFile.write(&amp;quot;--&amp;quot; + time.ctime()+&amp;quot; --\n&amp;quot;)&lt;br /&gt;
                 outFile.write(fileTraceback.text(type, value, traceback))&lt;br /&gt;
                 outFile.write(&amp;quot;\n-- --\n&amp;quot;)&lt;br /&gt;
                 outFile.close()&lt;br /&gt;
         sys.excepthook = theexecphook&lt;br /&gt;
     from IPython.Debugger import Tracer&lt;br /&gt;
     ST=Tracer()&lt;br /&gt;
 except ImportError:&lt;br /&gt;
     # create dummy functions&lt;br /&gt;
     def IPS():&lt;br /&gt;
         print &amp;quot;(EE): IPython is not available&amp;quot;&lt;br /&gt;
         pass&lt;br /&gt;
     def ip_syshook(*args):&lt;br /&gt;
         pass&lt;br /&gt;
 &lt;br /&gt;
     def ST():&lt;br /&gt;
         pass&lt;br /&gt;
 &lt;br /&gt;
Now include this file in your project and place IPS() somewhere in your code to open a shell there.&lt;br /&gt;
 from ipHelp import IPS, ip_syshook&lt;br /&gt;
 ip_syshook() # (1) -&amp;gt; debugger on exception&lt;br /&gt;
 # run IPS() somewhere to open ipython shell&lt;br /&gt;
&lt;br /&gt;
If you have are unsing PyQT and you get the message &amp;quot;QCoreApplication::exec: The event loop is already running&amp;quot;, inserting the following into your __init__ of your main class will help:&lt;br /&gt;
 QtCore.pyqtRemoveInputHook()&lt;/div&gt;</summary>
		<author><name>Torben</name></author>
	</entry>
</feed>