Joseph Markiewicz
Joseph Markiewicz

Reputation: 75

How to install python module in Azure Function Linux app

I am trying to install modules for my python script in an Azure function. I have read documentation on using Kudu tools to pip install. However, this is not allowed on Linux apps. Also, it seems Microsoft has depreciated support for Python Azure Functions to run on Windows OS. Below is what I have so far.

Edit Update: I did more research and found the way to install the python modules is through use of a venv, then deploy the venv (with pip packages installed) to the Azure Function. I created the venv in the folder path with the pip packages installed, but still an issue is persisting.

Folder Structure

 -FunctionApp
    -MainFunction
      -_init_.py
      -functions.json
 -Requirements.txt

The Requiremtents.txt file contains:

psycopg2 == 2.8.5
yfinance == 0.1.54

The Init.py file contains the following code to import the modules referenced in the Requirements.txt file:

import psycopg2
import yfinance

When I try to run the function in the Azure Portal I get the following error:

Result: Result: Failure Exception: ImportError: libpq.so.5: cannot open shared object file: No such file or directory Stack: File "/azure-functions-host/workers/python/3.7/LINUX/X64/azure_functions_worker/dispatcher.py", line 242, in _handle__function_load_request func_request.metadata.entry_point) File "/azure-functions-host/workers/python/3.7/LINUX/X64/azure_functions_worker/loader.py", line 66, in load_function mod = importlib.import_module(fullmodname) File "/usr/local/lib/python3.7/importlib/__init__.py", line 127, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "<frozen importlib._bootstrap>", line 1006, in _gcd_import File "<frozen importlib._bootstrap>", line 983, in _find_and_load File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed File "<frozen importlib._bootstrap>", line 1006, in _gcd_import File "<frozen importlib._bootstrap>", line 983, in _find_and_load File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 677, in _load_unlocked File "<frozen importlib._bootstrap_external>", line 728, in exec_module File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed File "/home/site/wwwroot/Navi_Function_Notifier/__init__.py", line 3, in <module> import psycopg2 File "/home/site/wwwroot/.python_packages/lib/site-packages/psycopg2/__init__.py", line 51, in <module> from psycopg2._psycopg import ( # noqa

If anyone has been able to import a python module within an Azure Function on Linux I would greatly appreciate the guidance!

Upvotes: 5

Views: 9885

Answers (1)

Hury Shen
Hury Shen

Reputation: 15734

It seems your "requirements.txt" is in the wrong location, the "requirement.txt" file should be in the function app folder but in your folder structure it locates coordinate with the "FunctionApp" folder. Below I provide all of the steps of how to install the modules in python function and deploy it to azure for your reference.

1. Run the pip install command to install the modules in python function in local.

pip install psycopg2
pip install yfinance

2. Generate the "requirements.txt" file by running the command below:

pip freeze > requirements.txt

enter image description here

We can see the "requirements.txt" was generated automatically and its location shown as below screenshot. enter image description here

3. Run the below command to deploy your function from local to azure.

func azure functionapp publish <function app name> --build remote

The should be the name of the function app which you created on azure portal. After running the command, it will deploy your function to azure and install the modules according to the modules list in "requirements.txt".

By the way:

If the steps above don't work, you can try to deploy the function by "build local", run this command:

func azure functionapp publish <function app name> --build local

This command will build your function in local and deploy it to azure(including all of the modules).

Hope it helps~

Upvotes: 4

Related Questions