{"action":"opened","issue":{"url":"https://api.github.com/repos/fabioz/PyDev.Debugger/issues/136","repository_url":"https://api.github.com/repos/fabioz/PyDev.Debugger","labels_url":"https://api.github.com/repos/fabioz/PyDev.Debugger/issues/136/labels{/name}","comments_url":"https://api.github.com/repos/fabioz/PyDev.Debugger/issues/136/comments","events_url":"https://api.github.com/repos/fabioz/PyDev.Debugger/issues/136/events","html_url":"https://github.com/fabioz/PyDev.Debugger/issues/136","id":395105852,"node_id":"MDU6SXNzdWUzOTUxMDU4NTI=","number":136,"title":"attach_pydevd doesn't work from PyPI-installed pydevd","user":{"login":"chrahunt","id":2312275,"node_id":"MDQ6VXNlcjIzMTIyNzU=","avatar_url":"https://avatars2.githubusercontent.com/u/2312275?v=4","gravatar_id":"","url":"https://api.github.com/users/chrahunt","html_url":"https://github.com/chrahunt","followers_url":"https://api.github.com/users/chrahunt/followers","following_url":"https://api.github.com/users/chrahunt/following{/other_user}","gists_url":"https://api.github.com/users/chrahunt/gists{/gist_id}","starred_url":"https://api.github.com/users/chrahunt/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/chrahunt/subscriptions","organizations_url":"https://api.github.com/users/chrahunt/orgs","repos_url":"https://api.github.com/users/chrahunt/repos","events_url":"https://api.github.com/users/chrahunt/events{/privacy}","received_events_url":"https://api.github.com/users/chrahunt/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2019-01-01T19:18:00Z","updated_at":"2019-01-01T19:18:00Z","closed_at":null,"author_association":"NONE","body":"When the target process is running in a Python environment that does not have pydevd installed, then running attach_pydevd does not work.\r\n\r\n# Expected behavior\r\n\r\nRunning `pydevd_attach_to_process/attach_pydevd.py` on the same host as a target process should attach that process to a running debugger server.\r\n\r\n# Observed behavior\r\n\r\nWhen the target process does not already have `pydevd` in its python path, the operation fails with error `ModuleNotFoundError: No module named 'pydevd'`.\r\n\r\n# To reproduce\r\n\r\nSetup, running and successfully attaching to the target process when executed in the virtual environment that has pydevd installed\r\n\r\n```\r\n$ uname -a\r\nLinux host 4.19.11-041911-generic #201812191931 SMP Wed Dec 19 19:33:33 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux\r\n$ python3 -V\r\nPython 3.7.1\r\n$ pwd\r\n/tmp/pydevd-test\r\n$ cat test.py\r\nimport time\r\n\r\n\r\nif __name__ == '__main__':\r\n    while True:\r\n        time.sleep(1)\r\n$ python3 -m venv .venv\r\n$ . .venv/bin/activate\r\n(.venv) $ pip install pydevd\r\nCollecting pydevd\r\n  Using cached https://files.pythonhosted.org/packages/fe/47/77aaa3552aa638cb01c397fe0938b42ff995f2e1bdacd1041fdea7a2fedb/pydevd-1.4.0.tar.gz\r\nInstalling collected packages: pydevd\r\n  Running setup.py install for pydevd ... done\r\nSuccessfully installed pydevd-1.4.0\r\nYou are using pip version 10.0.1, however version 18.1 is available.\r\nYou should consider upgrading via the 'pip install --upgrade pip' command.\r\n(.venv) $ python test.py > output.txt 2>&1 &\r\n[1] 28897\r\n(.venv) $ sudo python /tmp/pydevd-test/.venv/pydevd_attach_to_process/attach_pydevd.py --pid 28897 --port 12345\r\nAttaching with arch: i386:x86-64\r\nRunning: gdb --nw --nh --nx --pid 28897 --batch --eval-command='set scheduler-locking off' --eval-command='set architecture i386:x86-64' --eval-command='call dlopen(\"/tmp/pydevd-test/.venv/pydevd_attach_to_process/attach_linux_amd64.so\", 2)' --eval-command='call (int)DoAttach(0, \"import sys;sys.path.append(\\\"/tmp/pydevd-test/.venv\\\");sys.path.append(\\\"/tmp/pydevd-test/.venv/pydevd_attach_to_process\\\");import attach_script;attach_script.attach(port=12345, host=\\\"127.0.0.1\\\");\", 0)' --command='/tmp/pydevd-test/.venv/pydevd_attach_to_process/linux/gdb_threads_settrace.py'\r\nRunning gdb in target process.\r\nstdout: [Thread debugging using libthread_db enabled]\r\nUsing host libthread_db library \"/lib/x86_64-linux-gnu/libthread_db.so.1\".\r\n0x00007f66247465b3 in __select_nocancel () at ../sysdeps/unix/syscall-template.S:84\r\nThe target architecture is assumed to be i386:x86-64\r\n$1 = (void *) 0x12e29b0\r\n[New Thread 0x7f661e31c700 (LWP 29021)]\r\n[New Thread 0x7f661db1b700 (LWP 29022)]\r\n[New Thread 0x7f661d31a700 (LWP 29023)]\r\n[New Thread 0x7f661cb19700 (LWP 29024)]\r\n$2 = 19371104\r\n$3 = 0\r\n$4 = 0\r\n$5 = 0\r\n$6 = 0\r\n$7 = 0\r\n[Inferior 1 (process 28897) detached]\r\n\r\nstderr: 84      ../sysdeps/unix/syscall-template.S: No such file or directory.\r\n\r\n(.venv) $ echo \"Connected OK\"\r\nConnected OK\r\n(.venv) $ echo \"Stopping from IDE\"\r\nStopping from IDE\r\n(.venv) $ cat output.txt\r\n(.venv) $ kill %1\r\n(.venv) $\r\n[1]+  Terminated              python test.py > output.txt 2>&1\r\n(.venv) $ deactivate\r\n```\r\n\r\nNow running the target process outside the virtual environment\r\n\r\n```\r\n$ python3 test.py > output.txt 2>&1 &\r\n[1] 29362\r\n$ . .venv/bin/activate\r\n(.venv) $ sudo python /tmp/pydevd-test/.venv/pydevd_attach_to_process/attach_pydevd.py --pid 29362 --port 12345\r\nAttaching with arch: i386:x86-64\r\nRunning: gdb --nw --nh --nx --pid 29362 --batch --eval-command='set scheduler-locking off' --eval-command='set architecture i386:x86-64' --eval-command='call dlopen(\"/tmp/pydevd-test/.venv/pydevd_attach_to_process/attach_linux_amd64.so\", 2)' --eval-command='call (int)DoAttach(0, \"import sys;sys.path.append(\\\"/tmp/pydevd-test/.venv\\\");sys.path.append(\\\"/tmp/pydevd-test/.venv/pydevd_attach_to_process\\\");import attach_script;attach_script.attach(port=12345, host=\\\"127.0.0.1\\\");\", 0)' --command='/tmp/pydevd-test/.venv/pydevd_attach_to_process/linux/gdb_threads_settrace.py'\r\nRunning gdb in target process.\r\nstdout: [Thread debugging using libthread_db enabled]\r\nUsing host libthread_db library \"/lib/x86_64-linux-gnu/libthread_db.so.1\".\r\n0x00007f330db825b3 in __select_nocancel () at ../sysdeps/unix/syscall-template.S:84\r\nThe target architecture is assumed to be i386:x86-64\r\n$1 = (void *) 0x2477ad0\r\n$2 = 37815664\r\n$3 = 9\r\n[Inferior 1 (process 29362) detached]\r\n\r\nstderr: 84      ../sysdeps/unix/syscall-template.S: No such file or directory.\r\n\r\n(.venv) $\r\n[1]+  Exit 1                  python3 test.py > output.txt 2>&1\r\n(.venv) $ cat output.txt\r\nTraceback (most recent call last):\r\n  File \"/tmp/pydevd-test/.venv/pydevd_attach_to_process/attach_script.py\", line 3, in attach\r\n    import pydevd\r\nModuleNotFoundError: No module named 'pydevd'\r\nTraceback (most recent call last):\r\n  File \"<frozen importlib._bootstrap>\", line 983, in _find_and_load\r\n  File \"<frozen importlib._bootstrap>\", line 965, in _find_and_load_unlocked\r\nModuleNotFoundError: No module named 'pydevd_tracing'\r\n\r\nThe above exception was the direct cause of the following exception:\r\n\r\nTraceback (most recent call last):\r\n  File \"test.py\", line 6, in <module>\r\n    time.sleep(1)\r\nSystemError: <built-in function sleep> returned a result with an error set\r\n```\r\n\r\nWe can see from the output that the following directories are added to the Python path:\r\n\r\n* \"/tmp/pydevd-test/.venv\"\r\n* \"/tmp/pydevd-test/.venv/pydevd_attach_to_process\"\r\n\r\nWe may want to try to get `pydevd_dirname` in `attach_pydevd.py` by importing `pydevd` and using `os.path.dirname(os.path.dirname(pydevd.__file__))` and then use the current value (`os.path.dirname(os.path.dirname(__file__))` as a fallback if `pydevd` is not importable."}}
{"actor":{"display_login":"chrahunt"}}