2

I'm trying to deploy a cloud function with Python 3.9 but when I run

gcloud functions deploy my_function --project my_project --runtime python39 --trigger-resource bucket_name --trigger-event google.storage.object.finalize

the deploy fails with this error:

Traceback (most recent call last):
  File "/layers/google.python.pip/pip/bin/functions-framework", line 8, in <module>
    sys.exit(_cli())
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/click/core.py", line 1128, in __call__
    return self.main(*args, **kwargs)
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/click/core.py", line 1053, in main
    rv = self.invoke(ctx)
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/click/core.py", line 1395, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/click/core.py", line 754, in invoke
    return __callback(*args, **kwargs)
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/functions_framework/_cli.py", line 37, in _cli
    app = create_app(target, source, signature_type)
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/functions_framework/__init__.py", line 288, in create_app
    spec.loader.exec_module(source_module)
  File "<frozen importlib._bootstrap_external>", line 843, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/workspace/main.py", line 2, in <module>
    import fitz
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/fitz/__init__.py", line 1, in <module>
    from frontend import *
ModuleNotFoundError: No module named 'frontend'

I found a similar problem here but how can i install PyMuPDF in the cloud function? Shouldn't it install it by itself via the requirements.txt file?

Here's my directory structure:

── folder
   ├── main.py
   ├── requirements.txt

Here's my main.py:

import numpy as np
import fitz
import os
from google.cloud import storage

def my_function()
    do stuff with fitz

Here's my requirements.txt file:

cachetools==4.2.4
certifi==2021.10.8
charset-normalizer==2.0.9
ci-info==0.2.0
click==8.0.3
configobj==5.0.6
configparser==5.2.0
etelemetry==0.2.2
filelock==3.4.0
fitz==0.0.1.dev2
future==0.18.2
google-api-core==2.3.0
google-auth==2.3.3
google-cloud-core==2.2.1
google-cloud-storage==1.43.0
google-crc32c==1.3.0
google-resumable-media==2.1.0
googleapis-common-protos==1.54.0
httplib2==0.20.2
idna==3.3
isodate==0.6.0
lxml==4.6.4
networkx==2.6.3
nibabel==3.2.1
nipype==1.7.0
numpy==1.21.4
packaging==21.3
pandas==1.3.4
pathlib==1.0.1
protobuf==3.19.1
prov==2.0.0
pyasn1==0.4.8
pyasn1-modules==0.2.8
pydot==1.4.2
PyMuPDF==1.19.2
pyparsing==3.0.6
python-dateutil==2.8.2
pytz==2021.3
pyxnat==1.4
rdflib==6.0.2
requests==2.26.0
rsa==4.8
scipy==1.7.3
simplejson==3.17.6
six==1.16.0
traits==6.3.2
urllib3==1.26.7
joblib~=1.1.0

I created this list through pip freeze> requirements.txt

Patrick
  • 33
  • 1
  • 5

1 Answers1

3

In the thread you linked, there are several solutions, and an interesting one seems to be that a package named fitz conflicts with PyMuPDF, as they both use the same top name inside a script (being fitz). I see both libraries are in your requirements.txt, so this could be the cause of this error. I tested adding both libraries inside a Cloud Function and received the same error, which was resolved after removing fitz 0.0.1.dev2 from the file, and using only PyMuPDF.

You can see another example of this behavior from this GitHub issue.

ErnestoC
  • 2,660
  • 1
  • 6
  • 19
  • Thanks man, I removed `fitz 0.0.1.dev2` from the `requirements.txt` file and everything works perfectly. – Patrick Dec 10 '21 at 17:08