4

Despite specifying a python.envFile in workspace (.vscode/settings.json), python.defaultInterpreterPath does not seem to fetch the interpreter path via an environment variable, declared in the envFile.

  1. File: .env
# filename: .env
# set this in .vscode/settings.json:
# "python.envFile": "${workspaceFolder}/.env"
DEFAULT_INTERPRETER_PATH=path/to/python/interepreter
  1. File: .vscode/settings.json
// filename: .vscode/settings.json
{
    "python.envFile": "${workspaceFolder}/.env",
    "python.defaultInterpreterPath": "${env:DEFAULT_INTERPRETER_PATH}",
    "python.terminal.activateEnvironment": true,
    "python.terminal.activateEnvInCurrentTerminal": false,
    "jupyter.jupyterServerType": "local",
}

If I hard code the python.defaultInterpreterPath, it works, and auto activates the interpreter, when I open a new terminal window. But it does not activate the the interpreter from the variable (in .env file).

References

Issue opened on GitHub with VS Code

CypherX
  • 7,019
  • 3
  • 25
  • 37
  • Changes to the python.defaultInterpreterPath will not be picked up by the Python extension once user explicitly chooses a different interpreter for the workspace.[Reference documentation](https://github.com/microsoft/vscode-python/wiki/Setting-descriptions#pythondefaultinterpreterpath). – JialeDu Jun 10 '22 at 09:33
  • @JialeDu Thank you, for your comment. But that's not the issue. Hardcoded path is recognized. Path referenced via an environment variable is not. – CypherX Jun 10 '22 at 09:46
  • i wonder if because the env file hasnt been loaded as yet. you could try setting the interpreter via env virable in the workspace settings file (workspace.code-workspace) – mike01010 Oct 10 '22 at 20:18

2 Answers2

0

You can use variable substitution in settings files, currently variables in environment files are not recognized.

You can also use an environment variable in the path setting using the syntax ${env:VARIABLE}. For example, if you've created a variable named PYTHON_INSTALL_LOC with a path to an interpreter, you can then use the following setting value:

"python.defaultInterpreterPath": "${env:PYTHON_INSTALL_LOC}",

Note: Variable substitution is only supported in VS Code settings files, it will not work in .env environment files.

JialeDu
  • 6,021
  • 2
  • 5
  • 24
  • Please see the question: this is already being done. Also see the issue raised with `vscode` on GitHub: this is a bug that falls under `vscode-python` extension. Your answer, I am afraid, does not provide any real value to fix the problem, as it is a bug and there's no fix, until there is one. – CypherX Jun 13 '22 at 06:26
  • I just told you the information in the official documentation in response to "But it does not activate the the interpreter from the variable (in .env file)." – JialeDu Jun 13 '22 at 06:39
  • The documentation says that you could use a given path for your python interpreter in the `.env` file and in turn use the variable in the `.vscode/settings.json` file to define `python.defaultInterpreterPath`. If the `defaultInterpreterPath` is defined, this could be used to auto start a python environment or interpreter with the correct python executable. When hardcoded, this works. But if you try using the `.env` route, it does not. I am not trying to load some environment variable inside a python script -- I can use the `dotenv` library for that or vscode also provides that via `.env` file. – CypherX Jun 13 '22 at 06:53
  • I don't know if you are talking about [this document](https://code.visualstudio.com/docs/python/environments#_manually-specify-an-interpreter)? [screenshot](https://imgur.com/a/kDGJINb). – JialeDu Jun 13 '22 at 07:00
  • Yes. That's the one. (same as the source you quoted from). Also, I had it mentioned in the question's `References` section: [source](https://code.visualstudio.com/docs/python/environments#_environment-variables). – CypherX Jun 13 '22 at 10:50
  • some folks just spend their time regurgitating documentation as answers for the points...that's all this is. – mike01010 Oct 10 '22 at 20:15
0

I think there are at least two possible misunderstandings here.

  1. Variable substitution is not supported in all settings- only in select ones, of which I am not aware of python.defaultInterpreterPath being one of them.

  2. From my understanding, the ${env:VARIABLE_NAME} mechanism is for referencing variables from the environment of the VS Code process itself, and the python extension's envFile mechanism does not modify the environment of the VS Code process, but instead child processes. The documentation states:

    Environment variable definitions files can be used for scenarios such as debugging and tool execution (including linters, formatters, IntelliSense, and testing tools), but aren't applied to the terminal.

Of course, I haven't ruled out that I could have misunderstood what I've read either, but this is my understanding from what I've read.

starball
  • 20,030
  • 7
  • 43
  • 238