Skip to content

debugpy ignores isolated mode (-I) #1916

Closed

Description

Before creating a new issue, please check the FAQ to see if your question is answered there.

Environment data

  • debugpy version: 1.8.14
  • OS and version: Debian 12, Mac OS 15.5
  • Python version (& distribution if applicable, e.g. Anaconda): python3.9, python3.11
  • Using VS Code or Visual Studio: VS Code

Actual behavior

sys.path contains current script directory in isolated mode (-I) with debugpy.

Relevant code is in cli.py, looks like debugpy should check for isolated mode before doing sys.path.insert(0, dir)

Expected behavior

debugpy respects isolated mode and sys.path under debugpy running in isolated mode doesn't contain current script directory

Steps to reproduce:

sys.path contains directory of the script being run in isolated mode with debugpy:

python -I -m debugpy --listen 5678 -c 'import sys; print(f"{sys.path[0]=!r}")'
sys.path[0]=''

sys.path doesn't contain script directory in isolated mode without debugpy:

python -I -c 'import sys; print(f"{sys.path[0]=!r}")'
sys.path[0]='/opt/homebrew/Cellar/[email protected]/3.9.22/Frameworks/Python.framework/Versions/3.9/lib/python39.zip'

Activity

  1. added
    bug
    on Feb 5, 2026
  2. karandhaodiyal28-hash commented on Jul 30, 2026

    @karandhaodiyal28-hash
    Contributor

    Reproduced on Windows 11 / Python 3.12 / debugpy 1.8.x against current main:

    > python -I -m debugpy --listen 5680 -c "import sys; print(repr(sys.path[0]))"
    ''                                          # cwd injected - isolated mode violated
    
    > python -I -c "import sys; print(repr(sys.path[0]))"
    'C:\\Program Files\\Python312\\python312.zip'   # correct

    The three sys.path.insert sites in src/debugpy/server/cli.py (run_file, run_module, run_code) are all unconditional. The comments say "like Python itself does" — but CPython skips all three prepends when -I is in effect, so the missing piece is just the interpreter's own flag:

    if not sys.flags.isolated:
        sys.path.insert(0, dir)   # resp. str("")

    sys.flags.isolated exists since 3.4, so it's safe across all supported versions, and non-isolated runs are untouched. The attach-to-PID injection snippet also inserts a path, but it removes it immediately after import, so I'd leave that one alone.

    I'd like to take this — will put up a PR with the three guards plus a regression test that runs the debuggee under -I for all three run modes and asserts the script dir / cwd is not prepended.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions