问题
I'm trying to run the sample provided here https://developers.google.com/analytics/devguides/reporting/core/v3/quickstart/service-py for authorization.
I've noticed from other questions in SO that (ImportError: cannot import name SignedJwtAssertionCredentials) SignedJwtAssertionCredentials has been removed and therefore could not be imported.
So, I started to follow the solutions provided both on the GitHub page (https://github.com/google/oauth2client/issues/401) and StackOverflow. So far, nothing worked, I'm still seeing the same error. Following is my code.
import argparse
from apiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials
import httplib2
from oauth2client import client
from oauth2client import file
from oauth2client import tools
And, this is the error I'm receiving on running the above code.
ImportError: cannot import name ServiceAccountCredentials
As I'm a complete newbie in this space, I tried to do this for both versions of OAuth (2.0.0 and 1.5.2). I also tried it after installing pyopenssl, but still failed.
回答1:
It seems oauth2client installation is unsuccessful. Try
pip install --upgrade google-api-python-client
回答2:
Installing pyopenssl fixed the issue for me:
pip install pyopenssl
Based on this answer.
回答3:
I had similar issues where I was getting cannot import name xxxx error. It turn out I had old *.pyc files in my environment from an older oauth2client version. Even though I updated to the lastest oauth2client version, the *.pyc files were getting used when I tried to run. I simply deleted the oauth2client *.pyc files and then reran my program without any issues.
Even if you upgrade to use the latest google-api-python-client... you'll want to make sure any *.pyc files from the old library have been removed.
回答4:
I was able to fix the issue in python3.
My python packages were a little bit messy and broken, because I was using python2 (being default one) and python3 and I was not using virtualenv. My os was Debian GNU/Linux 8 (jessie). I had exactcly the same issue:
ImportError: cannot import name ServiceAccountCredentials
Before I have fixed it, my packages were like this in python3:
Python 3.4.2 (default, Feb 7 2019, 06:08:06)
[GCC 4.9.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import oauth2client
>>> oauth2client.__version__
'1.5.2'
I had to uninstall every pip installation:
sudo pip uninstall pip
sudo pip3 uninstall pip
sudo python -m pip uninstall pip
sudo python3 -m pip uninstall pip
I had to install pip3 with easy_install:
sudo easy_install3 pip
I had also to uninstall oauth2client:
sudo pip3 uninstall oauth2client
It also turned out there was some files under ~/.local/lib/python3.4/site-packages/oauth2client/ in my home directory, so I executed the following command from my current user (not root) to delete the directory:
rm -rf ~/.local/lib/python3.4/site-packages/oauth2client*
I have installed oauth2client:
sudo pip3 uninstall oauth2client
After that the issue was resolved. Please note that while it was resolved on my local system, the other systems might need different solution (for example, python temporary files might be in a different path). However, the main approach is to have only 1 pip module, 1 oauth2client installation and be sure that there are no conflicting or temporary files that might affect current python3 environment.
Python 3.4.2 (default, Feb 7 2019, 06:08:06)
[GCC 4.9.2] on linux
>>> import oauth2client
>>> oauth2client.__version__
'4.1.2'
来源:https://stackoverflow.com/questions/35733897/import-error-google-analytics-api-authorization