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

Why can't the env file be read during RunPythonFile and can only be read during debugging? #20932

Closed
xxc-zsz opened this issue Mar 29, 2023 · 2 comments
Assignees
Labels
triage-needed Needs assignment to the proper sub-team

Comments

@xxc-zsz
Copy link

xxc-zsz commented Mar 29, 2023

Use the following sample code to load the .env file without loading the env file with additionally installed third-party modules.

import os
print(os.getenv("name"))

File structure

py03
├─ .env
├─ .vscode
│  ├─ launch.json
│  └─ settings.json
└─ demo02.py

.env file

name=VSCODE

The content of settings.json file:

{
    "python.envFile": "${workspaceFolder}/.env"
}

Results using Run Python FIle

image

Add envFile configuration in launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "justMyCode": true,
            "envFile": "${workspaceFolder}/.env"
        }
    ]
}

Use the triangle button to debug the script in the Run and Debug panel, or use Ctrl+F5 can get results

image

Why does the same code work under debug, but run doesn't work?

@github-actions github-actions bot added the triage-needed Needs assignment to the proper sub-team label Mar 29, 2023
@karrtikr
Copy link

Closing as dup of #944

@karrtikr karrtikr closed this as not planned Won't fix, can't repro, duplicate, stale Mar 29, 2023
@karrtikr
Copy link

Btw this issue should be solved with #11039, the fix should already be out if you want to try it:

  • Use pre-release:
    image
  • Add "python.experiments.optInto": ["pythonTerminalEnvVarActivation"] to User settings and reload window.
  • Note you won't see the prompt (<env_name>) in fish but the env should be activated.
  • Make sure to not be using a multiroot workspace

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 29, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
triage-needed Needs assignment to the proper sub-team
Projects
None yet
Development

No branches or pull requests

2 participants