chiru
chiru

Reputation: 997

Issue with Python Flask App Deployment to Azure App Service During Zip Deployment

I'm facing an issue with deploying my Python Flask app to Azure App Service(Python 3.9) using zip deployment. Below are the details:

Directory structure:

.
|---app.py    
|---requirements.txt

Working Code:

app.py:

from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello():
    return "Hello, Azure!--1"

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8000)

requirements.txt:

Flask==3.1.0

Non-Working Code:

app.py:

from flask import Flask
import openai

app = Flask(__name__)

@app.route('/')
def hello():
    return "Hello, Azure!--2"

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8000)

requirements.txt:

Flask==3.1.0
openai==1.57.1

Steps for deploying:

Create zip:

Command to deploy into Azure app service:

curl -k -v -X POST -H 'Content-Type: application/zip' -u "<$app_service_user>:<60character_length_userPWD>" --data-binary @app.zip https://<appservicename>.scm.dev001.ase.frk.com/api/zipdeploy

Error in the API log stream:

Site's appCommandLine: gunicorn --bind=0.0.0.0:8000 app:app
Launching oryx with: create-script -appPath /home/site/wwwroot -output /opt/startup/startup.sh -virtualEnvName antenv -defaultApp /opt/defaultsite -userStartupCommand 'gunicorn --bind=0.0.0.0:8000 app:app'
Could not find build manifest file at '/home/site/wwwroot/oryx-manifest.toml'
Could not find operation ID in manifest. Generating an operation id...
Build Operation ID: c9b33d68-8d6d-4326-9b08-257fe923d135
Oryx Version: 0.2.20240619.2, Commit: cf006407a02b225f59dccd677986973c7889aa50, ReleaseTagName: 20240619.2
Writing output script to '/opt/startup/startup.sh'
WARNING: Could not find virtual environment directory /home/site/wwwroot/antenv.
WARNING: Could not find package directory /home/site/wwwroot/__oryx_packages__.



Booting worker with pid: 1075
[1075] [ERROR] Exception in worker process
Traceback (most recent call last):
File "/opt/python/3.9.19/lib/python3.9/site-packages/gunicorn/arbiter.py", line 609, in spawn_worker
  worker.init_process()
File "/opt/python/3.9.19/lib/python3.9/site-packages/gunicorn/workers/base.py", line 134, in init_process
  self.load_wsgi()
File "/opt/python/3.9.19/lib/python3.9/site-packages/gunicorn/workers/base.py", line 146, in load_wsgi
  self.wsgi = self.app.wsgi()
File "/opt/python/3.9.19/lib/python3.9/site-packages/gunicorn/app/base.py", line 67, in wsgi
  self.callable = self.load()
File "/opt/python/3.9.19/lib/python3.9/site-packages/gunicorn/app/wsgiapp.py", line 58, in load
  return self.load_wsgiapp()
File "/opt/python/3.9.19/lib/python3.9/site-packages/gunicorn/app/wsgiapp.py", line 48, in load_wsgiapp
  return util.import_app(self.app_uri)
File "/opt/python/3.9.19/lib/python3.9/site-packages/gunicorn/util.py", line 371, in import_app
  mod = importlib.import_module(module)
File "/opt/python/3.9.19/lib/python3.9/importlib/__init__.py", line 127, in import_module
  return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 850, in exec_module
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "/home/site/wwwroot/app.py", line 2, in <module>
    import openai
ModuleNotFoundError: No module named 'openai'
[1075] [INFO] Worker exiting (pid: 1075)
[1064] [ERROR] Worker (pid:1075) exited with code 3
[1064] [ERROR] Shutting down: Master
[1064] [ERROR] Reason: Worker failed to boot.

I've verified that the code works locally, but it fails when deployed to Azure App Service. Any insights or suggestions on what might be causing this issue would be greatly appreciated!

Thanks in advance!

Upvotes: 0

Views: 115

Answers (1)

Ikhtesam Afrin
Ikhtesam Afrin

Reputation: 6464

I have referred to the documentation available for creating and deploying sample Flask application and cloned it locally in vs code.

I also have used the same code provided by you a long with Flask and openai modules in requirements.txt file.

Then, I created the virtual environment and activated it using below commands.

py -m venv .venv 
.venv\scripts\activate

Installed the dependencies using pip install -r requirements.txt command.

It worked for me locally alike you.

enter image description here

I have created a Linux Azure App service with runtime as python 3.9.

Then, I deployed the code using vs code.

enter image description here

enter image description here

I am able to access the application successfully.

enter image description here

You could be getting this issue due to improper deployment. As its showing python could not find virtual environment directory /home/site/wwwroot/antenv warning for you which means either virtual environment is not created or it can't find it. So, you can deploy using any other alternate way like vs code or cli command.

Upvotes: 0

Related Questions