20

How can I import modules for a Python Azure Function?

import requests

Leads to:

2016-08-16T01:02:02.317 Exception while executing function: Functions.detect_measure. Microsoft.Azure.WebJobs.Script: Traceback (most recent call last):
  File "D:\home\site\wwwroot\detect_measure\run.py", line 1, in <module>
    import requests
ImportError: No module named requests

Related, where are the modules available documented?

Related question with fully documented answer Python libraries on Web Job

BlackTigerX
  • 6,006
  • 7
  • 38
  • 48
Ryan Galgon
  • 431
  • 1
  • 3
  • 9
  • Did you install requests, and then include them with your Azure Functions code? – David Makogon Aug 16 '16 at 01:18
  • I'm trying to work directly from the portal.azure.com site, so entering the code directly into the function interface for run.py – Ryan Galgon Aug 16 '16 at 01:22
  • Looks like there's not a way to do it directly from the Azure portal, but following the instructions here: http://nicholasjackson.github.io/azure/python/python-packages-and-azure-webjobs/ I was able to create a site-packages folder with all the requisite files. – Ryan Galgon Aug 16 '16 at 01:55
  • @RyanGalgon The link you gave is not working anymore. Would you have the steps to install requests module? – Gandhali Samant Jan 10 '17 at 05:24
  • @RyanGalgon - Do you have the updated URL? It's vital we have it please. – Anonymous Person Nov 04 '19 at 19:04

3 Answers3

17

You need to include a requirements.txt file with your code which lists all the python dependencies of your function

From the docs: https://learn.microsoft.com/en-us/azure/azure-functions/functions-reference-python#python-version-and-package-management

For example, your reqirements.txt file would contain:

requests==2.19.1
Zain Rizvi
  • 23,586
  • 22
  • 91
  • 133
  • Hi, do you know how to upload this file to Azure Function ? – Vincent G Nov 08 '22 at 14:37
  • 1
    @VincentG : from the Azure Portal go to your Function App -> App Files -> requirements.txt, and you can edit there directly – Ryan Dec 29 '22 at 15:29
  • ...also run 'pip3 install -r requirements.txt' in the terminal within the directory for the azure function to install all the stuff that you added to the requirements.txt file – thatguy1155 Jun 15 '23 at 18:41
6

Python support is currently experimental for Azure Functions, so documentation isn't very good.

You need to bring your own modules. None are available by default on Azure Functions. You can do this by uploading it via the portal UX or kudu (which is handy for lots of files).

You can leave comments on which packages you'd like, how you'd like to manage you packages here on the tracking issue for "real" Python support - https://github.com/Azure/azure-webjobs-sdk-script/issues/335

Chris Anderson
  • 8,305
  • 2
  • 29
  • 37
  • 1
    Thanks. One suggestion would be to make it more clear on the overview page (https://azure.microsoft.com/en-us/documentation/articles/functions-overview/) which languages are not fully supported. It would have saved me some time. – Ryan Galgon Aug 17 '16 at 00:12
  • @Chris Anderson-MSFT to get `requests` working, which files are needed (with a link to get them from), and what is the code required to make requests loaded from within the python script? – Adrian Torrie Nov 18 '16 at 08:10
2

Install python packages from the python code itself with the following snippet:

def install(package):
    # This function will install a package if it is not present
    from importlib import import_module
    try:
        import_module(package)
    except:
        from sys import executable as se
        from subprocess import check_call
        check_call([se,'-m','pip','-q','install',package])


for package in ['requests','hashlib']:
    install(package)

Desired libraries mentioned the list gets installed when the azure function is triggered for the first time. for the subsequent triggers, you can comment/ remove the installation code.