Search code examples
pythondockervisual-studio-codevscode-debugger

vscode attached to Docker container - module not found


I'm running vscode debugger inside Docker container but it started showing an error: enter image description here

That is very strange because when I open python shell in the same vscode window and import the same module import works just fine. So I need to find the reason why debugger doesn't see the modules

The full error code:

root@854c8a51d1f6:/opt/HonkioServer# python3 entrypoints/api2/docker_entry 
Traceback (most recent call last):
  File "entrypoints/api2/docker_entry", line 3, in <module>
    from index import app
  File "/opt/HonkioServer/entrypoints/api2/index.py", line 9, in <module>
    from honkio.db.Application import ApplicationModel
ModuleNotFoundError: No module named 'honkio'
root@854c8a51d1f6:/opt/HonkioServer#  cd /opt/HonkioServer ; /usr/bin/env /bin/python3 /root/.vscode-server/extensions/ms-python.python-2022.20.1/pythonFiles/lib/python/debugpy/adapter/../../debugpy/launcher 39239 -- /opt/HonkioServer/entrypoints/api2/docker_entry 
Traceback (most recent call last):
  File "/usr/lib/python3.8/runpy.py", line 194, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/usr/lib/python3.8/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/root/.vscode-server/extensions/ms-python.python-2022.20.1/pythonFiles/lib/python/debugpy/adapter/../../debugpy/launcher/../../debugpy/__main__.py", line 39, in <module>
    cli.main()
  File "/root/.vscode-server/extensions/ms-python.python-2022.20.1/pythonFiles/lib/python/debugpy/adapter/../../debugpy/launcher/../../debugpy/../debugpy/server/cli.py", line 430, in main
    run()
  File "/root/.vscode-server/extensions/ms-python.python-2022.20.1/pythonFiles/lib/python/debugpy/adapter/../../debugpy/launcher/../../debugpy/../debugpy/server/cli.py", line 284, in run_file
    runpy.run_path(target, run_name="__main__")
  File "/root/.vscode-server/extensions/ms-python.python-2022.20.1/pythonFiles/lib/python/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_runpy.py", line 321, in run_path
    return _run_module_code(code, init_globals, run_name,
  File "/root/.vscode-server/extensions/ms-python.python-2022.20.1/pythonFiles/lib/python/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_runpy.py", line 135, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "/root/.vscode-server/extensions/ms-python.python-2022.20.1/pythonFiles/lib/python/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_runpy.py", line 124, in _run_code
    exec(code, run_globals)
  File "/opt/HonkioServer/entrypoints/api2/docker_entry", line 3, in <module>
    from index import app
  File "/opt/HonkioServer/entrypoints/api2/index.py", line 9, in <module>
    from honkio.db.Application import ApplicationModel
ModuleNotFoundError: No module named 'honkio'

My debugger config file launch.json

    {
      "name": "Python: File",
      "type": "python",
      "request": "launch",
      "program": "/opt/HonkioServer/entrypoints/api2/docker_entry",
      "justMyCode": true
    }

Solution

  • Finally I found the source of this issue - it was in the file that directly imported to docker-entry file and modules from project directories were imported in a strange way:

    # This line adds to modules imports that added below cd two levels up
    sys.path.insert(0, os.path.dirname(__file__) + "/../..")
    
    import honkio
    

    However, right after I change this file in vs code, formatter changed lines order automatically placing that line at the bottom of all the other imports. Therefore file imports break and it shows module not found error