'Can't launch Python debug session within VS Code

My current project is using Python 2.7 which I can't change. I have a simple Python test script called env_test.py that contains

import os

print('\n\nPYTHONPATH environment variable is: ' + os.environ.get('PYTHONPATH'))

I have a launch session to debug Python script

{
    "name": "Run integration test script",
    "type": "python",
    "request": "launch",
    "program": "${workspaceFolder}/test/fte_test/integration/test_scripts/${input:IntegrationScripts}",
    "args": [
        "--dut_type=mint",
        "--command_timeout=180",
        "--compact_log",
        "--elog_only=1"
    ],
    "stopOnEntry": false,
    "cwd": "${workspaceFolder}/test/fte_test",
    "sudo": true,
    "python": "/usr/local/micron/bin/python2.7",
    "envFile": "${workspaceFolder}/dev.env",
    "console": "integratedTerminal"
},

The dev.env file contains the following

# dev.env - development  configuration

PYTHONPATH=/users/lordhog/projects/XXXXXX/repo/test/fte_test

When the debug session is launch the following error is reported

[fte_test]$  /usr/bin/env sudo -E /usr/local/XXXXXX/bin/python /users/lordhog/.vscode-server/extensions/ms-python.python-2022.4.1/pythonFiles/lib/python/debugpy/launcher 39257 -- /users/lordhog/projects/YYYYYYY/repo/test/fte_test/integration/test_scripts/cs/evn_test.py --dut_type=mint --command_timeout=180 --compact_log --elog_only=1 
Traceback (most recent call last):
  File "/usr/local/XXXXXX/lib/python2.7/runpy.py", line 162, in _run_module_as_main
    "__main__", fname, loader, pkg_name)
  File "/usr/local/XXXXXX/lib/python2.7/runpy.py", line 72, in _run_code
    exec code in run_globals
  File "/users/lordhog/.vscode-server/extensions/ms-python.python-2022.4.1/pythonFiles/lib/python/debugpy/__main__.py", line 43, in <module>
    from debugpy.server import cli
  File "/users/lordhog/.vscode-server/extensions/ms-python.python-2022.4.1/pythonFiles/lib/python/debugpy/../debugpy/server/__init__.py", line 9, in <module>
    import debugpy._vendored.force_pydevd  # noqa
  File "/users/lordhog/.vscode-server/extensions/ms-python.python-2022.4.1/pythonFiles/lib/python/debugpy/../debugpy/_vendored/force_pydevd.py", line 37, in <module>
    pydevd_constants = import_module('_pydevd_bundle.pydevd_constants')
  File "/usr/local/XXXXXX/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/users/lordhog/.vscode-server/extensions/ms-python.python-2022.4.1/pythonFiles/lib/python/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_constants.py", line 362, in <module>
    from _pydev_bundle._pydev_saved_modules import thread, threading
  File "/users/lordhog/.vscode-server/extensions/ms-python.python-2022.4.1/pythonFiles/lib/python/debugpy/_vendored/pydevd/_pydev_bundle/_pydev_saved_modules.py", line 94, in <module>
    import _thread as thread;    verify_shadowed.check(thread, ['start_new_thread', 'start_new', 'allocate_lock'])
ImportError: No module named _thread

When running the script at the command line it does run

[fte_test]$ sudo -E PYTHONPATH=/users/lordhog/projects/XXXXXXX/repo/test/fte_test /usr/local/YYYYYY/bin/python2.7 ./integration/test_scripts/cs/env_test.py


PYTHONPATH environment variable is: /users/lordhog/projects/XXXXXXX/repo/test/fte_test
[fte_test]$ 

What seems odd to me is the command that VS Code issues. There are no parameters after "-E" . Seems this is where VS Code should insert the environment variable found in the dev.env file. Immediately after "-E" is the Python interpreter path.

Instead of using the envFile parameter in the launch.json file I also tried while removing the envFile parameter

"env": {
    "PYTHONPATH": "${workspaceFolder}/test/fte_test",
    
},

Launching the debug session results in the same error. What am I doing wrong?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source