Skip to content

python.interpreterPath resolves incorrectly in tasks.json when it isn't first #1235

@pgraenin

Description

@pgraenin

I am working on switching some workflows to be venv-based, partly to support hombrew-installed versions of Python that are used to create the Venv, and kept running into ${command:python.interpreterPath} not resolving to the active venv interpreter that was selected for the opened workspace.

Here's the original entry. (I've obfuscated details that aren't impactful)

{
    "label": "Run xxx.py and open report",
    "type": "shell",
    "command": "cd ${workspaceFolder}/scripts && ${command:python.interpreterPath} xxx.py && code xxx.txt",
    "problemMatcher": [],
    "group": {
        "kind": "test",
        "isDefault": false
}

After setting the active environment for the workspace to a venv in my venvs folder, and running the task, this is the task's terminal output:

Executing task: cd /Users/xxx/xxx/xxx/scripts && /opt/homebrew/bin/python3 xxx.py && code xxx.txt 

Note I was not expecting the homebrew Python interpreter to be used.

I created a separate task for a little troubleshooting:

{
    "label": "Variable tinkering",
    "type": "shell",
    "command": "echo 'Would call: ${command:python.interpreterPath}'",
    "problemMatcher": [],
    "group": {
        "kind": "test",
        "isDefault": false
   }

This echoed to the terminal: Would call: /Users/xxx/venvs/xxx-python/bin/python

Weird, right?

So, I changed the shell command for my first task to assign interpreterPath to a variable first, and used that to call Python:

PYEXEC=${command:python.interpreterPath} && cd /Users/xxx/xxx/xxx/scripts && ${PYEXEC} xxx.py && code xxx.txt 

This ran, and the terminal output showed:

 *  Executing task: PYEXEC=/Users/xxx/venvs/xxx-python/bin/python && cd /Users/xxx/xxx/xxx/scripts && ${PYEXEC} xxx.py && code xxx.txt

And it called the interpreter living in that particular venv.

I could have left it like that, but I wanted to see if using PYEXEC was actually necessary, so first, I continued setting it, but didn't use it to call the interpreter:

PYEXEC=${command:python.interpreterPath} && cd ${workspaceFolder}/scripts && ${command:python.interpreterPath} xxx.py && code xxx.txt

And lo and behold, the terminal output showed:

 *  Executing task: PYEXEC=/Users/xxx/venvs/xxx-python/bin/python && cd /Users/xxx/xxx/xxx/scripts && /Users/xxx/venvs/xxx-python/bin/python xxx.py && code xxx.txt 

And the task will run with the venv's interpreter. So, do I even need PYEXEC?

I removed setting the PYEXEC variable... back to the original, cd ${workspaceFolder}/scripts && ${command:python.interpreterPath} xxx.py && code xxx.txt and it regressed to running the interpreter at /opt/hombrew/bin/python3.

Last check - is it just because I used workspaceFolder, or is it because pythonInterpreter isn't the first variable used?

I changed the working task command to this:

echo ${workspaceFolderBasename} && PYEXEC=${command:python.interpreterPath} && cd ${workspaceFolder}/scripts && ${command:python.interpreterPath} grade.py && code tempReportFile.txt

And, surprise, it output:

 *  Executing task: echo xxx-folder && PYEXEC=/opt/homebrew/bin/python3 && cd /Users/xxx/xxx/xxx/scripts && /opt/homebrew/bin/python3 xxx.py && code xxx.txt 

Regressing back to the homebrew copy of Python.

VS Code build info:
Version: 1.109.3 (Universal)
Commit: b6a47e94e326b5c209d118cf0f994d6065585705
Electron: 39.3.0
ElectronBuildId: 13168319
Chromium: 142.0.7444.265
Node.js: 22.21.1
V8: 14.2.231.22-electron.0
OS: Darwin arm64 25.3.0

Extension versions:
ms-python.python 2026.0.0
ms-python.debugpy 2025.18.0
ms-python.vscode-python-envs 1.16.0
Python 3.14.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions