Skip to content

Selecting a Python interpreter in a virtual environment automatically reads the project .env file #24573

@fmardero

Description

@fmardero

Does this issue occur when all extensions are disabled?: Yes

  • VS Code Version: 1.95.3 (Universal)
  • OS Version: MacOS Sequoia 15.0.1 (24A348) | Also tested on Windows 11 Pro (22H2)

Steps to Reproduce:

  1. Create a .env file in an empty project folder that includes an environment variable.
    Example:

    DEPLOY_ENV=dev
  2. Check if the environment variable exists before any further action:

    • On Linux/MacOS:
      echo $DEPLOY_ENV
    • On Windows:
      set DEPLOY_ENV
  3. Create a virtual environment in the project folder without activating it:

    python3 -m venv .venv
  4. Verify the environment variable again as in step 2. It should not exist.

  5. Activate the Python virtual environment manually:

    • On Linux/MacOS:
      source .venv/bin/activate
    • On Windows:
      .venv\Scripts\activate.bat
  6. Check the environment variable again as in step 2. It should still not exist.

  7. Select the virtual environment's Python interpreter in VS Code using the Command Palette.

  8. Open a new integrated terminal window in VS Code.

  9. Check the environment variable again as in step 2. This time, the variable exists and matches the value defined in the .env file.

Additional Observations

  • If VS Code automatically detects the virtual environment and you then open a new terminal window, the same behavior occurs: the environment variable is set according to the .env file.
  • Renaming the .env file to .enva and repeating the steps does not lead to the environment variable being set, indicating that .env files specifically are read and applied.

Summary

It seems that selecting the Python interpreter associated with the virtual environment in VS Code automatically reads and exports the variables defined in a .env file. However, I could not find any documentation describing this behavior.

Questions/Clarifications

  1. Is this behavior intended or a side effect of VS Code's environment handling?
  2. If intended, could you provide documentation or guidance on how this behavior can be controlled or disabled?

Metadata

Metadata

Assignees

Labels

triage-neededNeeds assignment to the proper sub-team

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions