I have these files in mymodule
mymodule โโโ config.py โโโ __init__.py โโโ lib.py
With this simple content:
# config.py NAME = "Julius Cesar"
I can run it (and nothing happens) with python -m mymodule.lib
But I can not comment on it:
ยป python -m cProfile mymodule/lib.py 2 function calls in 0.000 seconds Ordered by: standard name ncalls tottime percall cumtime percall filename:lineno(function) 1 0.000 0.000 0.000 0.000 lib.py:1(<module>) 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects} Traceback (most recent call last): File "/usr/lib/python2.7/runpy.py", line 162, in _run_module_as_main "__main__", fname, loader, pkg_name) File "/usr/lib/python2.7/runpy.py", line 72, in _run_code exec code in run_globals File "/usr/lib/python2.7/cProfile.py", line 199, in <module> main() File "/usr/lib/python2.7/cProfile.py", line 192, in main runctx(code, globs, None, options.outfile, options.sort) File "/usr/lib/python2.7/cProfile.py", line 49, in runctx prof = prof.runctx(statement, globals, locals) File "/usr/lib/python2.7/cProfile.py", line 140, in runctx exec cmd in globals, locals File "mymodule/lib.py", line 1, in <module> from .config import NAME ValueError: Attempted relative import in non-package
So how can I cProfile library? Since the library does nothing, only importing the lib module will be profiled, but for me it is good enough. At this stage, I do not want to profile all function calls by simply importing the module.
How to do this with cProfile for modules with relative imports, avoiding ValueError s?