Skip to content

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

Closed

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.

Activity

  1. added
    triage-neededNeeds assignment to the proper sub-team
    bugIssue identified by VS Code Team member as probable bug
    on Apr 15, 2020
  2. ghost removed
    triage-neededNeeds assignment to the proper sub-team
    on Apr 15, 2020
  3. ghost removed
    triage-neededNeeds assignment to the proper sub-team
    on Apr 28, 2020
  4. karthiknadig commented on Apr 29, 2020

    @karthiknadig
    Member

    fmanno (@fmanno) This is not currently supported, switching this to an enhancement.

  5. karthiknadig commented on Apr 29, 2020

    @karthiknadig
    Member

    We have marked this issue as "needs decision" to make sure we have a conversation about your idea. We plan to leave this feature request open for at least a month to see how many 👍 votes the opening comment gets to help us make our decision.

  6. fmanno commented on May 2, 2020

    @fmanno
    Author

    Karthik Nadig (@karthiknadig) Thanks for considering this. I think it should behave this way otherwise it's inconsistent with when the debugger is run and could be confusing.
    But up to you of course.

  7. DonJayamanne commented on May 3, 2020

    @DonJayamanne

    Closing as a dup of #944

  8. locked as resolved and limited conversation to collaborators on May 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions