31

I'm trying out the Python3.7 runtime on Google Cloud Functions. I am able to deploy the functions and make them work once deployed, however, I can't seem to run the emulator to test them locally before I deploy.

Google's documentation is a little inconsistent where they tell you to install the google functions emulator here: https://cloud.google.com/functions/docs/emulator

But over on Firebase they tell you to npm install firebase-admin, firebase-tools and firebase-functions.

All of the emulator documentation references examples written in JS, none in Python so I'm wondering if these emulator even run Python functions locally?

Thanks

1
  • I never use emulators. Running Cloud Functions code at your desktop is extremely easy. Emulating the entry point just requires Json data. This allows you full access to use your debuggers, editors, and plain old print statements, etc. Dec 9, 2018 at 18:15

4 Answers 4

47

You can use the Functions Framework for Python to run the function locally.

Given a function in a file named main.py like so:

def my_function(request):
    return 'Hello World'

You can do:

$ pip install functions-framework
$ functions-framework --target my_function

Which will start a local development server at http://localhost:8080.

To invoke it locally for an HTTP function:

$ curl http://localhost:8080

For a background function with non-binary data:

$ curl -d '{"data": {"hi": "there"}}' -X POST \
-H "Content-Type: application/json" \
http://localhost:8080

For a background function with binary data:

$ curl -d "@binary_file.file" -X POST \
-H "Ce-Type: true" \
-H "Ce-Specversion: true" \
-H "Ce-Source: true" \
-H "Ce-Id: true" \
-H "Content-Type: application/json" \
http://localhost:8080
16
  • 1
    it doesn't work if you try to emulate the path behaivours Aug 7, 2019 at 11:03
  • @DavidMabodo can you go into more detail? What do you mean by "emulate the path behaviors"? Aug 7, 2019 at 21:29
  • 2
    a pub/sub message? can i emulate that?
    – Hsn
    Apr 16, 2020 at 14:32
  • 1
    @Hsn I added some examples to my answer. Apr 16, 2020 at 18:05
  • 2
    @Hsn Try the "For a background function with non-binary data" example above. The value of data that you provide as the JSON request body eventually becomes the event argument, the context argument is created by the framework. Apr 20, 2020 at 20:47
12

Update

Please, use the official emulator and serving framework from GCP https://github.com/GoogleCloudPlatform/functions-framework-python

You can install it with

pip install functions-framework

Deprecated

Based on Dustin's answer I've developed a package to serve as emulator:

pip install gcp-functions-emulator

Given you want to serve the following function

# mycloudfunction.py
def api(request):
  return 'important data'

To emulate we have to call it like so:

gcpfemu <path/to/file.py> <function_name>

For example, with the code above we will call it:

gcpfemu mycloudfunction.py api

And to access the data we can use for example curl:

curl localhost:5000/api
> important data
4
  • 2
    Could you provide the Github project link? This is a kind of project I'd like to contribute in.
    – Sayan Sil
    Apr 27, 2019 at 21:50
  • @SayanSil gitlab.com/divisadero/cloud-functions-python-emulator
    – Suraj
    Nov 1, 2021 at 16:53
  • 1
    That's right @SurajSubramanian. However I recommend to use the official Google Functions Emulator instead of this one. Divisadero no longer maintains this pre-google-emulator one
    – dinigo
    Nov 2, 2021 at 9:31
  • Sure @dinigo, I'll try using the official emulator. Thanks ^^
    – Suraj
    Nov 2, 2021 at 10:05
0

See this project on GitHub: GoogleCloudPlatform/functions-framework

Currently there are only implementations in Node.js, Go, and PHP, but see Issue #5 about the Python implementation.

I suggest, whatever implementation you use, to follow the Functions Framework Contract

UPDATE: As Dustin mentioned, there is also a Python implementation now.

1
-1

To run it in IntelliJ with Target Type = Script Path and the default options it should look like this:

from flask import Flask, request
app = Flask(__name__)


@app.route('/')
def hello():
    return hello_get(request)

if __name__ == '__main__':
    app.run('127.0.0.1', debug=True)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.