Skip to content

azureFunctions.pythonVenv uses wrong folder #2041

@georged4s

Description

@georged4s

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 .venv instead)
  • 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
    image
  • Open the __init__.py file located under LocalFunctionProj/HttpExample and 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.txt under LocalFunctionProj
    image
  • Run source .venv/bin/activate to activate the virtual environment
  • Run pip install -r ./LocalFunctionProj/requirements.txt to install all dependencies
  • Press F5 key 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 ModuleNotFoundError error about pandas not being imported
    image

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 pandas and add import sys
  • Add logging.info("Python executable path is: {}".format(sys.executable)) to log the python executable directory anywhere in the __init__.py before the function ends
  • Launch the debug mode by pressing F5
  • Refresh http://localhost:7071/api/HttpExample?name=Functions and it prints out this
    image

Solution

At the end, I solved it by adding "azureFunctions.pythonVenv": "${workspaceFolder}/.venv" in the .vscode/settings.json file
image
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" } } ] }

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions