Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
fmardero opened this issue Dec 10, 2024 · 2 comments
Assignees
Labels
triage-needed Needs assignment to the proper sub-team

Comments

@fmardero
Copy link

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?
@karthiknadig karthiknadig removed their assignment Dec 10, 2024
@karthiknadig karthiknadig transferred this issue from microsoft/vscode Dec 10, 2024
@github-actions github-actions bot added the triage-needed Needs assignment to the proper sub-team label Dec 10, 2024
@karthiknadig
Copy link
Member

@fmardero This is likely coming from pythonTerminalEnvVarActivation feature, which is behind an experimental switch. You can turn it off by using this setting: "python.experiments.optOutFrom": ["pythonTerminalEnvVarActivation"],

@karthiknadig karthiknadig self-assigned this Dec 10, 2024
@karthiknadig
Copy link
Member

Duplicate #24209

@karthiknadig karthiknadig closed this as not planned Won't fix, can't repro, duplicate, stale Dec 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
triage-needed Needs assignment to the proper sub-team
Projects
None yet
Development

No branches or pull requests

3 participants