0

CloudFunctions does not support private dependencies. Following their recommendation (https://cloud.google.com/functions/docs/writing/specifying-dependencies-python#using_private_dependencies) I downloaded the package via:

pip install -t vendor foo
touch vendor/__init__.py

Which results in a directory:

vendor
|- foo-0.0.1.dist-info
   |- INSTALLER
   |- METADTA
   |- RECORD
   |- REQUESTED
   |- top_level.txt
   |- WHEEL
|- __init__.py

Now trying to import vendor.foo results in an error:

ModuleNotFoundError: No module named 'vendor.foo

Is there an import subtlety I am missing or how is this supposed to work?

abergmeier
  • 13,224
  • 13
  • 64
  • 120

2 Answers2

0

There are multiple ways to do this.

  • relative imports, see here, also the guide you linked uses relative imports. If you import as import vendor.foo it will only work if you run the python script in the folder, where also the 'vendor' folder is.

  • adding the package location to PYTHONPATH environment variable. export PYTHONPATH = $PYTHONPATH":/path/to/vendor. This can also be done inside a python script using sys.path. I'm not sure how this is done in case of Gcloud

  • installing the package to a virtual environment. As shown here you can setup a virtual environment. One you activated this you should be able to pip install packages to it.

First approach should definatly work for you.

T. J.
  • 21
  • 4
  • _also the guide you linked uses relative import_ As far as I can see quite the contrary - it uses absolute imports. – abergmeier Aug 24 '21 at 17:16
  • _it will only work if you run the python script in the folder, where also the 'vendor' folder is._ Yes and that is exactly my situation. – abergmeier Aug 24 '21 at 17:16
0

The problem was in foo. Forgot a toplevel __init__.py, package autodetection failed and it added no sources to the wheel.

Thus when downloading later, it created no package directory.

abergmeier
  • 13,224
  • 13
  • 64
  • 120