microsoft / vscode-python

Python extension for Visual Studio Code
https://aka.ms/pvsc-marketplace
MIT License
4.34k stars 1.19k forks source link

Python ignores the .env file #24486

Open rhalaly opened 4 days ago

rhalaly commented 4 days ago

Type: Bug

Behaviour

Setting python.envFile to .env ignores it and the environment variables are not found at runtime.

Steps to reproduce:

  1. Set python.envFile to ${workspaceFolder}/.env under User settings.
  2. Create .env file in the repo's root.
  3. Run python code that uses one of the environment variables defined in the .env (or even PYTHONPATH)

Diagnostic data

Output for Python in the Output panel (ViewOutput, change the drop-down the upper-right of the Output panel to Python)

``` 2024-11-25 13:18:09.867 [info] > & ./.venv/Scripts/Activate.ps1 ; echo 'e8b39361-0157-4923-80e1-22d70d46dee6' ; python ~/.vscode/extensions/ms-python.python-2024.20.0-win32-x64/python_files/printEnvVariables.py 2024-11-25 13:18:09.867 [info] shell: powershellCore 2024-11-25 13:18:09.889 [info] > & ./.venv/Scripts/Activate.ps1 ; echo 'e8b39361-0157-4923-80e1-22d70d46dee6' ; python ~/.vscode/extensions/ms-python.python-2024.20.0-win32-x64/python_files/printEnvVariables.py 2024-11-25 13:18:09.889 [info] shell: powershellCore 2024-11-25 13:18:10.092 [info] Discover tests for workspace name: undefined - uri: *****\AppData\Roaming\Code\User\settings.json 2024-11-25 13:18:10.092 [info] Running discovery for pytest using the new test adapter. 2024-11-25 13:18:10.092 [error] Unable to find workspace for given file 2024-11-25 13:18:10.429 [info] Prepending environment variable PATH in collection to *****\.vscode\extensions\ms-python.python-2024.20.0-win32-x64\python_files\deactivate\powershell;*****\.venv\Scripts;C:\Program Files\PowerShell\7;C:\Program Files (x86)\Razer Chroma SDK\bin;C:\Program Files\Razer Chroma SDK\bin;C:\Program Files (x86)\Razer\ChromaBroadcast\bin;C:\Program Files\Razer\ChromaBroadcast\bin;C:\Program Files (x86)\Common Files\Intel\Shared Libraries\redist\intel64\compiler;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8\bin;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8\libnvvp;C:\Program Files\Python39\Scripts\;C:\Program Files\Python39\;C:\Program Files\Microsoft\jdk-11.0.16.101-hotspot\bin;C:\Program Files (x86)\Microsoft SDKs\Azure\CLI2\wbin;C:\Program Files\Eclipse Adoptium\jdk-11.0.16.101-hotspot\bin;C:\Program Files\Microsoft HPC Pack 2019\Bin\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files\dotnet\;C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\170\Tools\Binn\;C:\Program Files\Microsoft SQL Server\150\Tools\Binn\;C:\Program Files\NVIDIA Corporation\NVIDIA NvDLISR;C:\Program Files (x86)\Android\android-sdk\platform-tools;*****\AppData\Roaming\nvm;C:\Program Files\nodejs;C:\ProgramData\chocolatey\bin;*****\AppData\Local\Android\Sdk\cmake\3.10.2.4988404\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\NVIDIA Corporation\Nsight Compute 2022.3.0\;C:\Program Files\OpenSSL-Win64\bin;C:\Program Files\Microsoft Service Fabric\bin\Fabric\Fabric.Code;C:\Program Files\Microsoft SDKs\Service Fabric\Tools\ServiceFabricLocalClusterManager;C:\WINDOWS\system32\config\systemprofile\AppData\Local\Microsoft\WindowsApps;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files\PowerShell\7\;C:\Program Files\Git\cmd;C:\Program Files\Docker\Docker\resources\bin;*****\AppData\Local\CloudBuild;*****\.pyenv\pyenv-win\bin;*****\.pyenv\pyenv-win\shims;*****\AppData\Local\Programs\Python\Python37\Scripts\;*****\AppData\Local\Programs\Python\Python37\;*****\AppData\Local\Microsoft\WindowsApps;*****\AppData\Local\Programs\Microsoft VS Code\bin;*****\AppData\Local\Programs\MiKTeX\miktex\bin\x64\;C:\Tools\rclone;*****\AppData\Roaming\nvm;C:\Program Files\nodejs;*****\.dotnet\tools;*****\.dotnet\tools;*****\AppData\Local\Microsoft\WindowsApps;*****\AppData\Local\Programs\Microsoft VS Code Insiders\bin; {"applyAtShellIntegration":true,"applyAtProcessCreation":true} 2024-11-25 13:18:10.429 [info] Setting environment variable PYTHONPATH in collection to . {"applyAtShellIntegration":true,"applyAtProcessCreation":true} 2024-11-25 13:18:10.430 [info] Setting environment variable VIRTUAL_ENV in collection to *****\.venv {"applyAtShellIntegration":true,"applyAtProcessCreation":true} 2024-11-25 13:18:10.430 [info] Setting environment variable VIRTUAL_ENV_PROMPT in collection to .venv {"applyAtShellIntegration":true,"applyAtProcessCreation":true} 2024-11-25 13:18:10.430 [info] Setting environment variable VSCODE_L10N_BUNDLE_LOCATION in collection to {"applyAtShellIntegration":true,"applyAtProcessCreation":true} 2024-11-25 13:18:10.435 [info] Prepending environment variable PATH in collection to *****\.vscode\extensions\ms-python.python-2024.20.0-win32-x64\python_files\deactivate\powershell;*****\.venv\Scripts;C:\Program Files\PowerShell\7;C:\Program Files (x86)\Razer Chroma SDK\bin;C:\Program Files\Razer Chroma SDK\bin;C:\Program Files (x86)\Razer\ChromaBroadcast\bin;C:\Program Files\Razer\ChromaBroadcast\bin;C:\Program Files (x86)\Common Files\Intel\Shared Libraries\redist\intel64\compiler;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8\bin;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8\libnvvp;C:\Program Files\Python39\Scripts\;C:\Program Files\Python39\;C:\Program Files\Microsoft\jdk-11.0.16.101-hotspot\bin;C:\Program Files (x86)\Microsoft SDKs\Azure\CLI2\wbin;C:\Program Files\Eclipse Adoptium\jdk-11.0.16.101-hotspot\bin;C:\Program Files\Microsoft HPC Pack 2019\Bin\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files\dotnet\;C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\170\Tools\Binn\;C:\Program Files\Microsoft SQL Server\150\Tools\Binn\;C:\Program Files\NVIDIA Corporation\NVIDIA NvDLISR;C:\Program Files (x86)\Android\android-sdk\platform-tools;*****\AppData\Roaming\nvm;C:\Program Files\nodejs;C:\ProgramData\chocolatey\bin;*****\AppData\Local\Android\Sdk\cmake\3.10.2.4988404\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\NVIDIA Corporation\Nsight Compute 2022.3.0\;C:\Program Files\OpenSSL-Win64\bin;C:\Program Files\Microsoft Service Fabric\bin\Fabric\Fabric.Code;C:\Program Files\Microsoft SDKs\Service Fabric\Tools\ServiceFabricLocalClusterManager;C:\WINDOWS\system32\config\systemprofile\AppData\Local\Microsoft\WindowsApps;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files\PowerShell\7\;C:\Program Files\Git\cmd;C:\Program Files\Docker\Docker\resources\bin;*****\AppData\Local\CloudBuild;*****\.pyenv\pyenv-win\bin;*****\.pyenv\pyenv-win\shims;*****\AppData\Local\Programs\Python\Python37\Scripts\;*****\AppData\Local\Programs\Python\Python37\;*****\AppData\Local\Microsoft\WindowsApps;*****\AppData\Local\Programs\Microsoft VS Code\bin;*****\AppData\Local\Programs\MiKTeX\miktex\bin\x64\;C:\Tools\rclone;*****\AppData\Roaming\nvm;C:\Program Files\nodejs;*****\.dotnet\tools;*****\.dotnet\tools;*****\AppData\Local\Microsoft\WindowsApps;*****\AppData\Local\Programs\Microsoft VS Code Insiders\bin; {"applyAtShellIntegration":true,"applyAtProcessCreation":true} 2024-11-25 13:18:10.435 [info] Setting environment variable PYTHONPATH in collection to . {"applyAtShellIntegration":true,"applyAtProcessCreation":true} 2024-11-25 13:18:10.435 [info] Setting environment variable VIRTUAL_ENV in collection to *****\.venv {"applyAtShellIntegration":true,"applyAtProcessCreation":true} 2024-11-25 13:18:10.435 [info] Setting environment variable VIRTUAL_ENV_PROMPT in collection to .venv {"applyAtShellIntegration":true,"applyAtProcessCreation":true} 2024-11-25 13:18:10.435 [info] Setting environment variable VSCODE_L10N_BUNDLE_LOCATION in collection to {"applyAtShellIntegration":true,"applyAtProcessCreation":true} ```

Extension version: 2024.20.0 VS Code version: Code 1.95.3 (f1a4fb101478ce6ec82fe9627c43efbf9e98c813, 2024-11-13T14:50:04.152Z) OS version: Windows_NT x64 10.0.26100 Modes:

User Settings

``` envFile: "" languageServer: "Pylance" testing • pytestArgs: "" • pytestEnabled: true ```

Installed Extensions |Extension Name|Extension Id|Version| |---|---|---| |.NET Install Tool|ms-dotnettools.vscode-dotnet-runtime|2.2.3| |Adaptive Card Previewer|TeamsDevApp.vscode-adaptive-cards|0.1.3| |Alphabetical Sorter|ue.alphabetical-sorter|2.0.1| |Athena Assistant|teamsengsys.athena-vsc-ext|0.0.2024102501| |Augmentation Loop VS Code Extension|AugmentationLoop.augloop-vscode-extension|0.9.277| |Auto Close Tag|formulahendry.auto-close-tag|0.5.15| |Auto Rename Tag|formulahendry.auto-rename-tag|0.1.10| |autopep8|ms-python.autopep8|2024.0.0| |Azure Account|ms-vscode.azure-account|0.12.0| |Azure DevOps Snippets|damienaicheh.azure-devops-snippets|1.0.6| |Azure Functions|ms-azuretools.vscode-azurefunctions|1.16.1| |Azure Resources|ms-azuretools.vscode-azureresourcegroups|0.9.9| |C#|ms-dotnettools.csharp|2.55.29| |C# Dev Kit|ms-dotnettools.csdevkit|1.13.9| |Chronicler|arcsine.chronicler|0.1.16| |Code Spell Checker|streetsidesoftware.code-spell-checker|4.0.21| |Conventional Commits|vivaxy.vscode-conventional-commits|1.26.0| |EBNF Tools|igochkov.vscode-ebnf|1.3.1| |EditorConfig for VS Code|EditorConfig.EditorConfig|0.16.4| |ESLint|dbaeumer.vscode-eslint|3.0.10| |Excel Viewer|GrapeCity.gc-excelviewer|4.2.62| |FiraCode font - Professional Font for Developers|SeyyedKhandon.firacode|2.2.2| |Git History|donjayamanne.githistory|0.6.20| |GitHub Copilot|GitHub.copilot|1.245.0| |GitHub Copilot Chat|GitHub.copilot-chat|0.23.2024102903| |GitLens — Git supercharged|eamodio.gitlens|16.0.3| |GraphQL for VSCode|kumar-harsh.graphql-for-vscode|1.15.3| |Highlight Matching Tag|vincaslt.highlight-matching-tag|0.11.0| |IntelliCode for C# Dev Kit|ms-dotnettools.vscodeintellicode-csharp|2.2.3| |JavaScript Debugger|ms-vscode.js-debug|1.95.3| |JavaScript Debugger Companion Extension|ms-vscode.js-debug-companion|1.1.3| |Jest Runner|firsttris.vscode-jest-runner|0.4.74| |Jupyter|ms-toolsai.jupyter|2024.10.0| |Jupyter Cell Tags|ms-toolsai.vscode-jupyter-cell-tags|0.1.9| |Jupyter Keymap|ms-toolsai.jupyter-keymap|1.1.2| |Jupyter Notebook Renderers|ms-toolsai.jupyter-renderers|1.0.21| |Jupyter Slide Show|ms-toolsai.vscode-jupyter-slideshow|0.1.6| |Kotlin Language|mathiasfrohlich.Kotlin|1.7.1| |Live Preview|ms-vscode.live-server|0.4.15| |Markdown Preview Enhanced|shd101wyy.markdown-preview-enhanced|0.8.15| |Markdown Preview Mermaid Support|bierner.markdown-mermaid|1.27.0| |Material Icon Theme|PKief.material-icon-theme|5.14.1| |MDX|unifiedjs.vscode-mdx|1.8.11| |Microsoft Edge Tools for VS Code|ms-edgedevtools.vscode-edge-devtools|2.1.6| |mplstyle (Matplotlib)|yy0931.mplstyle|1.14.4| |npm Intellisense|christian-kohler.npm-intellisense|1.4.5| |Parquet Explorer|AdamViola.parquet-explorer|1.2.1| |Path Intellisense|christian-kohler.path-intellisense|2.9.0| |Prettier - Code formatter|esbenp.prettier-vscode|11.0.0| |Pylance|ms-python.vscode-pylance|2024.11.3| |Pylint|ms-python.pylint|2024.0.0| |Python|ms-python.python|2024.20.0| |Python Debugger|ms-python.debugpy|2024.12.0| |Python Environment Manager|donjayamanne.python-environment-manager|1.2.4| |Rainbow CSV|mechatroner.rainbow-csv|3.13.0| |React Snippets|ugross.vscode-react-snippets|2.0.1| |Scope to This|rhalaly.scope-to-this|0.2.1| |Subtitles Editor|pepri.subtitles-editor|1.1.7| |Table Visualizer for JavaScript Profiles|ms-vscode.vscode-js-profile-table|1.0.10| |Tailwind CSS IntelliSense|bradlc.vscode-tailwindcss|0.12.15| |TypeScript + Webpack Problem Matchers|amodio.tsl-problem-matcher|0.6.2| |VS Code Speech|ms-vscode.vscode-speech|0.12.1| |Vue - Official|Vue.volar|2.1.10| |YAML|redhat.vscode-yaml|1.15.0| |Zip File Explorer|slevesque.vscode-zipexplorer|0.3.1|
A/B Experiments ``` vsliv368:30146709 vspor879:30202332 vspor708:30202333 vspor363:30204092 vscod805:30301674 binariesv615:30325510 vsaa593:30376534 py29gd2263:31024239 vscaat:30438848 c4g48928:30535728 azure-dev_surveyonecf:30548226 962ge761:30959799 pythonnoceb:30805159 asynctok:30898717 pythonmypyd1:30879173 h48ei257:31000450 pythontbext0:30879054 cppperfnew:31000557 dsvsc020:30976470 pythonait:31006305 dsvsc021:30996838 724cj586:31013169 dvdeprecation:31068756 dwnewjupytercf:31046870 newcmakeconfigv2:31071590 nativerepl2:31139839 pythonrstrctxt:31112756 cf971741:31144450 iacca1:31171482 notype1:31157159 5fd0e150:31155592 dwcopilot:31170013 stablechunks:31184530 ```
karthiknadig commented 4 days ago

@rhalaly Where are you setting this? From the error it looks like you may not have a workspace opened.

rhalaly commented 4 days ago

Of course I have. I opened a folder which is my repo's root and have .env file in the root. For some reason, the Python extension doesn't find it

karthiknadig commented 4 days ago

Can you share the entire log? Also, when you say run Python code, how are you running it? Using the run in terminal command, or using a activated terminal?

rhalaly commented 4 days ago

Can you share the entire log?

Edited the original post.

how are you running it?

Activated terminal / vscode task / vscode launch / vscode debugger / run button on top right corner

Image

None are reading the env var