Google Cloud Function - ImportError: cannot import name 'pubsub' from 'google.cloud' (unknown location)
Your main.py
file and requirements.txt
file should be in the same directory, and this should also be the same directory you're deploying your function from.
Also, the google-cloud
package is deprecated and shouldn't be used with other google-cloud-*
packages. You should remove it from your requirements.txt
file.
Jed
About Me I'm an Associate Director of Data Science with a strong background in industrial businesses. I worked in manufacturing for ~10 years, then transitioned to work on Advanced Analytics, Machine Learning, Data Science projects in the industrial & supply-chain spaces as well.
Updated on June 19, 2022Comments
-
Jed almost 2 years
I am deploying a Google Cloud Function that will kick off other Google Cloud Functions using
google.cloud.pubsub_v1
and I'm getting this errorImportError: cannot import name 'pubsub' from 'google.cloud' (unknown location)
The beginning of my requirements.txt file looks like this
# Function dependencies, for example: # package>=version google-cloud-pubsub ....
The beginning of my main.py script looks like this:
import base64 import json from google.cloud import pubsub_v1 publisher = pubsub_v1.PublisherClient() topic_path = publisher.topic_path(<PROJECT_ID>, <PUBSUB_TOPIC>)
I am deploying this code from a Google Cloud Source Repository. I have read through this SO post about my error, but that question appears to be about this error arising in a Client application. My error is being generated by the Google Cloud function itself during the deploy process. I don't have
sudo
rights to the auto-created VM that Google is using to run my process, do I? I should be able to resolve this issue from therequirements.txt
file, but nothing I've tried seems to be working.What's more frustrating is that when I put this same code in the "Inline editor" on the web-based Google Function editor, I don't get an error. I only get this error when loading the code from the repository.
The current file structure in the repository looks something like this:
. ├── package | ├── main.py | ├── script1.py | └── script2.py ├── package2 ├── ... └── requirements.txt
I moved main.py inside of a package because of issues I was having in this SO Question
Any ideas on how to resolve this import error?
-
Jed over 5 yearsI get the same error with or without the
google-cloud
package in therequirements.txt
file. -
Dustin Ingram over 5 yearsI see you're using Cloud Source Repositories. Are you making sure to re-deploy the function after pushing a new commit to the repository? It won't auto-deploy.
-
Jed over 5 yearsyes, I push to my repository, then I run the
gcloud deploy ...
command in my terminal and that is when I get this error, during the deployment of the function. -
Dustin Ingram over 5 yearsDo you have a
lib
folder in your repository that might contain other dependencies? -
Jed over 5 yearsNope, no
lib
folder with other dependencies. -
Jed over 5 yearsAny other ideas of things I should be looking into to resolve this issue?
-
Dustin Ingram over 5 yearsWhen you're deploying, are you deploying from the same directory as your
requirements.txt
file? -
Jed over 5 yearsThat did it! The
requirements.txt
file needs to be in the same directory asmain.py
. I moved mine into thepackage
directory. Will you update your answer to include this information and I'll mark it as the solution?