Skip to content

debugpy ImportError: cannot import name module_from_spec from importlib.util #1972

@gswifort

Description

@gswifort

debugpy ImportError: cannot import name module_from_spec from importlib.util

Environment

  • OS: Windows 11
  • Python Version: 3.12.7
  • VS Code Version: Latest
  • debugpy Extension Version: 2025.16.0-win32-x64

Description

When trying to debug a Python file using the "Current File" debug configuration in VS Code, debugpy fails to start with an ImportError claiming it cannot import module_from_spec from importlib.util. However, manual testing shows that this function is available and works correctly in the same Python environment.

Steps to Reproduce

  1. Create a simple Python file (e.g., main.py with basic content)
  2. Set up VS Code with Python extension and debugpy
  3. Try to debug using "Current File" configuration (F5)
  4. Observe the ImportError

Expected Behavior

The debugger should start successfully and allow debugging of the Python file.

Actual Behavior

debugpy fails to start with the following error:

ImportError: cannot import name 'module_from_spec' from 'importlib.util' (c:\Users\GSWI\AppData\Local\Programs\Python\Python312\Lib\importlib\util.py)

Full Error Log

D:\Users\GSWI\Documents\debugpy-bug> cmd /C "C:\Users\GSWI\AppData\Local\Programs\Python\Python312\python.exe c:\Users\GSWI\.vscode\extensions\ms-python.debugpy-2025.16.0-win32-x64\bundled\libs\debugpy\launcher 63437 -- D:\Users\GSWI\Documents\debugpy-bug\main.py "
Traceback (most recent call last):
  File "C:\Users\GSWI\AppData\Local\Programs\Python\Python312\Lib\runpy.py", line 198, in _run_module_as_main
    return _run_code(code, main_globals, None,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\GSWI\AppData\Local\Programs\Python\Python312\Lib\runpy.py", line 88, in _run_code
    exec(code, run_globals)
  File "c:\Users\GSWI\.vscode\extensions\ms-python.debugpy-2025.16.0-win32-x64\bundled\libs\debugpy\launcher/../..\debugpy\__main__.py", line 69, in <module>
    from debugpy.server import cli
  File "c:\Users\GSWI\.vscode\extensions\ms-python.debugpy-2025.16.0-win32-x64\bundled\libs\debugpy\launcher/../..\debugpy/..\debugpy\server\__init__.py", line 7, in <module>
    import debugpy._vendored.force_pydevd  # noqa
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "c:\Users\GSWI\.vscode\extensions\ms-python.debugpy-2025.16.0-win32-x64\bundled\libs\debugpy\launcher/../..\debugpy/..\debugpy\_vendored\force_pydevd.py", line 44, in <module>
    preimport('pydevd', [
  File "c:\Users\GSWI\.vscode\extensions\ms-python.debugpy-2025.16.0-win32-x64\bundled\libs\debugpy\launcher/../..\debugpy/..\debugpy\_vendored\__init__.py", line 126, in preimport
    import_module(name)
  File "C:\Users\GSWI\AppData\Local\Programs\Python\Python312\Lib\importlib\__init__.py", line 90, in import_module  
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "c:\Users\GSWI\.vscode\extensions\ms-python.debugpy-2025.16.0-win32-x64\bundled\libs\debugpy\_vendored\pydevd\pydevd.py", line 49, in <module>
    from _pydevd_bundle import pydevd_utils
  File "c:\Users\GSWI\.vscode\extensions\ms-python.debugpy-2025.16.0-win32-x64\bundled\libs\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_utils.py", line 11, in <module>
    from importlib.util import module_from_spec, spec_from_file_location
ImportError: cannot import name 'module_from_spec' from 'importlib.util' (C:\Users\GSWI\AppData\Local\Programs\Python\Python312\Lib\importlib\util.py)

Proof that module_from_spec is Available

Manual verification shows that module_from_spec is indeed available in the same Python environment:

>>> import importlib.util as u
>>> u.__file__
'C:\\Users\\GSWI\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\importlib\\util.py'
>>> u.module_from_spec
<function module_from_spec at 0x0000028A14BC2AC0>

Debug Configuration Used

The issue occurs with the "Current File" debug configuration:

{
    "name": "Current File",
    "type": "debugpy",
    "request": "launch",
    "program": "${file}",
    "console": "integratedTerminal",
    "justMyCode": false,
}

Additional Context

  • The function module_from_spec has been available in importlib.util since Python 3.4
  • This appears to be an issue with how debugpy's bundled libraries are handling imports
  • The error occurs in the vendored pydevd component within debugpy
  • Regular Python execution works fine, only debugging is affected

Workaround

As a temporary solution, you can use remote debugging by manually starting debugpy from the command line:

  1. Run the following command in the terminal:

    python -m debugpy --listen 5678 --wait-for-client main.py
  2. Use the "Remote Attach" debug configuration in VS Code:

    {
        "name": "Remote Attach",
        "type": "debugpy",
        "request": "attach",
        "connect": {
            "host": "127.0.0.1",
            "port": 5678
        },
        "justMyCode": false
    }
  3. Start debugging using this configuration (F5) after running the terminal command.

This bypasses the launcher issue and allows debugging functionality to work normally.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions