Azure Functions dont deploy when I try to import pymssql

Aamir Mogra 20 Reputation points
2024-11-27T15:53:26.43+00:00

I want my azure functions to be able to connect to my azure SQL server/database.

I have configured the connection using SQL username and password and this works as expected when running locally.

However, when I try to deploy my functions to azure, they dont register when I include import pymssql in my script. When I comment this out, they deploy successfully.

I have included pymssql at version 2.2.5 in my requirements.txt and my function app is running on python 3.11

Thanks!

edit- the functions don’t deploy whenever I try any import such as import pandas. These are included in my requirements.txt which is the root of my directory and .yml

Azure SQL Database
Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,185 questions
{count} votes

Accepted answer
  1. Vinodh247 25,046 Reputation points MVP
    2024-12-01T14:26:40.71+00:00

    Hi ,

    Thanks for reaching out to Microsoft Q&A.

    The issue you're encountering is likely related to the deployment process of your Azure Functions app. When you include additional Python packages (example: pymssql or pandas), Azure Functions needs to ensure those dependencies are correctly installed and included during the deployment. Here's how you can troubleshoot and resolve the issue:

    Steps to Fix the Deployment Issue:

    1. Ensure Correct Dependency Management
      • Verify your requirements.txt includes all the dependencies your function needs, such as:
             
             
             pymssql==2.2.5
        

    pandas ```

     - Place `requirements.txt` in the root directory of your Azure Function app.
     
     **Check Python Version Compatibility**
     
        - Ensure that the version of Python (3.11) you are using in Azure is compatible with the libraries listed in your `requirements.txt`.
        
           - Note: Some libraries may not yet fully support Python 3.11. You can check library compatibility in their documentation or try using Python 3.9 as a fallback (Python 3.9 is well-supported in Azure Functions).
           
    
    1. Enable Remote Build for Dependencies
      • By default, Azure Functions on Linux uses remote build to install dependencies. Ensure remote build is enabled in your deployment settings. To do so:
             bash
             
             func azure functionapp publish <FUNCTION_APP_NAME> --build remote
        
        • Alternatively, set the --build remote flag when deploying via Azure CLI or in your CI/CD pipeline.
    2. Create a .python_packages Folder Locally
      • If you are deploying from your local machine without remote build, ensure dependencies are pre-installed. Run:
             bash
             
             pip install -r requirements.txt --target=.python_packages/lib/site-packages
        
        • Ensure the .python_packages directory is included in your deployment.
        Check Deployment Logs
        • Review deployment logs in the Azure Portal to identify the exact cause of the failure. Logs can be found under Azure Functions App > Monitoring > Log Stream or Deployment Center.
    3. Upgrade Azure Functions Core Tools
      • If you're using Azure Functions Core Tools locally for deployment, ensure you're using the latest version:
             bash
             
             npm install -g azure-functions-core-tools@4 --unsafe-perm 
        
        • Older versions may not handle dependency installations correctly.
    4. Use App Service Storage for Large Dependencies
      • If your dependencies are large (ex: pandas), you might run into deployment size limits. In this case:
        - Enable **App Service Storage** for your Azure Function app:
        
                 - Go to **Azure Portal** > **Function App Settings** > **Configuration** > **General Settings**.
        
                          - Set `WEBSITE_RUN_FROM_PACKAGE` to `1`.
        
                                - Package your dependencies in a zip file and upload it to a storage account. Update the function app to reference this package.
        
    5. Switch to Docker for Deployment (Optional)
    • If remote builds are still causing issues, consider deploying using a custom Docker container with all dependencies pre-installed. This approach gives you full control over the environment.
      • Create a Dockerfile like this:
             
             
             FROM mcr.microsoft.com/azure-functions/python:4-python3.11
        

    COPY . /home/site/wwwroot RUN pip install -r /home/site/wwwroot/requirements.txt ```

    • Build and deploy:
           bash
           
           docker build -t <your-container-name> .
      

    docker push <your-container-name> ```

    1. Test Dependency Compatibility in Azure
      • Verify that your dependencies are compatible with Azure’s runtime environment. Azure Functions may have specific restrictions based on the underlying platform.

    Please feel free to click the 'Upvote' (Thumbs-up) button and 'Accept as Answer'. This helps the community by allowing others with similar queries to easily find the solution.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.