'gmail API : ModuleNotFoundError: No module named 'google'
While running the quickstart.py of Gmail, I get the following error on macOS.
Traceback (most recent call last):
File "quickstart.py", line 4, in <module>
from googleapiclient.discovery import build
File "/Users/<user>/Documents/venv/bin/googleapiclient/discovery.py", line 49, in <module>
import google.api_core.client_options
ModuleNotFoundError: No module named 'google'
These are the installed Google libraries.
$ pip freeze | grep google
DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. A future version of pip will drop support for Python 2.7. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
google-api-core==1.16.0
google-api-python-client==1.8.0
google-auth==1.12.0
google-auth-httplib2==0.0.3
google-auth-oauthlib==0.4.1
google-cloud-bigquery==1.24.0
google-cloud-core==1.3.0
google-resumable-media==0.5.0
googleapis-common-protos==1.51.0
Solution 1:[1]
From the error, the google module is not installed for Python3. I'm gonna take a wild guess and say it's because your version of Python(Python2.7) has depreciated, clearly, try using python3.7(alongside pip3) hopefully it'll work.
Solution 2:[2]
I think you should run
pip install google-cloud
If the issue persist then you have some issues with you python paths.
You should check where the google package actually is installed:
python3 -c 'import google; print(google.__file__)'
Also you can try to install your packages in a virtualenv:
virtualenv venv
source venv/bin/activate
venv/bin/pip install your-packages
venv/bin/python -c 'import google'
deactivate
rm -fr venv/
Solution 3:[3]
The library google-api-core 1.16.0 is no longer supported in Python 2.7. Only versions above 3.5 are supported, as specified here:
Supported Python Versions: Python >= 3.5
Deprecated Python Versions: Python == 2.7. Python 2.7 support will be removed on January 1, 2020.
You should upgrade to a supported Python version.
Reference:
Solution 4:[4]
I suspect that you are using python3 rather than python2.7. Using pip is pointing to your python2.7 installation. You can use pip for python3 by slightly editing the command you are running in your shell:
pip3 install --upgrade google-api-python-client
Even more secure could be this (do note that we use pip, not pip3 in this command):
python3 -m pip install --upgrade google-api-python-client
This command will point to the python3 interpreter you are currently using.
Also make sure your venv is activated when using the above commands if you want to work in your virtual environment.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Johan Khanye |
| Solution 2 | marian.vladoi |
| Solution 3 | Community |
| Solution 4 | lcdumort |
