Azure / Azure/azure-functions-core-tools

"func host start" ignore venv and uses global python interpreter

Open
#3,042 34 comments 16 reactions 0 assignees View on GitHub
bug core-tools investigate needs-investigation
Dominant language
C#
Stars
1.5k
Forks
498
Avg merge
5d 4h
Merged PRs (30d)
15

Description

When developing azure function using python, any new packages added to requirements.txt cannot be found when debugging locally. It will always cause "Module not found" error, deploy to cloud has no issue.

I did some tests, and it turns out that even in a venv, but "func host start" will still use the global interpreter

Here are the steps to reproduce this. Using git-bash, same result when using powershell, before run function, I edit __init__.py to print out sys.path and sys.executable to check which python been used.

[🤖💤] ~/Workspace/azfunc2 $ python -m venv .venv

[🤖💤] ~/Workspace/azfunc2 $ source .venv/Scripts/activate

(.venv) [🤖💤] ~/Workspace/azfunc2 $ func init
Use the up/down arrow keys to select a worker runtime:python
Found Python version 3.7.9 (python3).
Writing requirements.txt
Writing .funcignore
Writing getting_started.md
Writing .gitignore
Writing host.json
Writing local.settings.json
Writing C:\Users\shfan\Workspace\azfunc2\.vscode\extensions.json

(.venv) [🤖💤] ~/Workspace/azfunc2 $ func new HttpTrigger
Use the up/down arrow keys to select a template:HTTP trigger
Function name: [HttpTrigger]
Writing C:\Users\shfan\Workspace\azfunc2\HttpTrigger\__init__.py
Writing C:\Users\shfan\Workspace\azfunc2\HttpTrigger\function.json
The function "HttpTrigger" was created successfully from the "HTTP trigger" template.

(.venv) [🤖💤] ~/Workspace/azfunc2 $ vim HttpTrigger/__init__.py

(.venv) [🤖💤] ~/Workspace/azfunc2 $ func host start
Found Python version 3.7.9 (python3).

Azure Functions Core Tools
Core Tools Version: 4.0.4483 Commit hash: N/A (64-bit)
Function Runtime Version: 4.1.3.17473

Functions:

HttpTrigger: [GET,POST] http://localhost:7071/api/HttpTrigger

For detailed output, run func with --verbose flag.
[2022-04-29T22:25:44.326Z] Worker process started and initialized.
[2022-04-29T22:25:47.685Z] Executing 'Functions.HttpTrigger' (Reason='This function was programmatically called via the host APIs.', Id=2c032d7d-5dcc-40fc-88f7-bbc8f211e7c8)
[2022-04-29T22:25:47.744Z] Python HTTP trigger function processed a request.
[2022-04-29T22:25:47.748Z] ['C:\\Program Files\\Microsoft\\Azure Functions Core Tools\\workers\\python\\3.7\\WINDOWS\\X64', 'C:\\Program Files\\Microsoft\\Azure Functions Core Tools\\workers\\python\\3.7\\WINDOWS\\X64', 'C:\\Program Files\\WindowsApps\\PythonSoftwareFoundation.Python.3.7_3.7.2544.0_x64__qbz5n2kfra8p0\\python37.zip', 'C:\\Program Files\\WindowsApps\\PythonSoftwareFoundation.Python.3.7_3.7.2544.0_x64__qbz5n2kfra8p0\\DLLs', 'C:\\Program Files\\WindowsApps\\PythonSoftwareFoundation.Python.3.7_3.7.2544.0_x64__qbz5n2kfra8p0\\lib', 'C:\\Program Files\\WindowsApps\\PythonSoftwareFoundation.Python.3.7_3.7.2544.0_x64__qbz5n2kfra8p0', 'C:\\Program Files\\WindowsApps\\PythonSoftwareFoundation.Python.3.7_3.7.2544.0_x64__qbz5n2kfra8p0\\lib\\site-packages', 'C:\\Users\\shfan\\Workspace\\azfunc2']
[2022-04-29T22:25:47.749Z] C:\Users\shfan\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\python.exe
[2022-04-29T22:25:47.805Z] Executed 'Functions.HttpTrigger' (Succeeded, Id=2c032d7d-5dcc-40fc-88f7-bbc8f211e7c8, Duration=138ms)

This is on my Windows 11 laptop, tested with python 3.7, 3.8, 3.9, all have the same issue, but on another windows 10 with python 3.7, works fine, not sure if this is related to OS or some settings.

Contributor guide

Open the contributing guide

Research direction

Reproduce the issue with a Python virtual environment on Windows, using `func host start` and the reported `__init__.py` diagnostics for `sys.path` and `sys.executable`. Compare the interpreter and package paths with the activated venv; done means local debugging uses the venv interpreter so packages from `requirements.txt` can be imported.

Written by the indexing model from the issue text.

Assessment

Tech stack
azure, python
Domain
cli, devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.