Skip to content

Comments

Replace removed pkgutil.get_loader with importlib.util.find_spec in get_fullname#1998

Merged
rchiodo merged 2 commits intomainfrom
copilot/fix-python-subprocess-issue
Feb 23, 2026
Merged

Replace removed pkgutil.get_loader with importlib.util.find_spec in get_fullname#1998
rchiodo merged 2 commits intomainfrom
copilot/fix-python-subprocess-issue

Conversation

Copy link
Contributor

Copilot AI commented Feb 23, 2026

pkgutil.get_loader() was removed in Python 3.14, causing get_fullname() to silently return None for all module lookups. When subProcess: true, debugpy intercepts subprocess calls and uses get_fullname() to resolve -m <module> targets. The silent failure cascades to "No module named ..." for any single-file module run via subprocess.run([sys.executable, "-m", "mod"]).

Changes

  • pydevd_file_utils.py: Replace pkgutil.get_loader() with importlib.util.find_spec(), checking spec.has_location to exclude built-in/frozen module origins (e.g. "built-in", "frozen") that aren't file paths
  • test_convert_utilities.py: Add regression test for get_fullname() covering single-file modules, packages, and nonexistent modules
# Before (broken on 3.14+)
def get_fullname(mod_name):
    import pkgutil
    try:
        loader = pkgutil.get_loader(mod_name)  # AttributeError on 3.14
    except:
        return None
    ...

# After
def get_fullname(mod_name):
    try:
        import importlib.util
        spec = importlib.util.find_spec(mod_name)
        if spec is not None and spec.origin is not None and spec.has_location:
            return spec.origin
    except (ImportError, ModuleNotFoundError, ValueError):
        pass
    return None
Original prompt

This section details on the original issue you should resolve

<issue_title>Unable to run a Python module in a subprocess when subProcess: true</issue_title>
<issue_description>The following script executes a Python module in a subprocess.

# main.py
import subprocess
import sys

subprocess.run([sys.executable, "-m", "sub"])
# sub.py
print("Hello, world!")
// launch.json
{
    "version": "0.2.0",
    "configurations": [
    
        {
            "name": "Python Debugger: Current File",
            "type": "debugpy",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "subProcess": true,
        }
    ]
}

Running this in the debugger with "subProcess": true fails:

bash-3.2$  cd /Users/pete/projects/subprocess_test ; /usr/bin/env /Users/pete/projects/subprocess_test/.venv/bin/python /Users/pete/.vscode/extensions/ms-python.debugpy-2025.18.0-darwin-arm64/bundled/libs/debugpy/adapter/../../debugpy/launcher 56894 -- /Users/pete/projects/subprocess_test/main.py 
No module named sub

Running it with "subProcess": false works as expected:

bash-3.2$  cd /Users/pete/projects/subprocess_test ; /usr/bin/env /Users/pete/projects/subprocess_test/.venv/bin/python /Users/pete/.vscode/extensions/ms-python.debugpy-2025.18.0-darwin-arm64/bundled/libs/debugpy/adapter/../../debugpy/launcher 57618 -- /Users/pete/projects/subprocess_test/main.py 
Hello, world!

OS: Darwin arm64 25.3.0
VSCode: 1.109.5 (Universal)
Python Debugger: 2025.18.0
Python: 3.14.2 (in a virtualenv)</issue_description>

<agent_instructions>I can reproduce this problem. You'll likely need to write a test that reproduces the issue and then add logging to debugpy to figure out why it can find the 'sub' module.</agent_instructions>

Comments on the Issue (you are @copilot in this section)


🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.

…kgutil.get_loader

pkgutil.get_loader() was removed in Python 3.14, causing subprocess
module execution to fail with "No module named" when subProcess: true.
Replace with importlib.util.find_spec() which is the recommended
replacement and works across all supported Python versions.

Co-authored-by: rchiodo <19672699+rchiodo@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix inability to run Python module in subprocess Replace removed pkgutil.get_loader with importlib.util.find_spec in get_fullname Feb 23, 2026
Copilot AI requested a review from rchiodo February 23, 2026 17:54
@rchiodo
Copy link
Contributor

rchiodo commented Feb 23, 2026

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@rchiodo rchiodo marked this pull request as ready for review February 23, 2026 17:56
@rchiodo rchiodo requested a review from a team as a code owner February 23, 2026 17:56
@rchiodo rchiodo merged commit bc6fe51 into main Feb 23, 2026
28 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unable to run a Python module in a subprocess when subProcess: true

3 participants