Docker容器内VSCode调试器报ModuleNotFoundError问题求助
问题:Docker容器内VSCode调试器无法识别honkio模块
在Docker容器内使用VSCode调试器运行代码时,提示ModuleNotFoundError: No module named 'honkio',但同一VSCode窗口的Python Shell却能正常导入该模块。
完整错误信息
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'
调试器配置文件launch.json
{ "name": "Python: File", "type": "python", "request": "launch", "program": "/opt/HonkioServer/entrypoints/api2/docker_entry", "justMyCode": true }
解决方法
统一Python解释器路径:调试器可能使用了与Shell不同的Python版本。在容器Shell中执行
which python3获取路径,然后在launch.json中添加pythonPath字段指定该路径:{ "name": "Python: File", "type": "python", "request": "launch", "program": "/opt/HonkioServer/entrypoints/api2/docker_entry", "pythonPath": "/usr/bin/python3", // 替换为Shell输出的路径 "justMyCode": true }设置工作目录与PYTHONPATH:确保调试器以项目根目录为工作目录,并将根目录加入Python模块搜索路径。修改launch.json如下:
{ "name": "Python: File", "type": "python", "request": "launch", "program": "/opt/HonkioServer/entrypoints/api2/docker_entry", "cwd": "/opt/HonkioServer", "env": { "PYTHONPATH": "/opt/HonkioServer" }, "justMyCode": true }验证模块合法性:确认
/opt/HonkioServer/honkio目录下存在__init__.py文件(空文件也可),保证Python能将其识别为合法模块。
内容的提问来源于stack exchange,提问作者Alexander P
相关产品推荐
相关产品推荐

