Wednesday, 27 January 2021

Can I run line_profiler on python module?

I have a module named my_module with the following structure.

.
└── my_module
    ├── main.py
    └── test.py

Here, I use python -m my_module.test, in order to run the test because it uses relative import.

Then how can I run line_profiler, memory_profiler on a module? (it can be pytest)

Followings are what I have tried

1st approach

python -m cProfile -m my_module.test   # works -> but I want line_profiler

2nd approach

import line_profiler
import unittest

profiler = line_profiler.LineProfiler()

class PageTester(unittest.TestCase):
  @profiler
  def test_abc(self):
    print(1)

if __name__ == "__main__"
  unittest.main(module='page.test')
  profiler.print_stats()           # doesn't print anything


from Can I run line_profiler on python module?

No comments:

Post a Comment