'Azure Functions deployment fails because pip install cannot find environment variable / app setting
EDIT: My flaw when deploying to Azure was that I was not saving the new Application settings I was introducing facepalm. Saving PIP_EXTRA_INDEX_URL=https://<token>@raw.githubusercontent.com/<gituser>
allows pip
to finally install my private library.
However, it still doesn't work when locally debugging with F5 on VSCode, regardless of my local.settings.json
. Below I'll describe the hack that makes it work locally, but I would prefer to resolve why it doesn't work from my local settings.
My requirements.txt indicates the private library as <package_name> @ https://raw.githubusercontent.com/<gituser>/<repo>/<branch>/dist/<package_name>0.1.11-py3-none-any.whl
. Based on the fact that adding an PIP_EXTRA_INDEX_URL
in Azure allows the deployment, I am trying to imitate that in my local.settings.json
:
{
"IsEncrypted": false,
"Values": {
...
"PIP_EXTRA_INDEX_URL": "https://<token>@raw.githubusercontent.com/<gituser>",
...
}
}
However, I get a 404 error when the Azure's pip
tries to install from requirements.txt
.
Locally, in tasks.json
, if I set that env var before I run pip install, the F5 starts working:
{
"version": "2.0.0",
"tasks": [
{
"type": "func",
"command": "host start",
"problemMatcher": "$func-python-watch",
"isBackground": true,
"dependsOn": "pip install (functions)"
},
{
"label": "pip install (functions)",
"type": "shell",
"osx": {
...
"windows": {
"command": "$env:PIP_EXTRA_INDEX_URL = 'https://<token>@raw.githubusercontent.com/<gituser>'; ${config:azureFunctions.pythonVenv}\\Scripts\\python -m pip install -r requirements.txt"
},
...
}
]
}
In other words, my F5 is not reading the env var from local.settings.json
before the pip install
task runs (note that the function itself is properly receiving the env vars as evidenced by successful os.environ[] calls). Also note that locally it does work if I set the env var in tasks.json
, which is not ideal. I prefer my local.settings.json
to mirror my Application settings on Azure.
Solution 1:[1]
In this issue it has been confirmed that this is expected behavior and that my workaround is valid:
"local.settings.json" is unique to core tools and thus only applies during the func start command. Since "pip install" happen before func start, you would have to set the env var elsewhere (and yes tasks.json is a good place).
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 | Salazaja |