问题
The following problem has been encountered by me and others in a variety of different GCP projects, and under circumstances where the same code worked a few days earlier.
I am walking through all of the steps again right now, just to make sure that I still see the same thing. I started by deleting the running instance of Cloud Datalab in my GCP project, and then I started over: first deploy Datalab, then "start using" Datalab.
I then select a pre-existing notebook that I have which starts with the following lines:
!pip install --upgrade google-api-python-client
from httplib2 import Http
from oauth2client.client import GoogleCredentials
credentials = GoogleCredentials.get_application_default()
http = Http()
credentials.authorize(http)
This has worked in the past for me and for others. Now I get this ImportError:
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-2-deffc94100d1> in <module>()
1 from httplib2 import Http
2 from oauth2client.client import GoogleCredentials
----> 3 credentials = GoogleCredentials.get_application_default()
4 http = Http()
5 credentials.authorize(http)
/usr/local/lib/python2.7/dist-packages/oauth2client/client.pyc in get_application_default()
1202 GoogleCredentials.get_application_default().
1203
-> 1204 Args:
1205 access_token: string, access token.
1206 client_id: string, client identifier.
/usr/local/lib/python2.7/dist-packages/oauth2client/client.pyc in _get_implicit_credentials(cls)
1187 """
1188
-> 1189 NON_SERIALIZED_MEMBERS = (
1190 frozenset(['_private_key']) |
1191 OAuth2Credentials.NON_SERIALIZED_MEMBERS)
/usr/local/lib/python2.7/dist-packages/oauth2client/client.pyc in _implicit_credentials_from_gce()
1123
1124 def _in_gae_environment():
-> 1125 """Detects if the code is running in the App Engine environment.
1126
1127 Returns:
/usr/local/lib/python2.7/dist-packages/oauth2client/client.pyc in _get_application_default_credential_GCE()
1378 """Get the Application Default Credentials for the current environment.
1379
-> 1380 Raises:
1381 ApplicationDefaultCredentialsError: raised when the credentials
1382 fail to be retrieved.
ImportError: No module named gce
Has something changed? has a module that was previously named "gce" been renamed? do I need to create credentials? (The project has both a default App Engine service account and a default Compute Engine service account, and has all of the APIs that I think might need to be enabled already enabled.)
20-apr-2016 UPDATE: After not trying to repeat this for about a month, I was looking at this again yesterday. The first time I tried to re-run the same notebook I was working with last month, I needed to take out the fix I had added in as a result of this issue in order to get it to work. But today that same code would again not work. In the mean-time I noticed that the recommendations had been updated, so I am now going to walk through the recommended approaches.
!pip show google-api-python-client
---
Name: google-api-python-client
Version: 1.5.0
Location: /usr/local/lib/python2.7/dist-packages
Requires: httplib2, uritemplate, six, oauth2client
Looking here the specific versions required seem to be: httplib2>=0.8,<1; any oauth2client; six>=1.6.1,<2; and uritemplate>=0.6,<1. (The change to allow earlier oauth2client versions seems to have been committed on Mar 14, but Pypi shows the most recent version as 1.5.0 uploaded on Feb 20, which agrees with the Releases page which says that 1.5.0 was released on Feb 19, which may have been when this problem originally appeared although I didn't notice it for a few weeks.)
I have not been able to get much further in trying out the 3 options described by Anthonios because I repeatedly get "Connection failed" or other errors when trying to use Datalab. I will try to update this issue again soon.
回答1:
You may install additional python libraries in Datalab, but you must make sure that the installation does not break the working Datalab environment. The best way to ensure this is by checking to see whether the dependencies required by the additional python library are currently installed in Datalab. Follow the steps below:
Step 1: Confirm which dependencies are required for the library that you want to install
Use !pip show <python library>
For example, use the following to view dependencies required for google-api-python-client
:
>>> !pip show google-api-python-client
---
Name: google-api-python-client
Version: 1.5.0
Location: /usr/local/lib/python2.7/dist-packages
Requires: httplib2, uritemplate, six, oauth2client
The specific versions required can be found here.
Step 2: Determine whether any of the dependencies are already installed in Datalab
In Datalab, run the following code and check for the dependencies in step 1
>>> import pip
>>> for dist in pip.get_installed_distributions():
>>> print dist
GCPData 0.1.0
GCPDataLab 0.1.0
wheel 0.26.0
tensorflow 0.6.0
protobuf 3.0.0a3
nbformat 4.0.1
ipykernel 4.2.2
jsonschema 2.5.1
nose 1.3.7
singledispatch 3.4.0.3
pyparsing 2.0.7
pandas 0.17.1
futures 3.0.3
pyasn1-modules 0.0.8
mock 1.3.0
MarkupSafe 0.23
cycler 0.9.0
ipython 4.0.3
terminado 0.6
path.py 8.1.2
certifi 2015.11.20.1
Pygments 2.1
funcsigs 0.4
backports-abc 0.4
jupyter-client 4.1.1
pexpect 4.0.1
backports.ssl-match-hostname 3.5.0.1
statsmodels 0.6.1
seaborn 0.6.0
scikit-learn 0.16.1
rsa 3.3
jupyter-core 4.0.6
brewer2mpl 1.4.1
py-dateutil 2.2
patsy 0.4.1
ptyprocess 0.5
PyYAML 3.11
Jinja2 2.8
decorator 4.0.6
pandocfilters 1.2.4
pickleshare 0.6
sympy 0.7.6
pytz 2015.7
httplib2 0.9.2
functools32 3.2.3-2
notebook 4.0.2
tornado 4.3
simplegeneric 0.8.1
numpy 1.10.4
matplotlib 1.5.1
scipy 0.17.0
pyasn1 0.1.9
pbr 1.8.1
python-dateutil 2.4.2
traitlets 4.1.0
oauth2client 1.4.12
ipython-genutils 0.1.0
nbconvert 4.1.0
ggplot 0.6.5
mistune 0.7.1
Based on the output in this example, one conflict is that Datalab currently has oauth2client
version 1.4.12
installed, and the google-api-python-client
library requires a version of oauth2client
that is greater than or equal to 2.0.0
.
We have 3 options :
- Wait until Datalab upgrades support for
oauth2client
to>=2.0.0
- Install a prior version of
google-api-python-client
that supports the existing installation ofoauth2client
1.4.12. For example, google-api-python-client 1.4.2 Try installing the latest
google-api-python-client
while maintaining the existingoauth2client
installation. This can be achieved by installinggoogle-api-python-client
without dependencies, then manually installing any missing dependencies that are not already installed in Datalab. (There is a chance that the library may not work properly, but we are less likely to break the working Datalab environment). For example:!pip install google-api-python-client==1.5.0 --no-deps
!pip install uritemplate
来源:https://stackoverflow.com/questions/36142739/authentication-error-in-cloud-datalab-importerror-no-module-named-gce