Skip to content

.env file not working on run current file action #11174

Description

@fmanno

Environment data

  • VS Code version: 1.44.1
  • Extension version (available under the Extensions sidebar): 2020.3.7.659
  • OS and version: macOS Catalina 10.15.3
  • Python version (& distribution if applicable, e.g. Anaconda): Python 3.7.7 64 bit
  • Type of virtual environment used (N/A | venv | virtualenv | conda | ...): none
  • Relevant/affected Python packages and their versions: n/a
  • Relevant/affected Python-related VS Code extensions and their versions: n/a
  • Jedi or Language Server? (i.e. what is "python.jediEnabled" set to; more info How to update the language server to the latest stable version #3977): jedi == False
  • Value of the python.languageServer setting: Microsoft

Expected behaviour

When clicking the green "Play" button on the top right of the editor window I expect the current python file to be executed and the .env file in the workspace to be loaded adding the environment variables in that file to the environment where python executes the file.

Actual behaviour

.env file doesn't seem to be loaded because the expected environment variables are not available in the python script

Steps to reproduce:

  1. Create a folder
  2. Open the folder in VScode
  3. Create a .env file with the contents:
VARIABLE=testing
  1. Create a test.py file with the contents:
import os
print(os.environ['VARIABLE'])
  1. Click the green "Play" button on the top right corner of the editor window
/Volumes/git/env_bug 
02:29 $ /usr/local/bin/python3 /Volumes/git/env_bug/test.py
Traceback (most recent call last):
  File "/Volumes/git/env_bug/test.py", line 2, in <module>
    print(os.environ['VARIABLE'])
  File "/usr/local/Cellar/python/3.7.7/Frameworks/Python.framework/Versions/3.7/lib/python3.7/os.py", line 681, in __getitem__
    raise KeyError(key) from None
KeyError: 'VARIABLE'

Output from Console under the Developer Tools panel:

[Extension Host] Info Python Extension: 2020-04-15 08:48:39: Cached data exists getEnvironmentVariables, /Volumes/git/env_bug
console.ts:137 [Extension Host] Info Python Extension: 2020-04-15 08:48:39: Cached data exists getEnvironmentVariables, extension-output-#4
console.ts:137 [Extension Host] Info Python Extension: 2020-04-15 08:48:51: Cached data exists getEnvironmentVariables, /Volumes/git/env_bug
console.ts:137 [Extension Host] Info Python Extension: 2020-04-15 08:48:54: Cached data exists getEnvironmentVariables, extension-output-#4
console.ts:137 [Extension Host] Info Python Extension: 2020-04-15 08:49:04: Cached data exists getEnvironmentVariables, /Volumes/git/env_bug
console.ts:137 [Extension Host] Info Python Extension: 2020-04-15 08:49:04: Cached data exists getEnvironmentVariables, extension-output-#5
console.ts:137 [Extension Host] Info Python Extension: 2020-04-15 08:49:06: Cached data exists getEnvironmentVariables, /Volumes/git/env_bug
console.ts:137 [Extension Host] Info Python Extension: 2020-04-15 08:49:06: Cached data exists getEnvironmentVariables, extension-output-#9
console.ts:137 [Extension Host] Info Python Extension: 2020-04-15 08:49:16: Cached data exists getEnvironmentVariables, /Volumes/git/env_bug
console.ts:137 [Extension Host] Info Python Extension: 2020-04-15 08:49:20: Cached data exists getEnvironmentVariables, extension-output-#9

If instead I run the same file with the debugger I get the expected result

  1. Run and debug
  2. Current file
/Volumes/git/env_bug 
02:29 $  cd /Volumes/git/env_bug ; env VARIABLE=testing PYTHONIOENCODING=UTF-8 PYTHONUNBUFFERED=1 /usr/local/bin/python3 /Users/fmanno/.vscode/extensions/ms-python.python-2020.3.71659/pythonFiles/ptvsd_launcher.py --default --client --host localhost --port 53281 /Volumes/git/env_bug/test.py 
testing

I have already read all the discussion on #944 and I don't think it's the same. I observe the same as #9358 which I think was closed down prematurely.

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions