Why can't I import the Google Cloud client lib

2019-08-02 18:00发布

问题:

I'm working in Python 2.7.10.

i'm just trying to run from google.cloud import storage, but I get ImportError: No module named google.cloud.

google-cloud appears to be installed correctly. pip install google-cloud returns Requirement already satisfied: google-cloud in /Library/Python/2.7/site-packages (0.34.0), and pip show google-cloud returns:

Name: google-cloud
Version: 0.34.0
Summary: API Client library for Google Cloud
Home-page: https://github.com/GoogleCloudPlatform/google-cloud-python
Author: Google Cloud Platform
Author-email: googleapis-publisher@google.com
License: Apache 2.0
Location: /Library/Python/2.7/site-packages
Requires: 
Required-by: 

EDIT: thanks to Duy below for reminding me about the next step. Running pip install google-cloud-storage throws a bunch of errors:

Using cached https://files.pythonhosted.org/packages/7b/7c/c9386b82a25115cccf1903441bba3cbadcfae7b678a20167347fa8ded34c/pyasn1-0.4.5-py2.py3-none-any.whl
protobuf 3.6.1 has requirement six>=1.9, but you'll have six 1.4.1 which is incompatible.
google-api-core 1.7.0 has requirement six>=1.10.0, but you'll have six 1.4.1 which is incompatible.
google-auth 1.6.2 has requirement six>=1.9.0, but you'll have six 1.4.1 which is incompatible.
Installing collected packages: setuptools, protobuf, googleapis-common-protos, urllib3, certifi, chardet, idna, requests, futures, pyasn1, rsa, pyasn1-modules, cachetools, google-auth, google-api-core, google-cloud-core, google-resumable-media, google-cloud-storage
  Found existing installation: setuptools 18.5
    Uninstalling setuptools-18.5:
Could not install packages due to an EnvironmentError: [('/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/_markerlib/markers.pyc', '/private/var/folders/_h/5cqnls4d2jxckyjk70267q_c0000gp/T/pip-uninstall-ZBmwkH/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/_markerlib/markers.pyc', "[Errno 1] Operation not permitted: '/private/var/folders/_h/5cqnls4d2jxckyjk70267q_c0000gp/T/pip-uninstall-ZBmwkH/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/_markerlib/markers.pyc'"), ('/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/_markerlib/__init__.py', '/private/var/folders/_h/5cqnls4d2jxckyjk70267q_c0000gp/T/pip-uninstall-ZBmwkH/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/_markerlib/__init__.py', "[Errno 1] Operation not permitted: '/private/var/folders/_h/5cqnls4d2jxckyjk70267q_c0000gp/T/pip-uninstall-ZBmwkH/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/_markerlib/__init__.py'"), ('/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/_markerlib/markers.py', '/private/var/folders/_h/5cqnls4d2jxckyjk70267q_c0000gp/T/pip-uninstall-ZBmwkH/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/_markerlib/markers.py', "[Errno 1] Operation not permitted: '/private/var/folders/_h/5cqnls4d2jxckyjk70267q_c0000gp/T/pip-uninstall-ZBmwkH/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/_markerlib/markers.py'"), ('/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/_markerlib/__init__.pyc', '/private/var/folders/_h/5cqnls4d2jxckyjk70267q_c0000gp/T/pip-uninstall-ZBmwkH/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/_markerlib/__init__.pyc', "[Errno 1] Operation not permitted: '/private/var/folders/_h/5cqnls4d2jxckyjk70267q_c0000gp/T/pip-uninstall-ZBmwkH/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/_markerlib/__init__.pyc'"), ('/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/_markerlib', '/private/var/folders/_h/5cqnls4d2jxckyjk70267q_c0000gp/T/pip-uninstall-ZBmwkH/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/_markerlib', "[Errno 1] Operation not permitted: '/private/var/folders/_h/5cqnls4d2jxckyjk70267q_c0000gp/T/pip-uninstall-ZBmwkH/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/_markerlib'")]

I don't understand what the environment error is, and all my searches run down unhelpful rabbit holes.

回答1:

You must use a python environment.

pip install virtualenv
virtualenv <your-env>
source <your-env>/bin/activate
<your-env>/bin/pip install google-cloud-python

Check here for more details about virtualenv https://cloud.google.com/python/setup



回答2:

The google-cloud package is deprecated, you should uninstall it and install google-cloud-storage instead.



回答3:

You forget to install pip install google-cloud-storage