pythonpython-2.7dockerfirebase-admin

ImportError: No module named google.auth


When I try to import firebase_admin in python 2.7 I get the error:

ImportError: No module named google.auth

This is the DockerFile I'm using.

I've installed Python from the source code using

wget https://www.python.org/ftp/python/2.7/Python-2.7.tgz
tar xvzf Python-2.7.tgz
cd Python-2.7
./configure
make
make install

Then I've installed pip and firebase admin by running:

apt-get install -y python-pip
pip install firebase-admin

Then I ran import firebase_admin inside the python shell. I got the error:

ImportError: No module named google.auth

I've run pip show google.auth and got the following output:

Name: google-auth
Version: 1.6.3
Summary: Google Authentication Library
Home-page: https://github.com/GoogleCloudPlatform/google-auth- 
library-python
Author: Google Cloud Platform
Author-email: jonwayne+google-auth@google.com
License: Apache 2.0
Location: /usr/local/lib/python2.7/dist-packages
Requires: cachetools, six, pyasn1-modules, rsa

I've run echo $PYTHONPATH and got this:

/usr/local/lib/python2.7/dist-packages:/usr/lib/python2.7/dist-packages

That means the google.auth is installed and its directory is in the PYTHONPATH, why python can't find it? and how to fix it?


Solution

  • I looked into the image of adamantium/flutter and saw in the Dockerfile that it depends on ubuntu:18.04 which is shipped with Python2 directly, as mentioned in PEP-394 (see the link below for more information on this).

    https://www.python.org/dev/peps/pep-0394/

    So, I don't understand why you want to re-install it again. What happened is that you have used a Dockerfile that installs another version of Python2 in /usr/local/bin/ and overwrites the symbolic link that points to the original Python2 as you can see in docker build logs

    if test -f /usr/local/bin/python -o -h /usr/local/bin/python; \
    then rm -f /usr/local/bin/python; \
    else true; \
    fi
    (cd /usr/local/bin; ln python2.7 python)
    
    

    You can then verify the current Python interpreter within the container:

    root@9b9176e6c26c:/# which python
    /usr/local/bin/python
    
    root@9b9176e6c26c:/# python --version
    Python 2.7
    

    Meanwhile, I have removed the part which installs Python2 from the Dockerfile and got this.

    root@e6dd827dac1d:/# which python
    /usr/bin/python
    
    root@e6dd827dac1d:/# python --version
    Python 2.7.15rc1
    

    Then import what you want directly:

    root@e6dd827dac1d:/# python -c "import firebase_admin"
    
    root@e6dd827dac1d:/# echo $?
    0
    

    You can see that it succeeded by returning code 0.

    Dockerfile after modification:

    FROM adamantium/flutter
    
    RUN apt-get update && \
        apt-get install -y wget && \
        apt-get install -y build-essential && \
        apt-get install -y zlib1g && \
        apt-get install zlib1g-dev && \
        wget https://www.python.org/ftp/python/3.6.7/Python-3.6.7.tgz && \
        tar xvzf Python-3.6.7.tgz && \
        cd Python-3.6.7 && \
        ./configure && make && \
        make install
    
    RUN apt-get install -y python-pip && \
        pip install firebase-admin