简体   繁体   English

vscode/Python | 无法访问环境变量

[英]vscode/Python | unable to acess environment variables

So basically my issue is that I have no idea why I can't access the environment variables that I have set in my.env file.所以基本上我的问题是我不知道为什么我不能访问我在 my.env 文件中设置的环境变量。 I'm pretty sure I have set it up correctly because AERPL produces the correct output, but when I use my terminal (Git-bash) or code runner, they can't access the environment variables and just spit out an error.我很确定我已经正确设置了它,因为 AERPL 产生了正确的 output,但是当我使用我的终端(Git-bash)或代码运行器时,它们无法访问环境变量并且只是吐出一个错误。 I have done a lot of searching online, but for the life of me I can't figure out why it isn't working.我在网上做了很多搜索,但对于我的生活,我无法弄清楚为什么它不起作用。 I'm sure there is some easy fix that I am somehow missing... But if anyone can figure out why it isn't working, please let me know.我敢肯定有一些简单的修复方法,我不知何故错过了......但如果有人能弄清楚它为什么不起作用,请告诉我。 Thank you in advance!先感谢您!


File Structure文件结构

cwd/
    .vscode/
        settings.json
    env/
        Lib/
        Scripts/
        .env
    testenv.py

settings.json设置.json

{
    "python.testing.promptToConfigure": false,
    "python.testing.pytestEnabled": false,
    "python.testing.unittestEnabled": false,
    "python.testing.nosetestsEnabled": false,
    "python.pythonPath": "${workspaceFolder}\\env\\Scripts\\python.exe", // Python 3.8.2
    "python.envFile": "${workspaceFolder}\\env\\.env",
    "python.formatting.provider": "autopep8",
    "editor.formatOnSave": true,
    "python.linting.enabled": true,
    "code-runner.executorMap": {
        "python": "$pythonPath -u $fullFileName"
    },
    "code-runner.clearPreviousOutput": true,
    "code-runner.showExecutionMessage": true
} 

.env .env

TEST = "test"

testenv.py测试环境.py

import os
print(os.getenv('TEST'))
print("==========")
print(os.environ['TEST'])

Code Runner/Integrated Terminal Output:代码运行器/集成终端 Output:

os.getenv: None
==========
Traceback (most recent call last):
  File "c:\test\testenv.py", line 4, in <module>
    print(f"os.environ: {os.environ['TEST']}")
  File "c:\python38\lib\os.py", line 675, in __getitem__
    raise KeyError(key) from None
KeyError: 'TEST'

$ py testenv.py
os.getenv: None
==========
Traceback (most recent call last):
  File "testenv.py", line 4, in <module>
    print(f"os.environ: {os.environ['TEST']}")
  File "c:\python38\lib\os.py", line 675, in __getitem__
    raise KeyError(key) from None
KeyError: 'TEST'

AEREPL Output AEREPL Output

Print Output:
os.getenv: test
==========
os.environ: test
Variables:
{}

You may force to recognize environment varibles in integrated terminal by VSCode without entering to debug mode.您可以通过 VSCode 强制识别集成终端中的环境变量,而无需进入调试模式。 This example is for Linux and bash.此示例适用于 Linux 和 bash。

  • Find parameter terminal.integrated.profiles.linux找到参数terminal.integrated.profiles.linux
  • Add to settings.json, correct paths to actual.env file and shell:添加到 settings.json,更正实际.env 文件和 shell 的路径:
        "bash-env": {
            "path": "bash",
            "icon": "terminal-bash",
            "args": ["-c", "if [ -f .env ]; then export $(cat .env); fi; if [ -f .vscode/.env ]; then export $(cat .vscode/.env); fi; bash"]
        },
  • Find parameter terminal.integrated.defaultProfile.linux找到参数terminal.integrated.defaultProfile.linux
  • Choose profile bash-env选择配置文件bash-env

This example is inspired by issue这个例子的灵感来自问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM