-
Notifications
You must be signed in to change notification settings - Fork 146
Description
Problem: Vscode doesn't seems to recognise my python virtual environment for the azure function project.
System Specs:
OS: Ubuntu 18.04
Vscode Version: 1.44.2 (user setup)
Commit: ff915844119ce9485abfe8aa9076ec76b5300ddd
Date: 2020-04-16T16:36:23.138Z
Electron: 7.1.11
Chrome: 78.0.3904.130
Node.js: 12.8.1
V8: 7.8.279.23-electron.0
OS: Windows_NT x64 10.0.18363
Steps to Reproduce:
- follow the official documentation to create a new azure function project (except in the first command use
python3 -m venv .venvinstead) - Reload Window (Ctrl + R or Developer: Reload Window in the command palette)
- Allow vscode to create its configuration files for the azure function project by clicking on "Yes" when you see the below pops up

- Open the
__init__.pyfile located underLocalFunctionProj/HttpExampleand add a line to import a package not installed (I have used "pandas") under the your root python site-packages folder (in my case it is /home/<my_username>/.local/lib/python3.6/site-packages) - Add this package to the
requirements.txtunderLocalFunctionProj

- Run
source .venv/bin/activateto activate the virtual environment - Run
pip install -r ./LocalFunctionProj/requirements.txtto install all dependencies - Press
F5key to enter into the debug mode - Open up this link in your browser http://localhost:7071/api/HttpExample?name=Functions and see that it should throw an
ModuleNotFoundErrorerror about pandas not being imported

Diagnosis
After some researches I came to notices that it is not activating the virtual environment when using the debug mode. Here is how I found it out
- Delete
import pandasand addimport sys - Add
logging.info("Python executable path is: {}".format(sys.executable))to log the python executable directory anywhere in the__init__.pybefore the function ends - Launch the debug mode by pressing
F5 - Refresh http://localhost:7071/api/HttpExample?name=Functions and it prints out this

Solution
At the end, I solved it by adding "azureFunctions.pythonVenv": "${workspaceFolder}/.venv" in the .vscode/settings.json file

Btw, I don't think this value "python.pythonPath": ".venv/bin/python3" is used at all as even I have removed it completed it doesn't seem to impact the usability of neither the debug mode nor of func start.
Here are my configuration files:
extensions.json
{ "recommendations": [ "ms-azuretools.vscode-azurefunctions", "ms-python.python" ] }
launch.json
{ "version": "0.2.0", "configurations": [ { "name": "Attach to Python Functions", "type": "python", "request": "attach", "port": 9091, "preLaunchTask": "func: host start" } ] }
settings.json
{ "azureFunctions.pythonVenv": "${workspaceFolder}/.venv", "azureFunctions.deploySubpath": "LocalFunctionProj", "azureFunctions.scmDoBuildDuringDeployment": true, "azureFunctions.projectLanguage": "Python", "azureFunctions.projectRuntime": "~2", "debug.internalConsoleOptions": "neverOpen", "python.pythonPath": ".venv/bin/python3" }
tasks.json
{ "version": "2.0.0", "tasks": [ { "type": "func", "command": "host start", "problemMatcher": "$func-watch", "isBackground": true, "options": { "cwd": "${workspaceFolder}/LocalFunctionProj" } } ] }