I have a set of python scripts that I would like to profile with kernprof https://github.com/rkern/line_profiler , but I also want to be able to run this on normal execution without kernprof.
What is an elegant way to ignore undefined @profile at runtime without kernprof? Or any other decorator.
Code example:
@profile
def hello():
print('Testing')
hello()
Work with the:
kernprof -l test.py
The profiler correctly executes in @profile methods
Work with the:
python test.py
It returns an error:
Traceback (most recent call last):
File "test.py", line 1, in <module>
@profile
NameError: name 'profile' is not defined
I would like to avoid breaking this error everywhere, because I want the code to execute as if @profile was non-op when it was not called from kernprof.
Thanks! Laura
Edit: I ended up using cProfile with kcachegrind and generally avoided decorators.
cProfile KCacheGrind
python -m cProfile -o profile_data.pyprof run_cli.py
pyprof2calltree -i profile_data.pyprof && qcachegrind profile_data.pyprof.log