azure-functions-core-tools icon indicating copy to clipboard operation
azure-functions-core-tools copied to clipboard

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

Open fanshaoshan opened this issue 3 years ago • 32 comments

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.

fanshaoshan avatar Apr 29 '22 22:04 fanshaoshan

Seems this is related to how python has been installed.

When install python from MS Store, if always has issue, but I tried to download python from python.org and install from MSI, issue gone.

fanshaoshan avatar Apr 29 '22 22:04 fanshaoshan

@gavin-aguiar @vrdmr could you please help investigate this?

apawast avatar May 05 '22 17:05 apawast

I'm thankful to see I wasn't the only one! I have also encountered this issue with the same debugging steps. Removing the Python from the MS store and installing Python from python.org MSI seems to allow the proper venv Python to be used to execute the func host start command for me as well. In my case, Python version was 3.9.13

trwhitcomb avatar Jul 01 '22 04:07 trwhitcomb

Hi! I am having the same issue on an M1 MBA. Downloading and installing Python 3.8.10 sets the correct global interpreter but func start still uses Python 3.6.6 and throws a "Failed to initialize worker provider for: /usr/local/Cellar/azure-functions-core-tools@4/4.0.4544/workers/python". Any ideas?

friediisch avatar Aug 31 '22 21:08 friediisch

Hello, i have a similar issue however just as an addition: In case i'm running the function throgh cmd with core tools the environment seems to be picked up properly

 (.venv) (durable_functions) D:\workspace\functions>func host start
Found Python version 3.8.8 (py).

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

But when I run through vscode with debug the environment seems to be an old one:

Executing task: func host start 

Found Python version 3.7.3 (py).

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

thec0dewriter avatar Oct 18 '22 06:10 thec0dewriter