Search code examples
pythondebuggingpydev

PyDev attach to process fails with `ModuleNotFoundError: No module named 'add_code_to_python_process'`


I am trying to use PyDev to attach to a process on MS-Windows 10. Actually, to be more precise I was doing this. It worked wonderfully and I value it immensely, but now doesn't work and I wonder why.

I always do this to the same process, it is one written in C++ that loads a python interpreter internally to run Python plugin code. I have in past been happily breaking inside the plugin code and debugging with PyDev.

Come Dec 2020 and I try again and I get this error when trying to attach to the same process:

Traceback (most recent call last):
  File "E:\Util\Eclipse\dropins\PyDev 8.1.0\plugins\org.python.pydev.core_8.1.0.202012051215\pysrc\pydevd_attach_to_process\attach_pydevd.py", line 72, in <module>
    main(process_command_line(sys.argv[1:]))
  File "E:\Util\Eclipse\dropins\PyDev 8.1.0\plugins\org.python.pydev.core_8.1.0.202012051215\pysrc\pydevd_attach_to_process\attach_pydevd.py", line 41, in main
    import add_code_to_python_process
ModuleNotFoundError: No module named 'add_code_to_python_process'
Process finished with exitValue: 1

I last did this successfully around Oct 2020. I don't need this much. But when I do, it's ver very useful.

The error message isn't very helpful to me. I have searched on-line and through all my prior notes and found no clues.

Crucial of course in any "Was working, but isn't now" scenario is "what's changed". Well a good few things, not least the application I ma debugging is under constant development and I am regularly fetching new builds and running them. Also I have likely upgraded Eclipse and definitely upgraded PyDev since.

In fact suspecting some odd PyDev corruption I removed it and installed the latest one fresh using the Eclipse dropins method, which I have always used here and has worked fine.

The message suggests that this is failing on the Eclipse/PyDev side not the target process. Specifically that:

E:\Util\Eclipse\dropins\PyDev 8.1.0\plugins\org.python.pydev.core_8.1.0.202012051215\pysrc\pydevd_attach_to_process\attach_pydevd.py

tries on line 41 to import add_code_to_python_process and cant find the module, and that is an entirely PyDev internal issue it seems.

But checking the PyDev install:

E:\> tree "E:\Util\Eclipse\dropins\PyDev 8.1.0\plugins\org.python.pydev.core_8.1.0.202012051215\pysrc" /f
Folder PATH listing for volume DATA
Volume serial number is 646C-4A23
E:\UTIL\ECLIPSE\DROPINS\PYDEV 8.1.0\PLUGINS\ORG.PYTHON.PYDEV.CORE_8.1.0.202012051215\PYSRC
│   conftest.py
│   interpreterInfo.py
│   LICENSE
│   MANIFEST.in
│   pycompletionserver.py
│   pydevconsole.py
│   pydevd.py
│   pydevd_file_utils.py
│   pydevd_tracing.py
│   pydev_app_engine_debug_startup.py
│   pydev_coverage.py
│   pydev_pysrc.py
│   pydev_run_in_console.py
│   pytest.ini
│   README.rst
│   runfiles.py
│   setup.py
│   setup_cython.py
│
├───.github
│   │   install_and_run_debug_py.sh
│   │
│   └───workflows
│           pydevd-tests-python.yml
│
├───build_tools
│       build.py
│       build_binaries_osx.py
│       build_binaries_windows.py
│       check_no_git_modifications.py
│       generate_code.py
│       names_to_rename.py
│       pydevd_release_process.txt
│       rename_pep8.py
│
├───pydevd_attach_to_process
│   │   add_code_to_python_process.py
│   │   attach_amd64.dll
│   │   attach_linux_amd64.so
│   │   attach_linux_x86.so
│   │   attach_pydevd.py
│   │   attach_script.py
│   │   attach_x86.dll
│   │   attach_x86.dylib
│   │   attach_x86_64.dylib
│   │   inject_dll_amd64.exe
│   │   inject_dll_x86.exe
│   │   README.txt
│   │   run_code_on_dllmain_amd64.dll
│   │   run_code_on_dllmain_x86.dll
│   │   _always_live_program.py
│   │   _check.py
│   │   _test_attach_to_process.py
│   │   _test_attach_to_process_linux.py
│   │
│   ├───common
│   │       python.h
│   │       py_custom_pyeval_settrace.hpp
│   │       py_settrace.hpp
│   │       py_utils.hpp
│   │       py_version.hpp
│   │       ref_utils.hpp
│   │
│   ├───linux_and_mac
│   │       attach.cpp
│   │       compile_linux.sh
│   │       compile_mac.sh
│   │       lldb_prepare.py
│   │
│   ├───winappdbg
│   │   │   breakpoint.py
│   │   │   compat.py
│   │   │   crash.py
│   │   │   debug.py
│   │   │   disasm.py
│   │   │   event.py
│   │   │   interactive.py
│   │   │   module.py
│   │   │   process.py
│   │   │   registry.py
│   │   │   search.py
│   │   │   sql.py
│   │   │   system.py
│   │   │   textio.py
│   │   │   thread.py
│   │   │   util.py
│   │   │   window.py
│   │   │   __init__.py
│   │   │
│   │   ├───plugins
│   │   │       do_example.py
│   │   │       do_exchain.py
│   │   │       do_exploitable.py
│   │   │       do_symfix.py
│   │   │       README
│   │   │       __init__.py
│   │   │
│   │   └───win32
│   │           advapi32.py
│   │           context_amd64.py
│   │           context_i386.py
│   │           dbghelp.py
│   │           defines.py
│   │           gdi32.py
│   │           kernel32.py
│   │           ntdll.py
│   │           peb_teb.py
│   │           psapi.py
│   │           shell32.py
│   │           shlwapi.py
│   │           user32.py
│   │           version.py
│   │           wtsapi32.py
│   │           __init__.py
│   │
│   └───windows
│           attach.cpp
│           attach.h
│           compile_windows.bat
│           inject_dll.cpp
│           py_win_helpers.hpp
│           run_code_in_memory.hpp
│           run_code_on_dllmain.cpp
│           stdafx.cpp
│           stdafx.h
│           targetver.h
│
├───pydevd_concurrency_analyser
│       pydevd_concurrency_logger.py
│       pydevd_thread_wrappers.py
│       __init__.py
│
├───pydevd_plugins
│   │   django_debug.py
│   │   jinja2_debug.py
│   │   __init__.py
│   │
│   └───extensions
│       │   README.md
│       │   __init__.py
│       │
│       └───types
│               pydevd_helpers.py
│               pydevd_plugins_django_form_str.py
│               pydevd_plugin_numpy_types.py
│               __init__.py
│
├───pydev_ipython
│       inputhook.py
│       inputhookglut.py
│       inputhookgtk.py
│       inputhookgtk3.py
│       inputhookpyglet.py
│       inputhookqt4.py
│       inputhookqt5.py
│       inputhooktk.py
│       inputhookwx.py
│       matplotlibtools.py
│       qt.py
│       qt_for_kernel.py
│       qt_loaders.py
│       README
│       version.py
│       __init__.py
│
├───pydev_sitecustomize
│   │   sitecustomize.py
│   │   __not_in_default_pythonpath.txt
│   │
│   └───__pycache__
│           sitecustomize.cpython-38.pyc
│
├───stubs
│       pycompletion.py
│       _django_manager_body.py
│       _get_tips.py
│
├───third_party
│   │   cython_json.py
│   │   tests_cython_json.py
│   │
│   ├───isort_container
│   │   ├───backports
│   │   │       functools_lru_cache.py
│   │   │       __init__.py
│   │   │
│   │   └───isort
│   │           finders.py
│   │           hooks.py
│   │           isort.py
│   │           main.py
│   │           natural.py
│   │           pie_slice.py
│   │           pylama_isort.py
│   │           settings.py
│   │           utils.py
│   │           __init__.py
│   │           __main__.py
│   │
│   ├───pep8
│   │   │   autopep8.py
│   │   │   pycodestyle.py
│   │   │
│   │   └───lib2to3
│   │       └───lib2to3
│   │           │   btm_matcher.py
│   │           │   btm_utils.py
│   │           │   fixer_base.py
│   │           │   fixer_util.py
│   │           │   Grammar.txt
│   │           │   main.py
│   │           │   patcomp.py
│   │           │   PatternGrammar.txt
│   │           │   pygram.py
│   │           │   pytree.py
│   │           │   refactor.py
│   │           │   __init__.py
│   │           │   __main__.py
│   │           │
│   │           ├───fixes
│   │           │       fix_apply.py
│   │           │       fix_basestring.py
│   │           │       fix_buffer.py
│   │           │       fix_callable.py
│   │           │       fix_dict.py
│   │           │       fix_except.py
│   │           │       fix_exec.py
│   │           │       fix_execfile.py
│   │           │       fix_exitfunc.py
│   │           │       fix_filter.py
│   │           │       fix_funcattrs.py
│   │           │       fix_future.py
│   │           │       fix_getcwdu.py
│   │           │       fix_has_key.py
│   │           │       fix_idioms.py
│   │           │       fix_import.py
│   │           │       fix_imports.py
│   │           │       fix_imports2.py
│   │           │       fix_input.py
│   │           │       fix_intern.py
│   │           │       fix_isinstance.py
│   │           │       fix_itertools.py
│   │           │       fix_itertools_imports.py
│   │           │       fix_long.py
│   │           │       fix_map.py
│   │           │       fix_metaclass.py
│   │           │       fix_methodattrs.py
│   │           │       fix_ne.py
│   │           │       fix_next.py
│   │           │       fix_nonzero.py
│   │           │       fix_numliterals.py
│   │           │       fix_operator.py
│   │           │       fix_paren.py
│   │           │       fix_print.py
│   │           │       fix_raise.py
│   │           │       fix_raw_input.py
│   │           │       fix_reduce.py
│   │           │       fix_renames.py
│   │           │       fix_repr.py
│   │           │       fix_set_literal.py
│   │           │       fix_standarderror.py
│   │           │       fix_sys_exc.py
│   │           │       fix_throw.py
│   │           │       fix_tuple_params.py
│   │           │       fix_types.py
│   │           │       fix_unicode.py
│   │           │       fix_urllib.py
│   │           │       fix_ws_comma.py
│   │           │       fix_xrange.py
│   │           │       fix_xreadlines.py
│   │           │       fix_zip.py
│   │           │       __init__.py
│   │           │
│   │           └───pgen2
│   │                   conv.py
│   │                   driver.py
│   │                   grammar.py
│   │                   literals.py
│   │                   parse.py
│   │                   pgen.py
│   │                   token.py
│   │                   tokenize.py
│   │                   __init__.py
│   │
│   └───wrapped_for_pydev
│       │   not_in_default_pythonpath.txt
│       │
│       └───ctypes
│           │   ctypes-README.txt
│           │   util.py
│           │   wintypes.py
│           │   _ctypes.dll
│           │   _endian.py
│           │   __init__.py
│           │
│           └───macholib
│                   dyld.py
│                   dylib.py
│                   framework.py
│                   __init__.py
│
├───_pydevd_bundle
│   │   pydevconsole_code_for_ironpython.py
│   │   pydevd_additional_thread_info.py
│   │   pydevd_additional_thread_info_regular.py
│   │   pydevd_api.py
│   │   pydevd_breakpoints.py
│   │   pydevd_code_to_source.py
│   │   pydevd_collect_bytecode_info.py
│   │   pydevd_comm.py
│   │   pydevd_command_line_handling.py
│   │   pydevd_comm_constants.py
│   │   pydevd_console.py
│   │   pydevd_constants.py
│   │   pydevd_custom_frames.py
│   │   pydevd_cython.c
│   │   pydevd_cython.pxd
│   │   pydevd_cython.pyx
│   │   pydevd_cython_win32_27_32.pyd
│   │   pydevd_cython_win32_27_64.pyd
│   │   pydevd_cython_win32_36_32.cp36-win32.pyd
│   │   pydevd_cython_win32_36_64.cp36-win_amd64.pyd
│   │   pydevd_cython_win32_37_32.cp37-win32.pyd
│   │   pydevd_cython_win32_37_64.cp37-win_amd64.pyd
│   │   pydevd_cython_win32_38_32.cp38-win32.pyd
│   │   pydevd_cython_win32_38_64.cp38-win_amd64.pyd
│   │   pydevd_cython_win32_39_32.cp39-win32.pyd
│   │   pydevd_cython_win32_39_64.cp39-win_amd64.pyd
│   │   pydevd_cython_wrapper.py
│   │   pydevd_daemon_thread.py
│   │   pydevd_defaults.py
│   │   pydevd_dont_trace.py
│   │   pydevd_dont_trace_files.py
│   │   pydevd_exec.py
│   │   pydevd_exec2.py
│   │   pydevd_extension_api.py
│   │   pydevd_extension_utils.py
│   │   pydevd_filtering.py
│   │   pydevd_frame.py
│   │   pydevd_frame_utils.py
│   │   pydevd_import_class.py
│   │   pydevd_io.py
│   │   pydevd_json_debug_options.py
│   │   pydevd_net_command.py
│   │   pydevd_net_command_factory_json.py
│   │   pydevd_net_command_factory_xml.py
│   │   pydevd_plugin_utils.py
│   │   pydevd_process_net_command.py
│   │   pydevd_process_net_command_json.py
│   │   pydevd_referrers.py
│   │   pydevd_reload.py
│   │   pydevd_resolver.py
│   │   pydevd_safe_repr.py
│   │   pydevd_save_locals.py
│   │   pydevd_signature.py
│   │   pydevd_source_mapping.py
│   │   pydevd_stackless.py
│   │   pydevd_suspended_frames.py
│   │   pydevd_thread_lifecycle.py
│   │   pydevd_timeout.py
│   │   pydevd_traceproperty.py
│   │   pydevd_trace_api.py
│   │   pydevd_trace_dispatch.py
│   │   pydevd_trace_dispatch_regular.py
│   │   pydevd_utils.py
│   │   pydevd_vars.py
│   │   pydevd_vm_type.py
│   │   pydevd_xml.py
│   │   __init__.py
│   │
│   ├───_debug_adapter
│   │       debugProtocol.json
│   │       debugProtocolCustom.json
│   │       pydevd_base_schema.py
│   │       pydevd_schema.py
│   │       pydevd_schema_log.py
│   │       __init__.py
│   │       __main__pydevd_gen_debug_adapter_protocol.py
│   │
│   └───__pycache__
│           pydevd_comm_constants.cpython-38.pyc
│           pydevd_constants.cpython-38.pyc
│           pydevd_dont_trace.cpython-38.pyc
│           pydevd_frame_utils.cpython-38.pyc
│           pydevd_utils.cpython-38.pyc
│           pydevd_vm_type.cpython-38.pyc
│           __init__.cpython-38.pyc
│
├───_pydevd_frame_eval
│   │   pydevd_frame_evaluator.c
│   │   pydevd_frame_evaluator.cp38-win32.pyd
│   │   pydevd_frame_evaluator.cp38-win_amd64.pyd
│   │   pydevd_frame_evaluator.cp39-win32.pyd
│   │   pydevd_frame_evaluator.cp39-win_amd64.pyd
│   │   pydevd_frame_evaluator.pxd
│   │   pydevd_frame_evaluator.pyx
│   │   pydevd_frame_evaluator.template.pyx
│   │   pydevd_frame_evaluator_win32_36_32.cp36-win32.pyd
│   │   pydevd_frame_evaluator_win32_36_64.cp36-win_amd64.pyd
│   │   pydevd_frame_evaluator_win32_37_32.cp37-win32.pyd
│   │   pydevd_frame_evaluator_win32_37_64.cp37-win_amd64.pyd
│   │   pydevd_frame_eval_cython_wrapper.py
│   │   pydevd_frame_eval_main.py
│   │   pydevd_frame_tracing.py
│   │   pydevd_modify_bytecode.py
│   │   release_mem.h
│   │   __init__.py
│   │
│   ├───vendored
│   │   │   pydevd_fix_code.py
│   │   │   README.txt
│   │   │   __init__.py
│   │   │
│   │   └───bytecode
│   │       │   bytecode.py
│   │       │   cfg.py
│   │       │   concrete.py
│   │       │   flags.py
│   │       │   instr.py
│   │       │   peephole_opt.py
│   │       │   __init__.py
│   │       │
│   │       └───tests
│   │               test_bytecode.py
│   │               test_cfg.py
│   │               test_code.py
│   │               test_concrete.py
│   │               test_flags.py
│   │               test_instr.py
│   │               test_misc.py
│   │               test_peephole_opt.py
│   │               __init__.py
│   │
│   └───__pycache__
│           __init__.cpython-38.pyc
│
├───_pydev_bundle
│   │   pydev_console_utils.py
│   │   pydev_imports.py
│   │   pydev_import_hook.py
│   │   pydev_ipython_console.py
│   │   pydev_ipython_console_011.py
│   │   pydev_is_thread_alive.py
│   │   pydev_localhost.py
│   │   pydev_log.py
│   │   pydev_monkey.py
│   │   pydev_monkey_qt.py
│   │   pydev_override.py
│   │   pydev_umd.py
│   │   pydev_versioncheck.py
│   │   _pydev_calltip_util.py
│   │   _pydev_completer.py
│   │   _pydev_filesystem_encoding.py
│   │   _pydev_getopt.py
│   │   _pydev_imports_tipper.py
│   │   _pydev_jy_imports_tipper.py
│   │   _pydev_log.py
│   │   _pydev_tipper_common.py
│   │   __init__.py
│   │
│   └───__pycache__
│           pydev_is_thread_alive.cpython-38.pyc
│           pydev_localhost.cpython-38.pyc
│           pydev_log.cpython-38.pyc
│           _pydev_filesystem_encoding.cpython-38.pyc
│           _pydev_imports_tipper.cpython-38.pyc
│           _pydev_log.cpython-38.pyc
│           _pydev_tipper_common.cpython-38.pyc
│           __init__.cpython-38.pyc
│
├───_pydev_imps
│   │   _pydev_BaseHTTPServer.py
│   │   _pydev_execfile.py
│   │   _pydev_inspect.py
│   │   _pydev_pkgutil_old.py
│   │   _pydev_saved_modules.py
│   │   _pydev_SimpleXMLRPCServer.py
│   │   _pydev_SocketServer.py
│   │   _pydev_sys_patch.py
│   │   _pydev_xmlrpclib.py
│   │   __init__.py
│   │
│   └───__pycache__
│           _pydev_saved_modules.cpython-38.pyc
│           __init__.cpython-38.pyc
│
├───_pydev_runfiles
│       pydev_runfiles.py
│       pydev_runfiles_coverage.py
│       pydev_runfiles_nose.py
│       pydev_runfiles_parallel.py
│       pydev_runfiles_parallel_client.py
│       pydev_runfiles_pytest2.py
│       pydev_runfiles_unittest.py
│       pydev_runfiles_xml_rpc.py
│       __init__.py
│
└───__pycache__
        pydevd_file_utils.cpython-38.pyc

Then we can see that add_code_to_python_process.py is where it looks like it should be to import fine in attach_pydevd.py but by the by loooks like it always has.

Now I checked the target process too and the configured Python intereter, and I ensured that:

  1. The very same interpreter that my target porcess is running, is used. That target process comes bundled with its own python interpreter and uses its own site-packages folder etc. All no trouble, as I simply configure that interperter under the PyDev interpreters.
  2. I add the pysrcs directory tot he PYTHONPATH of that interpreter.

To illustrate (target app folder redacted out as it's not relevant):

PyDev Interpreter Settings

The nature of the error suggests a PyrDev configuration issue, that PyDev itself is not running right, but hard to see how, with a fresh install and everyhing visibly looking fine. PyDev (like any debugger) is a tad complicated, but I have read what I can and am none the wiser. It's not a bug in PyDev (or I'd file one) though, as it was working (with a prior version of PyDev I admit, as I have upgraded, but the PyDev upgrade came only as part of my effort to diagnose/fix this, in short this error ppeared first on the earlier version of PyDev I had installed with which I had on last efort (a month ago or s) connected PyDev to this same target process.

The real sticking point here is interpreting the traceback and error message.


Solution

  • It's really a bit odd that it doesn't find it given that it's alongside attach_pydevd.py and given that attach_pydevd.py is executed as a __main__ module it should (in theory) be able to find it... but practice it seems is sometimes different ;)

    So, try to do the following: open attach_pydevd.py and add sys.path.append(os.path.dirname(__file__)) as the first line of the def main(setup): to see if it fixes your issue (if it does, I'll also do the fix in the debugger side).