rkern / line_profiler

(OLD REPO) Line-by-line profiling for Python - Current repo ->
https://github.com/pyutils/line_profiler
Other
3.6k stars 256 forks source link

ModuleNotFound within virtualenv/ virtual environment #164

Open arthur-e opened 4 years ago

arthur-e commented 4 years ago

I'm trying to use kernprof with a script that is used in a virtual environment (created with virtualenv). This virtual environment uses some system-wide packages (i.e., with --system-site-packages) but some are also local to the virtual environment. It seems that kernprof can't locate these modules? In the example below, it can't find the cached_property module, but a quick pip freeze confirms that, indeed, this module is available.

$ kernprof -l myscript.py 
Wrote profile results to myscript.lprof
Traceback (most recent call last):
  File "/usr/local/bin/kernprof", line 11, in <module>
    sys.exit(main())
  File "/usr/local/lib/python3.6/dist-packages/kernprof.py", line 222, in main
    execfile(script_file, ns, ns)
  File "/usr/local/lib/python3.6/dist-packages/kernprof.py", line 35, in execfile
    exec_(compile(f.read(), filename, 'exec'), globals, locals)
  File "pyl4c/apps/soc.py", line 26, in <module>
    from cached_property import cached_property
ModuleNotFoundError: No module named 'cached_property'
$ pip freeze | grep cached
cached-property==1.5.1

I confirmed that switching a module from a local to a system-wide install fixed this issue; i.e., after removing cached_property from the virtual environment and installing system-wide with sudo -H pip install cached_property, kernprof moved on to complain about a different module.

Here are my stats:

Thanks!

xuezes commented 4 years ago

ran into the same problem today. For my case is that I can find two

kernprof: /home/user_name/.local/bin/kernprof /home/user_name/some/env/dirs/bin/kernprof

by running whereis kernprof. I suspect when one call kernprof the system prioritizes to call the first one, which is not the one in the virtual environment.

calling

/home/user_name/some/env/dirs/bin/kernprof some_code.py plus_args

(Or just simply delete the first one :-/) will resolve the problem.