3

When running kernprof:

kernprof -l script_to_profile.py

The output is stored in a binary file, which can be read in the terminal/command line.

Is there a way to output the results to a text file?

This seems like useful functionality to have, but can't find it in the documentation or in other posts.

PvK
  • 340
  • 2
  • 15
  • See [How do I use line_profiler (from Robert Kern)?](https://stackoverflow.com/questions/23885147/how-do-i-use-line-profiler-from-robert-kern). – martineau May 03 '19 at 16:38
  • Read the page you mentioned (before posting this question) -> but I don't think it has any mention of outputting to a text file. – PvK May 03 '19 at 16:48
  • The `kernprof` outputs to stdout, so redirect that to a file. – martineau May 03 '19 at 16:51
  • By default that output is a binary file (at least on my system) -> i.e. it needs to be read by issuing the command: "python -m line_profiler script_to_profile.py.lprof", which will print the contents to the command line. I am looking to output to a text file directly. What am I missing? – PvK May 03 '19 at 16:58
  • Sounds like they must have changed it. Unfortunately `pip install line_profiler` is [broken](https://github.com/rkern/line_profiler#id5) in the current release, so I can't install the latest version. Regardless, can't you do something like `python -m line_profiler script_to_profile.py.lprof > profile_output.txt`? – martineau May 03 '19 at 17:11
  • Amazing, that works. Thanks! – PvK May 08 '19 at 11:31
  • have posted your solution, if you post I'll mark yours as accepted – PvK May 08 '19 at 11:33
  • PvK: That's not necessary, but thanks for offering. Glad to hear you've found a solution. – martineau May 08 '19 at 12:26

1 Answers1

2
python -m line_profiler script_to_profile.py.lprof > profile_output.txt

Will convert the binary output to a text file (thanks @martineau)

PvK
  • 340
  • 2
  • 15