1

I'm trying to make a simple Azure Function that receives an HTTP event, grabs a JSON object in the body of that request, and saves that object as a document in a CosmosDB collection. I seem to be getting hung up on importing a python module however.

__init__.py

import json
import azure.functions as func
import azure.cosmos.cosmos_client as cosmos_client


config = {
  'ENDPOINT': 'https://XXXXXX.documents.azure.com:443/',
  'PRIMARYKEY': 'XXXXXX',
  'DATABASE': 'my-database',
  'CONTAINER': 'my-container'
}

def main(req: func.HttpRequest) -> func.HttpResponse:
  try:
    client = cosmos_client.CosmosClient(config['ENDPOINT'], {'masterKey': config['PRIMARYKEY']})
    results = req.get_json()

    client.get_database_client(config['DATABASE']).get_container_client(config['CONTAINER']).upsert_item(results)

    return func.HttpResponse("Success", status_code=200)
  except ValueError:
    return func.HttpResponse("Please pass a JSON object", status_code=400)

Error Log

019-07-11T22:51:22.613140092Z: [INFO]  Microsoft.Azure.WebJobs.Host.FunctionInvocationException: Exception while executing function: Functions.save-to-cosmos ---> Microsoft.Azure.WebJobs.Script.Rpc.RpcException: Result: Failure
2019-07-11T22:51:22.616516971Z: [INFO]  Exception: ModuleNotFoundError: No module named 'azure.cosmos'
2019-07-11T22:51:22.616547173Z: [INFO]  Stack:   File "/usr/local/lib/python3.6/site-packages/azure/functions_worker/dispatcher.py", line 230, in _handle__function_load_request
2019-07-11T22:51:22.632613023Z: [INFO]      func_request.metadata.entry_point)
2019-07-11T22:51:22.632631824Z: [INFO]    File "/usr/local/lib/python3.6/site-packages/azure/functions_worker/loader.py", line 66, in load_function
2019-07-11T22:51:22.632637524Z: [INFO]      mod = importlib.import_module(fullmodname)
2019-07-11T22:51:22.632641725Z: [INFO]    File "/usr/local/lib/python3.6/importlib/__init__.py", line 126, in import_module
2019-07-11T22:51:22.632646025Z: [INFO]      return _bootstrap._gcd_import(name[level:], package, level)
2019-07-11T22:51:22.632650325Z: [INFO]    File "<frozen importlib._bootstrap>", line 994, in _gcd_import
2019-07-11T22:51:22.632655025Z: [INFO]    File "<frozen importlib._bootstrap>", line 971, in _find_and_load
2019-07-11T22:51:22.632660926Z: [INFO]    File "<frozen importlib._bootstrap>", line 941, in _find_and_load_unlocked
2019-07-11T22:51:22.632682827Z: [INFO]    File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
2019-07-11T22:51:22.632688427Z: [INFO]    File "<frozen importlib._bootstrap>", line 994, in _gcd_import
2019-07-11T22:51:22.632692727Z: [INFO]    File "<frozen importlib._bootstrap>", line 971, in _find_and_load
2019-07-11T22:51:22.632697128Z: [INFO]    File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
2019-07-11T22:51:22.632723529Z: [INFO]    File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
2019-07-11T22:51:22.632729029Z: [INFO]    File "<frozen importlib._bootstrap_external>", line 678, in exec_module
2019-07-11T22:51:22.632733329Z: [INFO]    File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
2019-07-11T22:51:22.632737430Z: [INFO]    File "/home/site/wwwroot/save-to-cosmos/__init__.py", line 3, in <module>
2019-07-11T22:51:22.632741830Z: [INFO]      import azure.cosmos.cosmos_client as cosmos_client

I have tried going through Kudu like this question suggested Importing Python modules for Azure Function and tried running pip install azure-cosmos but that doesn't seem to fix the problem.

Ryan Grush
  • 2,076
  • 3
  • 37
  • 64
  • [**This**](https://github.com/yokawasa/azure-functions-python-samples/tree/master/v2functions/cosmos-trigger-cosmodb-output-binding#publish-the-function-to-the-cloud) will help. Also make sure you have a `requirements.txt` for your Python dependencies in the root of your function app (where `hosts.json` and `local.settings.json` sit). To develop and run locally you'll need to activate a Python Virtual Environment - see [**this**](https://github.com/yokawasa/azure-functions-python-samples/blob/master/docs/quickstart-v2-python-functions.md#create-and-activate-a-virtual-environment). – evilSnobu Jul 12 '19 at 09:01

1 Answers1

2

You need to add python extension in your Function App KUDU.

Step1: To Install Custom Python Version

Navigate to Kudu Console - https://Your_APP_NAME.scm.azurewebsites.net/DebugConsole Run Below command in kudu cli to install Python in D:\home\site\tools Folder

nuget.exe install -Source https://www.siteextensions.net/api/v2/ -OutputDirectory D:\home\site\tools python352x64

Step2: We need to have python.exe in D:\home\site\tools Folder. so lets move sub-folder content(D:\home\site\tools\python352x64.3.5.2.6\content\Python35 ) to D:\home\site\tools using below command

mv /d/home/site/tools/python352x64.3.5.2.6/content/python35/* /d/home/site/tools/

Step3: To Install custom python package.

Navigate to Kudu Console - https://Your_APP_NAME.scm.azurewebsites.net/DebugConsole Run below command to install pandas module in function app:

D:\home\site\tools\python.exe -m pip install azure-cosmos

Test example:

I installed requests package to do a little test.

 D:\home\site\tools\python.exe -m pip install requests

enter image description here

python code:

enter image description here

Output:

enter image description here

Jay Gong
  • 23,163
  • 2
  • 27
  • 32
  • I should've mentioned I'm using Bash, do you have the equivalent for my setup? – Ryan Grush Jul 12 '19 at 01:57
  • @RyanGrush Bash steps are listed in the official document: https://learn.microsoft.com/en-us/azure/azure-functions/functions-create-first-function-python#run-the-function-locally. You need to activate a virtual environment locally and use pip install package. Then deploy it into azure env. – Jay Gong Jul 12 '19 at 02:23
  • @RyanGrush Hi,any updates now? does my answer helps you? – Jay Gong Jul 15 '19 at 08:50
  • It looks like the problem was using "Deployment Center" linked to my ADO repo which was causing the problem. Publishing a function app directly works but linking it to an ADO repo expects a pipeline in place I'm assuming. – Ryan Grush Jul 15 '19 at 13:39
  • @JayGong can you please help me on this `https://stackoverflow.com/questions/71951268/azure-function-python-module-compatibility-issue` – Stark Apr 22 '22 at 05:53