1

I guess this is kind of a weird question, but let's say you run a code in python that does something computationally expensive, like image processing. Oh I'm running Ubuntu 12.04 by the way. So I'm running a code, and open another terminal and type top to see what's doing what. This is ok as it tells me that python is doing its job, but what if I want to see which line is being run on the code? Is this possible? More importantly is it worth it to get this information? I can post a sample code of some of the processing if necessary

faskiat
  • 689
  • 2
  • 6
  • 21
  • 1
    From within the code the `inspect` module may provide you some introspection capabilities. Otherwise, maybe look at `heapy`? – g.d.d.c Aug 29 '13 at 17:09
  • heapy looks promising too. Thank you – faskiat Aug 29 '13 at 17:15
  • Also have a look at the `trace` and `pdb` modules. Here's a related answer I recently added elsewhere: http://stackoverflow.com/a/16003844/1025391 – moooeeeep Aug 29 '13 at 17:18

3 Answers3

2

Don't blink, unless your "line of code" is unbelievably slow there is no way for such a thing to be useful. What you probably want is a Python Profiler. I suggest you start looking in http://docs.python.org/2/library/profile.html for info related to profiling your python code.

Gary Walker
  • 8,831
  • 3
  • 19
  • 41
  • This seems promising. But yeah I see what you mean, line by line may be completely useless to the naked eye. Thanks – faskiat Aug 29 '13 at 17:14
1
  1. It usually is very slow but you can trace you code:

    python -m trace --count -C . somefile.py ...
    
  2. More manual but traditional way is logging: you can insert print statements before and after slow operations.

  3. You can find slow places in you code using a profiler.

  4. And you can run your code step by step with a debugger. Just insert import pdb; pdb.set_trace() (or ipdb if you like ipython) before slow operation.

Leonid Shvechikov
  • 3,927
  • 2
  • 17
  • 14
0

This is the classic use case for a debugger. Have a look at Eclipse with the PyDev plugin, which is an IDE for Python with a useful debugger integration.

For example, a debugger allows you to set breakpoints where the execution will stop in order to let you manually step through the relevant lines of code to see how it goes. At the same time, you can inspect the variables' contents. You will thereby get a better understanding of what is happening, where and why it fails, and so on.

Go and get yourself a debugger!

moooeeeep
  • 31,622
  • 22
  • 98
  • 187