Python Azure function can't load pyodbc under Linux
See original GitHub issueI built an Azure function in Python that I’m deploying to a Linux function app (consumption type). I use the standard Azure Devops pipeline to deploy the function. However, when I run the function it gives me this error:
Result: Failure Exception: ModuleNotFoundError: No module named 'pyodbc'. Troubleshooting Guide: https://aka.ms/functions-modulenotfound Stack: File "/azure-functions-host/workers/python/3.9/LINUX/X64/azure_functions_worker/dispatcher.py", line 284, in _handle__function_load_request func = loader.load_function( File "/azure-functions-host/workers/python/3.9/LINUX/X64/azure_functions_worker/utils/wrappers.py", line 42, in call raise extend_exception_message(e, message) File "/azure-functions-host/workers/python/3.9/LINUX/X64/azure_functions_worker/utils/wrappers.py", line 40, in call return func(*args, **kwargs) File "/azure-functions-host/workers/python/3.9/LINUX/X64/azure_functions_worker/loader.py", line 76, in load_function mod = importlib.import_module(fullmodname) File "/usr/local/lib/python3.9/importlib/__init__.py", line 127, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "/home/site/wwwroot/RecreateStoredProcedure/__init__.py", line 2, in <module> import pyodbc
My function (simplified) looks like this:
import logging
import pyodbc
import azure.functions as func
username = "username"
password = "password"
driver = '{ODBC Driver 17 for SQL Server}'
def main(req: func.HttpRequest) -> func.HttpResponse:
databaseName = req.params.get('databaseName')
if (not databaseName):
serverPath = 'asdasdasd.database.windows.net'
con = pyodbc.connect('Driver={SQL Server};'
'Server='+serverPath+';'
'Database='+databaseName+';'
'UID='+username+';'
'PWD='+password
return func.HttpResponse(f"Successful.")
else:
return func.HttpResponse("Error.",status_code=500)
The requirements.txt file looks like this:
azure-functions
pyodbc==4.0.30
Finally, my devops pipeline looks like this (again, it’s built using the default Azure Function Python template):
stages:
- stage: Build
displayName: Build stage
jobs:
- job: Build
displayName: Build
pool:
vmImage: $(vmImageName)
steps:
- bash: |
if [ -f extensions.csproj ]
then
dotnet build extensions.csproj --runtime ubuntu.16.04-x64 --output ./bin
fi
workingDirectory: $(workingDirectory)
displayName: 'Build extensions'
- task: UsePythonVersion@0
displayName: 'Use Python 3.9'
inputs:
versionSpec: 3.9 # Functions V2 supports Python 3.6 as of today
- bash: |
python -m venv worker_venv
source worker_venv/bin/activate
pip install -r requirements.txt
workingDirectory: $(workingDirectory)
displayName: 'Install application dependencies'
- task: ArchiveFiles@2
displayName: 'Archive files'
inputs:
rootFolderOrFile: '$(workingDirectory)'
includeRootFolder: false
archiveType: zip
archiveFile: $(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip
replaceExistingArchive: true
- publish: $(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip
artifact: drop
- stage: Deploy
displayName: Deploy stage
dependsOn: Build
condition: succeeded()
jobs:
- deployment: Deploy
displayName: Deploy
environment: 'development'
pool:
vmImage: $(vmImageName)
strategy:
runOnce:
deploy:
steps:
- task: AzureFunctionApp@1
displayName: 'Azure functions app deploy'
inputs:
azureSubscription: '$(azureSubscription)'
appType: functionAppLinux
appName: $(functionAppName)
package: '$(Pipeline.Workspace)/drop/$(Build.BuildId).zip'
I tried the following things:
- Creating a Windows function app instead (not possible, it’s greyed out)
- Trying it with Docker (not possible, consumption plan is not available for Docker for whatever reason)
- I tried pypyodbc (not working locally, so not a solution)
- I created a separate release pipeline where I tried this: https://clemenssiebler.com/deploy-azure-functions-python-azure-devops/ (this also doesn’t work, there is an unspecified error while trying to install Function tools: Script failed with exit code: 127)
It’s super weird, creating a Python function that runs on Linux and connects with a database is a super super standard scenario I would say. Why does this not work out of the box? How can I solve this?
Issue Analytics
- State:
- Created 3 years ago
- Comments:6
Top Results From Across the Web
Linux Python Azure Function APP - pyodbc module not found ...
I have added pyodbc in the requirements.txt file and other modules in this file such as azure-storage are working fine. Has anyone seen...
Read more >Linux Python Function APP - pyodbc module not found ...
I'm using a Linux consumption plan. I get the following error; Exception: ModuleNotFoundError: No module named 'pyodbc'.
Read more >Troubleshoot Python errors in Azure Functions - Microsoft Learn
This error occurs when a Python function app fails to load a Python module. The root cause for this error is one of...
Read more >Step 3: Proof of concept connecting to SQL using pyodbc
Step 3 is a proof of concept, which shows how you can connect to SQL Server using Python and pyODBC. The basic examples...
Read more >Step 1: Configure pyodbc Python environment - SQL
Linux · Open terminal. · Install Microsoft ODBC Driver for SQL Server on Linux. · Install pyodbc. Bash Copy. sudo -H pip install...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Thank you for getting back to me @stefanushinardi 😃 That did indeed work! My problem is solved now. Should this be reported to the Azure DevOps team so they can fix the template?
I know this is closed, but where do we go to ask the Azure DevOps team so they can fix the template?