如何解决“ImportError: No module named google.auth”?

11

我正在本地通过 dev_appserver 运行标准的应用引擎环境,但无法消除以下错误:

ImportError:没有名为 google.auth 的模块

完整的回溯信息(使用 ... 替换了个人详细信息):

Traceback (most recent call last):
  File "/Users/.../google-cloud-sdk/platform/google_appengine/google/appengine/runtime/wsgi.py", line 240, in Handle
    handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
  File "/Users/.../google-cloud-sdk/platform/google_appengine/google/appengine/runtime/wsgi.py", line 299, in _LoadHandler
    handler, path, err = LoadObject(self._handler)
  File "/Users/.../google-cloud-sdk/platform/google_appengine/google/appengine/runtime/wsgi.py", line 85, in LoadObject
    obj = __import__(path[0])
  File "/Users/.../.../main.py", line 6, in <module>
    from services.get_campaigns import get_campaigns
  File "/Users/.../.../get_campaigns.py", line 3, in <module>
    from googleads import adwords
  File "/Users/.../.../lib/googleads/__init__.py", line 17, in <module>
    from ad_manager import AdManagerClient
  File "/Users/.../lib/googleads/ad_manager.py", line 28, in <module>
    import googleads.common
  File "/Users/.../lib/googleads/common.py", line 51, in <module>
    import googleads.oauth2
  File "/Users/.../lib/googleads/oauth2.py", line 28, in <module>
    import google.auth.transport.requests
  File "/Users/.../google-cloud-sdk/platform/google_appengine/google/appengine/tools/devappserver2/python/runtime/sandbox.py", line 1154, in load_module
    raise ImportError('No module named %s' % fullname)
ImportError: No module named google.auth

我已经安装了google.auth,如pip show google.auth所示:

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: /Users/.../Library/Python/2.7/lib/python/site-packages
Requires: rsa, pyasn1-modules, cachetools, six
Required-by: googleads, google-auth-oauthlib, google-auth-httplib2, google-api-python-client

我已经升级了所有需要google.auth的模块:googleads、google-auth-oauthlib、google-auth-httplib2、google-api-python-client,但没有结果。

我不太确定下一步该采取什么措施来调试此问题。有人可以指点我正确的方向吗?


展示完整的错误回溯,而不仅仅是错误行。 - John Gordon
2
你确定在执行脚本时使用的是默认解释器吗?你可以使用 import sys; print(sys.executable) 命令来查看。如果输出结果与 which python 不同,那么这就是你的问题所在。 - Alassane Ndiaye
@JohnGordon 添加了完整的回溯信息。 - Stiño
@AlassaneNdiaye 我目前无法在我的项目中记录任何内容(它立即失败)。我会看看是否有其他方法来检查这个问题。 - Stiño
我之前也有同样的问题,不过我已经解决了。你可以在这里看到解决方法:https://stackoverflow.com/a/56884902/8244338 - Israel
2个回答

2
你的google.auth已安装在系统Python站点包中,而不是你的应用程序中:

位置:/Users/.../Library/Python/2.7/lib/python/site-packages

你需要在应用程序内安装应用程序的Python依赖项 - 请注意复制第三方库过程中的-t lib/ pip选项。
  1. Use pip (version 6 or later) with the -t <directory> flag to copy the libraries into the folder you created in the previous step. For example:

    pip install -t lib/ <library_name>
    

0
经过多次尝试和错误,我找到了这个bug:一个Python运行时版本的问题。
在我的app.yaml文件中,我已经指定了以下内容:
service: default
runtime: python27
api_version: 1
threadsafe: false

我把运行时改成了:

runtime: python37

感谢@AlassaneNdiaye在评论中指引我朝这个方向。

1
这将使您进入第二代标准环境,其中适用私有依赖项。但是,在部署到GAE时,您仍然需要在应用程序中安装依赖项,否则它将无法正常工作。 - Dan Cornilescu

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接