line_profiler icon indicating copy to clipboard operation
line_profiler copied to clipboard

no support for profiling code at top level of script/module

Open jmuhlich opened this issue 10 years ago • 5 comments

I write lots of exploratory data processing scripts with a lot of code at the top level outside any function, and I desperately want to profile this code with line_profiler. I came up with a sort of a monkey patch solution to allow profiling top-level code which I've put into a gist here:

https://gist.github.com/jmuhlich/b6e349c5d80ec3897a77

I would be willing to work up a pull request if it would be considered.

jmuhlich avatar Jun 03 '15 16:06 jmuhlich

Oops, just realized this is sort of a duplicate of #3 .

jmuhlich avatar Jun 03 '15 17:06 jmuhlich

According to this comment it is not a duplicate at all.

lucasb-eyer avatar Sep 10 '16 20:09 lucasb-eyer

Hi, all. I still confused about this situation. I have the main function a in file a.py, and it another function b in b.py. So I add the @profile to b function. But when I run the kernprof -l -v a.py. There is not the detail of b function. Is the same problem with this issue? Is it solved?

Lan1991Xu avatar Jan 13 '17 16:01 Lan1991Xu

Unrelated.

rkern avatar Jan 13 '17 18:01 rkern

@jmuhlich Nice hack, thanks. Worked for me. I feel like I proposed adding a similar feature to line_profiler ages ago, but never ended up going anywhere with it; I forget what happened there.

Another use case for this is debugging import times of modules in large packages (see: sagemath). Understanding what causes slow import times can be non-trivial in some such packages.

embray avatar Dec 17 '18 11:12 embray