witi
witi

Reputation: 171

Firebase Cloud Functions Python - Cannot Add Dependencies

I'm using python cloud functions in my firebase project. After initializing cloud functions, adding firebase-admin to the requirements.txt file worked, and I could test with firebase emulators:start and also successfully deploy with firebase deploy --only functions.

The issue is when I try to add other packages. I added tldextract to requirements.txt and put import tldextract in main.py which causes

ModuleNotFoundError: No module named 'tldextract'

127.0.0.1 - - [14/Jun/2023 00:24:10] "GET /__/functions.yaml HTTP/1.1" 500 -

⬢  functions: Failed to load function definition from source: FirebaseError: Failed to parse build specification

when I run firebase emulators:start or firebase deploy --only functions. It also seems like the venv folder is not being updated.

I tried activating the venv and pip install -r requirements.txt which made the local execution work with firebase emulators:start, BUT after redeploying the functions, they're stilling failing in the cloud.

I tried this with different packages to make sure it's not just this one specific package. But adding other pip packages to requirements.txt and importing them in main.py failed for all packages that I tested.

What am I doing wrong?

Upvotes: 4

Views: 4296

Answers (3)

Luca Köster
Luca Köster

Reputation: 360

Okay the problem is, that the Python cloud Functions is in an virtual enviroment. But if you just run

pip install -r requirements.txt

it will install just on your system and NOT in the virtual enviroment.

So the right way to do it is:

1: check if virtualenv is installed on your system if not:

pip install virtualenv --user

2: Now you have to activate the virtuel enviroment. Therefore go in the right directory and run following command. For me:

cd ./functions

Windows:

.\venv\Scripts\activate 

Linux and Mac:

source venv/bin/activate

You should see the change inside the terminal.

3: NOW you can run pip and it works

pip install -r requirements.txt

4: If you want to deactivate the venv just run

deactivate 

in the console.

5: Restart the emulator

This should be the right way! Hope i could help :D

Upvotes: 3

Sarah
Sarah

Reputation: 11

Another workaround:

Add your dependencies to the requirements.txt and run firebase init functions again. This will install all dependencies. It worked for me with local execution.

Upvotes: 1

witi
witi

Reputation: 171

The following solved the issue for me:

Delete the venv folder created by firebase init functions.

Create a new one as follows:

python3.11 -m venv venv
source venv/bin/activate
pip3 install --upgrade pip
python3.11 -m pip install -r requirements.txt

Now deploy with firebase deploy --only functions

Upvotes: 8

Related Questions