<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Dynamically print the Executing Line of Code? in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/dynamically-print-the-executing-line-of-code/m-p/390226#M30834</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Isn't that what a debugger is for?&lt;BR /&gt;&lt;BR /&gt;You could do this, but it's a terrible idea and will slow your program down by orders of magnitude:&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import inspect
import sys

def tracefunction(frame, event, arg):
&amp;nbsp;&amp;nbsp;&amp;nbsp; if event == "line":
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; info = inspect.getframeinfo(frame)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; fname, lineno, fn = info.filename, info.lineno, info.function
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; with open(fname, 'rb') as f:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; line = [line.rstrip() for line in f][lineno - 1]
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "Function: {} (in file {}:{}) | {}".format(fname, fn, lineno, line)
&amp;nbsp;&amp;nbsp;&amp;nbsp; return tracefunction

def registertracefunction():
&amp;nbsp;&amp;nbsp;&amp;nbsp; sys.settrace(tracefunction)

registertracefunction()

def mainfunction():
&amp;nbsp;&amp;nbsp;&amp;nbsp; for x in xrange(10):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print x * 5

if __name__ == "__main__":
&amp;nbsp;&amp;nbsp;&amp;nbsp; mainfunction()&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;That is, paste the code up through &lt;SPAN style="font-family:Courier New;"&gt;registertracefunction()&lt;/SPAN&gt; line to the top of your script and it will print out every line it's on.&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks Jason,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I figured that something like this would degrade performance. Debugging would be a good option except Python Addins don't lend themselves to debugging quite as well as something like a Script tool and I was really hoping to not have to resort to inserting print statements on every other line.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 11 Dec 2021 17:53:34 GMT</pubDate>
    <dc:creator>JohnDye</dc:creator>
    <dc:date>2021-12-11T17:53:34Z</dc:date>
    <item>
      <title>Dynamically print the Executing Line of Code?</title>
      <link>https://community.esri.com/t5/python-questions/dynamically-print-the-executing-line-of-code/m-p/390223#M30831</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Kind of an odd question and it may have implications that I'm not seeing, but I'm wondering if anyone knows of a little method to instruct python to print the executing line of code, either before or after it executes instead of inserting print statements, you know like...everywhere.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 27 Jan 2014 18:39:08 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/dynamically-print-the-executing-line-of-code/m-p/390223#M30831</guid>
      <dc:creator>JohnDye</dc:creator>
      <dc:date>2014-01-27T18:39:08Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamically print the Executing Line of Code?</title>
      <link>https://community.esri.com/t5/python-questions/dynamically-print-the-executing-line-of-code/m-p/390224#M30832</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Isn't that what a debugger is for?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;You could do this, but it's a terrible idea and will slow your program down by orders of magnitude:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;import inspect import sys&amp;nbsp; def tracefunction(frame, event, arg): &amp;nbsp;&amp;nbsp;&amp;nbsp; if event == "line": &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; info = inspect.getframeinfo(frame) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; fname, lineno, fn = info.filename, info.lineno, info.function &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; with open(fname, 'rb') as f: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; line = [line.rstrip() for line in f][lineno - 1] &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "Function: {} (in file {}:{}) | {}".format(fname, fn, lineno, line) &amp;nbsp;&amp;nbsp;&amp;nbsp; return tracefunction&amp;nbsp; def registertracefunction(): &amp;nbsp;&amp;nbsp;&amp;nbsp; sys.settrace(tracefunction)&amp;nbsp; registertracefunction()&amp;nbsp; def mainfunction(): &amp;nbsp;&amp;nbsp;&amp;nbsp; for x in xrange(10): &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print x * 5&amp;nbsp; if __name__ == "__main__": &amp;nbsp;&amp;nbsp;&amp;nbsp; mainfunction()&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;That is, paste the code up through &lt;/SPAN&gt;&lt;SPAN style="font-family:Courier New;"&gt;registertracefunction()&lt;/SPAN&gt;&lt;SPAN&gt; line to the top of your script and it will print out every line it's on.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 27 Jan 2014 19:35:30 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/dynamically-print-the-executing-line-of-code/m-p/390224#M30832</guid>
      <dc:creator>JasonScheirer</dc:creator>
      <dc:date>2014-01-27T19:35:30Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamically print the Executing Line of Code?</title>
      <link>https://community.esri.com/t5/python-questions/dynamically-print-the-executing-line-of-code/m-p/390225#M30833</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;The best option I know of is using trace. It will can create a file of the code executed. Here is an example.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;A href="http://pymotw.com/2/trace/"&gt;http://pymotw.com/2/trace/&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Profile also has some good options.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;A href="http://docs.python.org/2/library/profile.html"&gt;http://docs.python.org/2/library/profile.html&lt;/A&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 27 Jan 2014 19:46:24 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/dynamically-print-the-executing-line-of-code/m-p/390225#M30833</guid>
      <dc:creator>MathewCoyle</dc:creator>
      <dc:date>2014-01-27T19:46:24Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamically print the Executing Line of Code?</title>
      <link>https://community.esri.com/t5/python-questions/dynamically-print-the-executing-line-of-code/m-p/390226#M30834</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Isn't that what a debugger is for?&lt;BR /&gt;&lt;BR /&gt;You could do this, but it's a terrible idea and will slow your program down by orders of magnitude:&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import inspect
import sys

def tracefunction(frame, event, arg):
&amp;nbsp;&amp;nbsp;&amp;nbsp; if event == "line":
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; info = inspect.getframeinfo(frame)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; fname, lineno, fn = info.filename, info.lineno, info.function
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; with open(fname, 'rb') as f:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; line = [line.rstrip() for line in f][lineno - 1]
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "Function: {} (in file {}:{}) | {}".format(fname, fn, lineno, line)
&amp;nbsp;&amp;nbsp;&amp;nbsp; return tracefunction

def registertracefunction():
&amp;nbsp;&amp;nbsp;&amp;nbsp; sys.settrace(tracefunction)

registertracefunction()

def mainfunction():
&amp;nbsp;&amp;nbsp;&amp;nbsp; for x in xrange(10):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print x * 5

if __name__ == "__main__":
&amp;nbsp;&amp;nbsp;&amp;nbsp; mainfunction()&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;That is, paste the code up through &lt;SPAN style="font-family:Courier New;"&gt;registertracefunction()&lt;/SPAN&gt; line to the top of your script and it will print out every line it's on.&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks Jason,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I figured that something like this would degrade performance. Debugging would be a good option except Python Addins don't lend themselves to debugging quite as well as something like a Script tool and I was really hoping to not have to resort to inserting print statements on every other line.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 17:53:34 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/dynamically-print-the-executing-line-of-code/m-p/390226#M30834</guid>
      <dc:creator>JohnDye</dc:creator>
      <dc:date>2021-12-11T17:53:34Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamically print the Executing Line of Code?</title>
      <link>https://community.esri.com/t5/python-questions/dynamically-print-the-executing-line-of-code/m-p/390227#M30835</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;This may be completely obvious to you, apologies if it is, but when you are trying to debug a python addin, having the Geoprocessing&amp;gt; Python command line open in ArcMap while running the tool/button etc will show any error messages there.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 29 Jan 2014 01:41:54 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/dynamically-print-the-executing-line-of-code/m-p/390227#M30835</guid>
      <dc:creator>TimBarnes</dc:creator>
      <dc:date>2014-01-29T01:41:54Z</dc:date>
    </item>
  </channel>
</rss>

