10

Can Google Cloud Functions handle python with packages like sklearn, pandas, etc? If so, can someone point me in the direction of resources on how to do so.

I've been searching a while and it seems like this is impossible, all I've found are resources to deploy the base python language to google cloud.

Murtaza Kanchwala
  • 2,425
  • 25
  • 33
Sean
  • 213
  • 1
  • 3
  • 11

4 Answers4

15

Python 3.7 is supported now.
Steps to create one via the google cloud console:

  1. go to google cloud functions in the google cloud console and click on create function enter image description here

2.specify the function's properties enter image description here

  1. select trigger enter image description here

4.change runtime to python 3.7 enter image description here

  1. enter your cloud function logic and entry point enter image description here

  2. enter python dependencies in requirements.txt enter image description here

Adarsh
  • 3,273
  • 3
  • 20
  • 44
  • 1
    This is great. Do you know when wider support might be available? (e.g. docs and firebase?) – songololo Jul 24 '18 at 14:35
  • The docs aren't updated yet but pretty sure will be. For now i suggest you to log the cloud function params, event and context to get a feel of the data structure and construct your cloud function. – Adarsh Jul 24 '18 at 15:11
  • documentation on deploying from source control: https://cloud.google.com/functions/docs/deploying/repo – Adarsh Aug 02 '18 at 09:27
  • Currently there seems to be no support for cloud source repos for python yet as stated here: https://stackoverflow.com/questions/51664813/firestore-cloud-functions-not-triggered-when-deployed-from-cloud-source-reposito – Adarsh Aug 03 '18 at 03:32
  • Sample code to the GitHub has more than one file. How do you run all these files? And Flask Framework is compulsory to use? And can you pass arguments to the functions? – johnrao07 Nov 05 '18 at 15:08
  • 1
    @johnrao07 The sample code was written in such a way to modularize and give you a clear picture of which piece sits where. You can use this kind of method if your code sits in google cloud repo and deploy the function using gcloud. unfortunately, gcloud for firestore isn't supported yet. Hence you have 2 options for now: upload the zip file or use the inline code editor.uploading as a zip file seems like a pretty good choice here. Google runs the function you write in flask runtime, so the signature of your entry point has to be in complaince with flask. – Adarsh Nov 05 '18 at 15:16
  • 1
    @johnrao07 if your cloud function's trigger is a HTTP request, you can use query params or send json via POST. similar question: https://stackoverflow.com/questions/52233949/passing-variables-to-google-cloud-functions – Adarsh Nov 05 '18 at 15:23
  • Trigger will be from firestore database, a change/update/create in the database will trigger the function passing the data from the database as argument to the function – johnrao07 Nov 05 '18 at 15:26
  • 1
    I am afraid you don't have the control to pass any explicit param in that case AFAIK. You get all the things required such as trigger path, data before updation, data after update in case of update or data added in case of creation in event and context params of your handler. from there, you can perform any neccasary action if required. I recommend you to log the event and context params to see the datastructure and access the required information. – Adarsh Nov 05 '18 at 15:36
11

EDIT: As of July 2018 there is now a Python runtime (3.7) available for Google Cloud Functions!

OLD ANSWER: Google Cloud Functions (GCF) are written in JavaScript (executed in a Node.js runtime), so there is no way for them to actually handle Python at this moment. There is a Python module at GitHub that you might have come across and it can be used to write and deploy GCF with one of three trigger types: http, Pub/Sub and bucket. The module takes care of translating your Python logic to a JavaScript code that is later run inside Google Cloud Platform.

When it comes to other packages like pandas, the ‘translation’ into JavaScript was not prepared for them by anyone AFAIK. If you really don’t like the idea of jumping into JavaScript and writing the Cloud Function code on your own (with the logic you intended to use in a Python script), you have a possible workaround. You can evoke your Python script from inside of the Cloud Function written in JS - the idea was discussed in this topic. Another way is using Object Change Notifications or Pub/Sub Notifications as explained here.

arudzinska
  • 3,152
  • 1
  • 16
  • 29
  • 2
    any indication when Python will be supported ? There has been no sign that G is actively working on it. Should Pythoniacs look elsewhere ? – yan-hic Mar 07 '18 at 14:13
  • 1
    Currently Google is actively working on implementing Python to GCF, however it is not know when exactly that will be ready. You can follow the [GCP blog](https://cloudplatform.googleblog.com/), it surely will be announced there. – arudzinska Mar 07 '18 at 15:26
  • @YannickEinsweiler python 3.7 is supported now. But docs aren't updated yet. There is an inline code editor to enter your code and also an option to upload a zip file. As for dependencies, it can be listed in requirements.txt Although the convenience of uploading the cloud fns via command line as supported for javascript/typescript isn't available at the moment AFAIK. – Adarsh Jul 22 '18 at 18:40
  • Thanks for letting us know, I'll be checking the docs and when some official information appears I'll update my answer. – arudzinska Jul 23 '18 at 08:39
  • Google officially announced Python 3.7 support today at Next 2018. – Jared Jul 24 '18 at 20:28
8

As of 19th July 2018, Google Cloud Functions supports Python 3.7.

Kindly check the Runtime environment to find the Python 3.7 runtime and sample script (based on Flask) .

--UPDATED--

Official Documentation for the Google Cloud Functions - Python 3.7 support Beta Release.

This is a beta release of the Python runtime for Google Cloud Functions. This feature might be changed in backward-incompatible ways and is not subject to any SLA or deprecation policy.

SkLearn, Numpy is supported in Google Cloud function. Also I've run a sample test to confirm the availability of Pandas as well and its working fine.

https://github.com/mkanchwala/google-functions-python-example

Hope this helps to all the "Py" lovers.

Murtaza Kanchwala
  • 2,425
  • 25
  • 33
1

You can use AWS lambda as well if you want to work around and still use Python as your main language. Some modules/packages will need to be imported via zip file with AWS Lambda but it has a broader range of usable languages than GCF

Loyal_Burrito
  • 125
  • 2
  • 14