Azure App (using python flask) can't import any packages

Aamir Mogra 80 Reputation points
2024-12-03T09:58:36.1333333+00:00

Hi,

I am using azure app service to deploy a flask app via Azure DevOps pipelines with a YAML for CI/CD.

It works locally, however, when I try to import anything it produces the following error in the logs -

2024-12-03T09:35:10.194186052Z import pandas as pd

2024-12-03T09:35:10.194188637Z ModuleNotFoundError: No module named 'pandas'

2024-12-03T09:35:17.251Z ERROR - Container australis-energy-dev-services-app_0_682141a1 didn't respond to HTTP pings on port: 8000, failing site start. See container logs for debugging.``

Here's my app.py -

from flask import Flask
import pandas as pd

# Create a Flask application instance
app = Flask(__name__)

# Define a simple route
@app.route("/")
def home():
    return "Hello, Flask!"

# Ensure the script runs only if executed directly
if __name__ == "__main__":
    # Run the Flask development server
    app.run(debug=True, host="0.0.0.0", port=5000)

As shown, its a simple app, as soon as I comment out the pandas import, it works.

I have included pandas with a version compatible with python 3.11 in my requirements.txt and I get the same problem whenever I try to import any other package like numpy, pymssql, pyodbc etc.

My YAML includes code to install requirements.txt and my CI azure devops pipeline corretly installs based on its logs -

Installing collected packages: pytz, pymssql, azure-functions, Werkzeug, urllib3, tzdata, typing_extensions, six, numpy, MarkupSafe, itsdangerous, isodate, idna, gunicorn, click, charset-normalizer, certifi, requests, python-dateutil, Jinja2, pandas, Flask, azure-core, azure-servicebus Successfully installed Flask-2.1.2 Jinja2-3.1.4 MarkupSafe-3.0.2 Werkzeug-2.1.2 azure-core-1.31.0 azure-functions-1.21.3 azure-servicebus-7.12.3 certifi-2024.8.30 charset-normalizer-3.4.0 click-8.1.7 gunicorn-20.1.0 idna-3.10 isodate-0.7.2 itsdangerous-2.2.0 numpy-1.26.4 pandas-2.1.3 pymssql-2.3.2 python-dateutil-2.9.0.post0 pytz-2024.2 requests-2.32.3 six-1.16.0 typing_extensions-4.12.2 tzdata-2024.2 urllib3-2.2.3

Finishing: Install dependencies

Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
8,930 questions
{count} votes

Accepted answer
  1. ajkuma 28,036 Reputation points Microsoft Employee Moderator
    2024-12-03T19:33:40.6033333+00:00

    Aamir Mogra, Just to highlight, the ModuleNotFoundError typically occurs when the module or package you're trying to import is not found.
    I understand you have checked the configuration and the webapp works fine locally.

    This can happen for several reasons, such as it not being listed in the requirements.txt file, not being installed in the site-packages directory during deployment, or it relying on a native library (.so files) that is missing in the container environment.

    if you haven't done, please validate that packages are actually being installed via the stdout from your terminal. Kindly ensure that you are deploying from the root of your project relative to your requirements.txt and Python entrypoint. If you are deploying from outside your main project folder (such as a parent folder on accident), then Oryx will not know how to build your application and you’ll likely encounter ModuleNotFoundError at runtime.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. ajkuma 28,036 Reputation points Microsoft Employee Moderator
    2024-12-03T19:38:36.2466667+00:00

    Aamir Mogra, Additionally, please checkout this:

    I see you have added the port: host="0.0.0.0", port=5000), but the error shows pings on : HTTP pings on port: 8000.

    •  Need to update the application code to listen correct port.
    •  Applications deployed to App Service Linux should not be attempting to bind to localhost or 127.0.0.1.

    If you’re running a (custom container) with Web App for Containers, use WEBSITES_PORT. Ensure that this is set to the port that’s also exposed in your Dockerfile through the EXPOSE instruction Vs On App Service Linux, using (Blessed Images) - use PORT, which directly influences the container port of the docker run command. See: Whats-the-difference-between-PORT-and-WEBSITES_PORT/index.html

    1 person found this answer helpful.
    0 comments No comments

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.