'How do I select environment variables using os.environ (using VSCode & Python)
I am building an Azure Function in VSCode and am unable to get my function locally to refer to the environment variables that I am adding to local.settings.json as I get an error when trying to access my environment variable. I want to access the one named clientId.
My local.settings.json:
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "",
"FUNCTIONS_WORKER_RUNTIME": "python",
"clientId": "test123"
}
}
My script:
import os
import azure.functions as func
print(os.environ['clientId'])
The error:
File "c:\Users\xxxx\source\Repos\xxxx\Python\xxxx\HttpTrigger1\__init__.py", line 7, in <module>
print(os.environ['clientID'])
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.3312.0_x64__qbz5n2kfra8p0\lib\os.py",
line 679, in __getitem__
raise KeyError(key) from None
KeyError: 'clientID'
To try and troubleshoot this I tried to print all environment variables expecting to see my local.settings.json however I do not see anything resembling the local.settings.json file.
Script:
import os
import azure.functions as func
print(os.environ)
Response:
environ({
"ALLUSERSPROFILE":"C:\\ProgramData",
"APPDATA":"C:\\Users\\xxxx\\AppData\\Roaming",
"CHROME_CRASHPAD_PIPE_NAME":"\\\\.\\pipe\\crashpad_4888_HMCJUBDIIALYYBRP",
"COMMONPROGRAMFILES":"C:\\Program Files\\Common Files",
"COMMONPROGRAMFILES(X86)":"C:\\Program Files (x86)\\Common Files",
"COMMONPROGRAMW6432":"C:\\Program Files\\Common Files",
"COMPUTERNAME":"xxxx",
"COMSPEC":"C:\\windows\\system32\\cmd.exe",
"DEPLOYMENT.EXPIRATION.CHECK.ENABLED":"false",
"DRIVERDATA":"C:\\Windows\\System32\\Drivers\\DriverData",
"FPS_BROWSER_APP_PROFILE_STRING":"Internet Explorer",
"FPS_BROWSER_USER_PROFILE_STRING":"Default",
"HOMEDRIVE":"C:",
"HOMEPATH":"\\Users\\xxxN",
"LOCALAPPDATA":"C:\\Users\\xxxN\\AppData\\Local",
"LOG4J_FORMAT_MSG_NO_LOOKUPS":"true",
"LOGONSERVER":"\\\\xxxxxxxxxx",
"NUMBER_OF_PROCESSORS":"8",
"ONEDRIVE":"C:\\Users\\xxxN\\OneDrive - xxx",
"ONEDRIVECOMMERCIAL":"C:\\Users\\xxxN\\OneDrive - xxx",
"ORIGINAL_XDG_CURRENT_DESKTOP":"undefined",
"OS":"Windows_NT",
"PATH":"C:\\Program Files\\Eclipse Foundation\\jdk-8.0.302.8-hotspot\\bin;C:\\Program Files (x86)\\Common Files\\Oracle\\Java\\javapath;C:\\windows\\system32;C:\\windows;C:\\windows\\System32\\Wbem;C:\\windows\\System32\\WindowsPowerShell\\v1.0\\;C:\\Program Files\\Gemalto\\Classic Client\\BIN;C:\\Program Files (x86)\\Gemalto\\Classic Client\\BIN;C:\\Program Files\\Git\\cmd;C:\\Program Files\\Microsoft SQL Server\\150\\Tools\\Binn\\;C:\\Program Files\\Microsoft SQL Server\\Client SDK\\ODBC\\170\\Tools\\Binn\\;C:\\Program Files\\dotnet\\;C:\\Program Files (x86)\\Microsoft SQL Server\\150\\DTS\\Binn\\;C:\\Program Files\\Azure Data Studio\\bin;C:\\Program Files\\Microsoft SQL Server\\130\\Tools\\Binn\\;C:\\Program Files (x86)\\dotnet\\;C:\\Users\\xxxN\\AppData\\Local\\Microsoft\\WindowsApps;;C:\\Users\\xxxxN\\AppData\\Local\\Programs\\Azure Data Studio\\bin;C:\\Users\\xxxxN\\AppData\\Local\\Programs\\Microsoft VS Code\\bin",
"PATHEXT":".COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.CPL",
"PROCESSOR_ARCHITECTURE":"AMD64",
"PROCESSOR_IDENTIFIER":"Intel64 Family 6 Model 140 Stepping 1, GenuineIntel",
"PROCESSOR_LEVEL":"6",
"PROCESSOR_REVISION":"8c01",
"PROGRAMDATA":"C:\\ProgramData",
"PROGRAMFILES":"C:\\Program Files",
"PROGRAMFILES(X86)":"C:\\Program Files (x86)",
"PROGRAMW6432":"C:\\Program Files",
"PSMODULEPATH":"C:\\Users\\xxxxN\\Documents\\WindowsPowerShell\\Modules;C:\\Program Files\\WindowsPowerShell\\Modules;C:\\windows\\system32\\WindowsPowerShell\\v1.0\\Modules",
"PUBLIC":"C:\\Users\\Public",
"SESSIONNAME":"Console",
"SYSTEMDRIVE":"C:",
"SYSTEMROOT":"C:\\windows",
"TEMP":"C:\\Users\\xxxxN\\AppData\\Local\\Temp",
"TMP":"C:\\Users\\xxxxN\\AppData\\Local\\Temp",
"UID":"fxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxf",
"USERDNSDOMAIN":"BLUE.LOCAL",
"USERDOMAIN":"BLUE",
"USERDOMAIN_ROAMINGPROFILE":"BLUE",
"USERNAME":"xxxxx",
"USERPROFILE":"C:\\Users\\xxxxN",
"WINDIR":"C:\\windows",
"ZES_ENABLE_SYSMAN":"1",
"TERM_PROGRAM":"vscode",
"TERM_PROGRAM_VERSION":"1.66.0",
"LANG":"en_US.UTF-8",
"COLORTERM":"truecolor",
"VSCODE_GIT_IPC_HANDLE":"\\\\.\\pipe\\vscode-git-33ca814331-sock",
"VSCODE_GIT_ASKPASS_NODE":"C:\\Users\\xxxN\\AppData\\Local\\Programs\\Microsoft VS Code\\Code.exe",
"VSCODE_GIT_ASKPASS_EXTRA_ARGS":"--ms-enable-electron-run-as-node",
"VSCODE_GIT_ASKPASS_MAIN":"c:\\Users\\xxxxN\\AppData\\Local\\Programs\\Microsoft VS Code\\resources\\app\\extensions\\git\\dist\\askpass-main.js",
"GIT_ASKPASS":"c:\\Users\\xxxxx\\AppData\\Local\\Programs\\Microsoft VS Code\\resources\\app\\extensions\\git\\dist\\askpass.sh",
"PYTHONUSERBASE":"C:\\Users\\xxxxx\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\\LocalCache\\local-packages"
})
Solution 1:[1]
local.settings.json is a configuration file used by Azure Functions. It is not something linked to Python itself that will directly work in any environment.
Using this module, if you start the Azure Function with it, it should simulate locally the AzureDevOps context.
https://marketplace.visualstudio.com/items?itemName=ms-azuretools.vscode-azurefunctions
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Floh |
