bloomberg / memray

Memray is a memory profiler for Python
https://bloomberg.github.io/memray/
Apache License 2.0
13.17k stars 392 forks source link

memray attach command reports error "No module named 'memray'" #453

Closed EricLin89 closed 1 year ago

EricLin89 commented 1 year ago

Is there an existing issue for this?

Current Behavior

using memray attach command to attach to a running process, the command reports "No module named 'memray'" error. This happens when memray is installed after the process is started. After I restart the process and use memray attach again, the command works as expected.

Expected Behavior

memray attach works on processes started before memray is installed.

Steps To Reproduce

  1. start a python process.
  2. install memray and its dependencies.
  3. run memray attach $pid to the process

Memray Version

1.9.0

Python Version

3.7

Operating System

Linux

Anything else?

No response

godlygeek commented 1 year ago

Cannot reproduce.

$ ls
test.py
$ cat test.py
import time

message = ""
while True:
    message += "x"
    time.sleep(0.1)
$ python3.7 -m venv venv
$ . venv/bin/activate
(venv) $ python test.py &
[1] 27554
(venv) $ pip install memray
...
Installing collected packages: typing-extensions, pygments, mdurl, MarkupSafe, markdown-it-py, jinja2, rich, memray
Successfully installed MarkupSafe-2.1.3 jinja2-3.1.2 markdown-it-py-2.2.0 mdurl-0.1.2 memray-1.9.1 pygments-2.16.1 rich-13.5.2 typing-extensions-4.7.1
(venv) $ memray attach 27554
Memray WARNING: Correcting symbol for aligned_alloc from 0x7f724c9e57f0 to 0x7f724e18c000
(successfully shows a TUI, then when I press `q` I get:)
Failed to write output, deactivating tracking
(venv) $ 

Are you sure that the environment that you installed Memray into was the environment that the script you were trying to attach to was running with? Things ought to work just fine as long as you install Memray into a directory that's on the sys.path of the process you're attaching to, and in my testing they do.

EricLin89 commented 1 year ago

Thanks. I checked again and found the problem. In my environment, memray is installed under /usr/local/lib64/python3.7/site-packages by default. The directory does not exist before memray is installed. So the processes started before memray is installed does not have this path in sys.path. Installing memray into existing sys.path should solve the problem.