-1

--- Question closed

It was my mistake, my uWSGI startup script switches to a different virtualenv.

--- Original question

I'm trying to publish push notifications from my Flask app server to Android APP.

Server environment (dev):

  • Mac OS Sierra
  • Flask 1.0.2
  • Python 3.6.5
  • firebase-admin SDK 2.17.0 (the latest version)

When running code as Flask application for initialising the SDK, import statement throws error:

import firebase_admin

ModuleNotFoundError: No module named 'firebase_admin'

However, running above import statement from Python interpreter mode gives no error. PyCharm is also able to recognize the firebase_admin module. All three setups (Flask app, interpreter, PyCharm) use the same virtualenv.

I had tried few older versions of SDK but error persists. Any clue?

--- More details

The module was installed from command line within virtualenv using command pip install firebase-admin.

The error occurs when running the code as Flask+uWSGI app from command line, within the same virtualenv. pip freeze shows that the module is indeed present.

--- Question closed

It was my mistake, my uWSGI startup script switches to a different virtualenv.

shiouming
  • 1,889
  • 4
  • 17
  • 26

2 Answers2

2

How did you install/add the firebase_admin package to your project?

If you haven't explicitly installed/added the package, I've only come across a few cases (there may be other cases/solutions as well), that gives off the error ModuleNotFoundError: No module named 'firebase_admin':

PyCharm

If using PyCharm, you can install/add it from the PyCharm preferences

  • File > Preferences > Project > Project Interpreter
  • Click the add button, search for firebase, select the appropriate package: firebase-admin
  • Click "Install Package" button.

Visual Studio Code / Alternative installation

I use Visual Studio Code, and opted to install firebase_admin libraries into the virtualenv using a requirements.txt file saved in the root directory. You can still use this method even if you're using PyCharm:

1) Activate your environment

2) Create a requirements.txt file with the following contents:

firebase_admin
// other packages

3) Run pip install

pip install -r requirements.txt

4) After installing, you can confirm that's already available by checking the site-packages folder in the following directory:

env/lib/site-packages/firebase_admin
env/lib/site-packages/firebase_admin-<version>.dist-info

5) Try running the code again.

Hope that helps!

johnsing
  • 141
  • 5
  • All dependencies including firebase_admin were installed from command line within virtualenv by using pip. In this case: pip install firebase-admin. Even though firebase-admin wasn't installed from PyCharm, in my case PyCharm has no issue to recognise this module. My problem occurs when running the code as Flask app, more specifically running it as Flask+uWSGI from command line. – shiouming Jun 11 '19 at 09:49
  • Thanks johnsing. I'd identified the root cause. My uWSGI startup script switched to a different virtualenv. – shiouming Jun 11 '19 at 10:36
  • Thanks for adding more info. In that case, have you tried comparing the (1) package path of the program executing your code and (2) the project interpreter and see if they're the same? You can check [this answer](https://stackoverflow.com/a/48617071/11630054) from [bernie-yum](https://stackoverflow.com/users/6399638/bernie-yum) – johnsing Jun 11 '19 at 10:40
  • Not really. The possibility of wrong virtualenv was completely off my mind, since I ran the app from the right virtualenv, and forgot that it could be auto-switched through uWSGI config. – shiouming Jun 11 '19 at 15:39
-1

Run this command pip install firebase-admin flask