Can't debug Python Function App from VS Code

Dave 96 Reputation points
2021-11-09T03:50:54.687+00:00

I created a new Function App project using Python 3.8 in VS Code on Windows 10. I am trying to import a local module from within the project like this:

import shared_code.my_second_helper_function

So the import works and the code runs when I run the function via 'func start' from the command line at the root of the project. My issue is I get errors when I try to run the debugger. When I hit the debug button in VS Code, I get an error:

Exception has occurred: ModuleNotFoundError  'No module named shared_code' in my_first_function\__init__.py

debug console output:

(.venv) PS C:\Source\Azure-Functions-Python>  c:; cd 'C:\Source\Azure-Functions-Python'; & 'C:\Source\Azure-Functions-Python\.venv\Scripts\python.exe' 'c:\Users\myuser\.vscode\extensions\ms-python.python-2021.11.1422169775\pythonFiles\lib\python\debugpy\launcher' '55726' '--' 'C:\Source\Azure-Functions-Python\HistorianHttpTrigger\__init__.py'

I'm not sure if this has to do with VS Code or python path or something. Not sure if anyone can create a new function app project in VS Code and setup a shared sibling directory and create a foo.py and try to import and call it from an HttpTrigger

I have a project File structure like this:

<project_root>/
 | - .venv/
 | - .vscode/
 | - my_first_function/
 | | - __init__.py
 | | - function.json
 | | - example.py
 | - my_second_function/
 | | - __init__.py
 | | - function.json
 | - shared_code/
 | | - __init__.py
 | | - my_first_helper_function.py
 | | - my_second_helper_function.py
 | - tests/
 | | - test_my_second_function.py
 | - .funcignore
 | - host.json
 | - local.settings.json
 | - requirements.txt
Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,321 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. MughundhanRaveendran-MSFT 12,431 Reputation points
    2021-12-07T17:12:11.42+00:00

    anonymous user-8967 ,

    When developing locally using the Azure Functions Core Tools or Visual Studio Code, add the names and versions of the required packages to the requirements.txt file and install them using pip.

    pip install -r requirements.txt

    Reference : https://learn.microsoft.com/en-us/azure/azure-functions/functions-reference-python?tabs=azurecli-linux%2Capplication-level#package-management

    So please make sure you have added the dependency in the requirements.txt file and run the pip command in the terminal.

    If you have followed the above steps and still facing the issue, please let me know and I will help you in resolving this issue.

    0 comments No comments