Hello everyone,
I'm deploying a Python Cloud Function on Google Cloud Run (via Cloud Functions Gen 2) that processes .xlsx Excel files. The function uses pandas.read_excel() with openpyxl as the engine.
I'm working on a Google Cloud Function (Python 3.11-tried on 3.10 also same problem) that downloads .xlsx reports from Google Drive using the Google Drive API. It uses pandas.read_excel() to parse the Excel content:
pythonCopyEditfh = io.BytesIO() request = drive_service.files().get_media(fileId=file_id) downloader = MediaIoBaseDownload(fh, request) while not done: _, done = downloader.next_chunk() fh.seek(0) df = pd.read_excel(fh, engine="openpyxl")
Even though I have added openpyxl to my requirements.txt and verified that the dependency is correctly installed (alongside a test library), the deployment fails with the following runtime error:
ImportError: Missing optional dependency 'openpyxl'. Use pip or conda to install openpyxl.
The full traceback shows the error arises when openpyxl attempts to parse XML using Python's built-in xml.etree.ElementTree, which internally depends on the missing libexpat module in the Cloud Run environment. As a result, the container fails the TCP probe and the revision never becomes ready.
Steps I’ve taken:
Verified requirements.txt is correctly processed (added test libraries and confirmed output).
Confirmed the error persists even after switching Python versions (3.10 → 3.11).
Tried installing additional parsing libraries (lxml, etc.) — no effect.
openpyxl works perfectly in my local environment but fails in the Cloud Function container.
Questions:
How can I read excel file on my cloud run?
Any guidance or official response would be greatly appreciated.
Thank you,
Hamza
Solved! Go to Solution.
Hi @hamza_Shariq,
Welcome to Google Cloud Community!
If you're deploying your Cloud Function using buildpacks with a Python runtime based on the google-22
stack, it won't include libexpat
, which is required by openpyxl
for XML parsing. To resolve this, switch to the google-22-full
stack, which includes the necessary system dependencies like libexpat
. Additionally, double-check that you're using compatible versions of openpyxl
and pandas
in your requirements.txt
to avoid any compatibility issues.
Was this helpful? If so, please accept this answer as “Solution”. If you need additional assistance, reply here within 2 business days and I’ll be happy to help.
Hi @hamza_Shariq,
Welcome to Google Cloud Community!
If you're deploying your Cloud Function using buildpacks with a Python runtime based on the google-22
stack, it won't include libexpat
, which is required by openpyxl
for XML parsing. To resolve this, switch to the google-22-full
stack, which includes the necessary system dependencies like libexpat
. Additionally, double-check that you're using compatible versions of openpyxl
and pandas
in your requirements.txt
to avoid any compatibility issues.
Was this helpful? If so, please accept this answer as “Solution”. If you need additional assistance, reply here within 2 business days and I’ll be happy to help.
That worked perfectly! Thank you!