pythonfunctiongoogle-cloud-platform

Firebase functions gen2 python init does not work


I have only one python installed in my system: 3.10.10. it includes the latest pip: 23.1.2 and I installed the latest module of firebase_functions

After I try to init firebase functions in my machine I follow the instructions and when it asks me to install dependencies I get this error:

ERROR: To modify pip, please run the following command:
C:\Users\XXX\functions\venv\Scripts\python.exe -m pip install --upgrade pip
Error: An unexpected error has occurred.

Next time I run the same process but this time I did not accept to install dependencies and it worked:

 Firebase initialization complete!

Now this is the default code google provided:

# Welcome to Cloud Functions for Firebase for Python!
# To get started, simply uncomment the below code or create your own.
# Deploy with `firebase deploy`

from firebase_functions import https_fn
from firebase_admin import initialize_app

initialize_app()


@https_fn.on_request()
def on_request_example(req: https_fn.Request) -> https_fn.Response:
    return https_fn.Response("Hello world!")

I have all dependencies installed. I made sure thousand times. When I run

firebase deploy

I get this error:

i  deploying functions
i  functions: preparing codebase default for deployment
i  functions: ensuring required API cloudfunctions.googleapis.com is enabled...
i  functions: ensuring required API cloudbuild.googleapis.com is enabled...
i  artifactregistry: ensuring required API artifactregistry.googleapis.com is enabled...
+  functions: required API cloudbuild.googleapis.com is enabled
+  artifactregistry: required API artifactregistry.googleapis.com is enabled
+  functions: required API cloudfunctions.googleapis.com is enabled

 Error: An unexpected error has occurred.

And this is the log in the firebase-debug.log

[debug] [2023-06-11T13:05:29.172Z] stderr: ModuleNotFoundError: No module named 'firebase_functions'

[debug] [2023-06-11T13:05:29.182Z] Error: spawn "C:\Users\XXX\functions\venv\Scripts\activate.bat" ENOENT
at notFoundError (C:\Users\XXX\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\cross-spawn\lib\enoent.js:6:26)
at verifyENOENT (C:\Users\XXX\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\cross-spawn\lib\enoent.js:40:16)
at cp.emit (C:\Users\XXX\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\cross-spawn\lib\enoent.js:27:25)
at ChildProcess._handle.onexit (node:internal/child_process:291:12)
[error] Error: An unexpected error has occurred.

Solution

  • Apparently firebase creates its own python dependency separately from your own python version in your machine. It is stored in the venv folder. To make it work follow the following steps:

    firebase init
    

    Choose functions: Functions: Configure a Cloud Functions directory and its files

    When it asks: Do you want to install dependencies now? (Y/n)

    Choose No

    Open cmd within the functions project and then:

    cd functions\venv\Scripts
    python.exe -m pip install --upgrade pip
    python.exe -m pip install firebase_functions
    cd ../../../
    

    And now:

    firebase init functions
    

    Choose Overwrite, and then:

    File functions/requirements.txt already exists. Overwrite? No
    File functions/.gitignore already exists. Overwrite? No
    File functions/main.py already exists. Overwrite? No
    Do you want to install dependencies now? Yes
    

    And now:

    firebase deploy --only functions
    

    And it should work perfectly