Error deploying Python Flask template to cloud run

I created a project with the python flask example and it runs fine in the editor. Great with the live preview.

I run the deploy command from the Project IDX menu to deploy to cloud run.
When I navigate to the site:
https://idx-pythonflaskfinance-2610882-j4bn5fufta-nn.a.run.app/

I get the message: Service Unavailable

I get this error in the logs:
Traceback (most recent call last):
File “/layers/google.python.pip/pip/lib/python3.12/site-packages/gunicorn/arbiter.py”, line 609, in spawn_worker
worker.init_process()
File “/layers/google.python.pip/pip/lib/python3.12/site-packages/gunicorn/workers/base.py”, line 134, in init_process
self.load_wsgi()
File “/layers/google.python.pip/pip/lib/python3.12/site-packages/gunicorn/workers/base.py”, line 146, in load_wsgi
self.wsgi = self.app.wsgi()

Hi @Kenneth_Haugland - does the deployment itself show any errors in the IDX output or terminal windows?

Hi Kenneth, I’m trying to repro this issue. Looks like the server isn’t coming up. Can you help us by including the full logs please?

Also if possible, can you share your main.py please (or where app.run is defined)?

Sorry for the late answer:

I did no changes to the default template for flask application:
main.py
import os

from flask import Flask, send_file

app = Flask(name)

@app.route(“/”)
def index():
return send_file(‘index.html’)

def main():
app.run(port=int(os.environ.get(‘PORT’, 80)))

if name == “main”:
main()


Error in the Logs Explorer:
Traceback (most recent call last):
File “/layers/google.python.pip/pip/lib/python3.12/site-packages/gunicorn/arbiter.py”, line 609, in spawn_worker
worker.init_process()
File “/layers/google.python.pip/pip/lib/python3.12/site-packages/gunicorn/workers/base.py”, line 134, in init_process
self.load_wsgi()
File “/layers/google.python.pip/pip/lib/python3.12/site-packages/gunicorn/workers/base.py”, line 146, in load_wsgi
self.wsgi = self.app.wsgi()

No errors:
Executing task: (set -o pipefail && gcloud run deploy idx-pythonflaskfinance-2610882 --source . --region northamerica-northeast1 --allow-unauthenticated 2>&1 | tee /tmp/integrations.log)

source /home/user/pythonflaskfinance/.venv/bin/activate
This command is equivalent to running gcloud builds submit --pack image=[IMAGE] . and gcloud run deploy idx-pythonflaskfinance-2610882 --image [IMAGE]

Building using Buildpacks and deploying container to Cloud Run service [idx-pythonflaskfinance-2610882] in project [stockoverflow2022] region [northamerica-northeast1]
Building and deploying…
Uploading sources…done
Building Container…done
Setting IAM Policy…done
Creating Revision…done
Routing traffic…done
Done.
Service [idx-pythonflaskfinance-2610882] revision [idx-pythonflaskfinance-2610882-00003-cfj] has been deployed and is serving 100 percent of traffic.
Service URL: https://idx-pythonflaskfinance-2610882-j4bn5fufta-nn.a.run.app

  • Terminal will be reused by tasks, press any key to close it.

I found creating a new Flask/Python project deployed ok but my old one didn’t. Seems the structure of the projects have changed - e.g. differences in projects’ devserver.sh were

python -m flask --app src/main run -p $PORT --debug
vs
python -m flask --app main run -p $PORT --debug

which doesn’t affect deployment but is evidence that the location of Python files in the template has changed over the last year or so. This might have something to do with it. Maybe the Docker deploy assumes a certain project structure - just speculating.