Repository navigation
.env file not working on run current file action #11174
Copy link
Copy link
Closed
Labels
area-terminalfeature-requestRequest for new features or functionalityRequest for new features or functionality
Description
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.languageServersetting: 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:
- Create a folder
- Open the folder in VScode
- Create a .env file with the contents:
VARIABLE=testing
- Create a test.py file with the contents:
import os
print(os.environ['VARIABLE'])
- 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
- Run and debug
- 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.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
area-terminalfeature-requestRequest for new features or functionalityRequest for new features or functionality
Activity
karthiknadig commentedon Apr 29, 2020
fmanno (@fmanno) This is not currently supported, switching this to an enhancement.
karthiknadig commentedon Apr 29, 2020
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.
fmanno commentedon May 2, 2020
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.
DonJayamanne commentedon May 3, 2020
Closing as a dup of #944