如何防止“导入错误:没有名为oauth2client.client的模块";在Google App Engine上?

如何防止“导入错误:没有名为oauth2client.client的模块

问题描述:

我们收到一个错误:

ImportError:没有名为OAuth2Client的模块

ImportError: No module named OAuth2Client

我们注意到有关该主题的数十个问题,许多问题尚未得到解答,至少有一个答案描述了从Google App Engine SDK复制文件的解决方案.

We have noticed scores of questions around this topic, many unanswered and at least one answer that describes the solution of copying over files from the Google App Engine SDK.

但是,这种方法似乎很乏味,因为所有依赖项还不清楚.如果我们复制oauth2client然后运行,则下一个错误是缺少另一个模块.修复此问题,然后缺少另一个模块,等等,等等.

This approach, however, seems tedious because all the dependencies are unclear. If we copy over oauth2client then run, the next error is another module that is missing. Fix that, then another module is missing, etc., etc.

具有讽刺意味的是,我们可以在PyCharm中看到从Google App Engine SDK列出的所有所需文件和模块,但是脚本似乎无法访问它们.

What is ironic is that we can see all the files and modules needed, listed from Google App Engine SDK right in PyCharm but they seem inaccessible to the script.

有没有更好的方法来提取oauth2client Python在App Engine上工作所需的所有文件?

Is there no better way to pull in all the files that oauth2client needs for Python to work on App Engine?

答案是文件中的供应商".

The answer is to "vendor" in the file(s).

我们根据此文档 https找到了一种解决此问题的快捷方法://cloud.google.com/appengine/docs/python/tools/libraries27#vendoring SO答案.

We found a quick way to solve this based on this documentation https://cloud.google.com/appengine/docs/python/tools/libraries27#vendoring and this SO answer.

  1. 在与app.yaml文件相同的文件夹中创建一个名为" lib "的新文件夹. (您可以命名其他名称.只需在下面使用该名称即可.)

  1. Create a new folder called "lib" in the same folder as your app.yaml file. (You can name it something else. Just use that name below.)

在与app.yaml文件相同的文件夹中创建一个名为 appengine_config.py 的空文件

Create an empty file called appengine_config.py in the same folder as your app.yaml file

在该appengine_config.py文件中添加两行:

Add two lines to that appengine_config.py file:

from google.appengine.ext import vendor vendor.add('lib')

from google.appengine.ext import vendor vendor.add('lib')

在终端上,导航到包含该文件的目录,然后执行以下命令:

From terminal, navigate to the directory which contains that file and execute the following command:

sudo pip install -t lib google-api-python-client

导入错误将消失,您还将拥有所有从属模块.

The import error will disappear and you will have all the sub-dependent modules as well.