How to add Python package to Azure Function?

Python Snake 0 Reputation points
2023-01-23T22:04:33.6833333+00:00

I am new to Azure, how do I get to install a Python package for an App Function? I followed the steps to add the required package into the requirement.txt file in the 'wwwroot folder. However this does not work. I also tried installing the package directly in the code (init.py` )but that doesn't work wither.

Also, I cannot access the Kudu console.

How do install the required python packages I need for my code?

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,321 questions
{count} votes

4 answers

Sort by: Most helpful
  1. Amir Katz 65 Reputation points
    2023-01-31T07:14:48.29+00:00

    Regarding Kudu, please note that it seems to be severely degraded if your Function App is on Linux. Here is what I've observed:

    1. There are no Platform feature tab in the Function App left navigation menu
    2. When going to the Kudu website (https://<Func-App-Name>.scm.azurewebsites.net), it seems to run Kudu Lite (the link next to the Build link)
    3. There are only two links under REST API, which are just read-only JSON data2023-01-31 09_12_01-Window
    2 people found this answer helpful.
    0 comments No comments

  2. Mike Urnun 9,756 Reputation points Microsoft Employee
    2023-01-30T23:48:59.41+00:00

    Hello @Python Snake - Thanks for reaching out and we're sorry for the inconvenience you're experiencing with not being able to use Python dependencies smoothly. Troubleshooting experience does get a bit confusing & can seem complicated with the mix of the following categories needing to be factored in:

    1. Specific deployment method (Remote Build, Run From Package, Zip Deploy)
    2. Target Azure hosting plan (Consumption, ASP, Premium)
    3. Target OS (Windows/Linux)
    4. Specific Python Package having the issue with.

    Because this a common pain point, the most effective way to overcome this issue would be to review our dedicated troubleshooting guide below & locate the root cause methodically across the categories listed above in an efficient way:

    If you're still hitting the same issue despite trying out everything in the guides above, please let me know in the comment and I'd be happy to help you get unblocked with more 1:1 support.

    1 person found this answer helpful.

  3. Ahmed Rauf 25 Reputation points
    2023-05-19T12:59:11.36+00:00

    I personally would recommend using the Vscode Azure Functions and Azure Resources Extension, using which you can create the Consumption based Function App and a local python project which can be deployed directly to the Function App through Vscode.

    Some things to make sure -

    • Both Function App and your local python function should have same python version
    • add requirements.txt in your local project's root directory
    • Once your local function is created, make sure there is a file in your .vscode folder called tasks.json with this script
    {
      "version": "2.0.0",
      "tasks": [
        {
          "type": "func",
          "label": "func: host start",
          "command": "host start",
          "problemMatcher": "$func-python-watch",
          "isBackground": true,
          "dependsOn": "pip install (functions)"
        },
        {
          "label": "pip install (functions)",
          "type": "shell",
          "osx": {
            "command": "${config:azureFunctions.pythonVenv}/bin/python -m pip install -r requirements.txt"
          },
          "windows": {
            "command": "${config:azureFunctions.pythonVenv}\\Scripts\\python -m pip install -r requirements.txt"
          },
          "linux": {
            "command": "${config:azureFunctions.pythonVenv}/bin/python -m pip install -r requirements.txt"
          },
          "problemMatcher": []
        }
      ]
    }
    

    This is how I always run Python on Azure Functions and never came across a module problem

    1 person found this answer helpful.
    0 comments No comments

  4. Nitish Sharma 336 Reputation points
    2023-01-23T23:42:49.8266667+00:00

    In Azure Functions, you can install Python packages by adding them to the requirements.txt file in the function app's root directory. Here are the steps you can follow:

    In the Azure portal, navigate to your function app.

    Under the Platform features tab, select "Advanced tools (Kudu)"

    In the Kudu console, navigate to the "Debug console" -> "CMD"

    Go to the "site" -> "wwwroot" directory

    Create a new file called "requirements.txt" (if it does not exist)

    In the requirements.txt file, add the packages that you want to install, one per line. For example, if you want to install the package "requests", you would add the line "requests" to the requirements.txt file

    Save the requirements.txt file

    In the Kudu console, navigate back to the "site" directory

    Run the command "pip install -r wwwroot/requirements.txt"

    Alternatively, you can also install packages in a function app by adding them to the requirements.txt file and then deploying the function app to Azure. The packages will be automatically installed when the function app is deployed.

    You can also use Azure Function's built-in support for Python packages. You can create a new file called "function.json" and add the package name in the "dependencies" field.

    You can also install packages in the function.py file