Intent
我正在为 App Engine 创建一个 CRUD 应用程序,其中用户通过 OAuth 进行身份验证,服务器将其访问和刷新令牌存储在数据存储区中。我正在进行本地测试dev_appserver
并陷入进退两难的境地:
-
The python37 runtime cannot import the ndb
module for Datastore and the python27 runtime cannot import the google.auth
module.
问题陈述
Python37
In the python37 runtime, my requirements.txt file is detected and all dependencies are installed properly. However, it appears that this runtime does not currently have native support for the ndb
module in order to make calls the Datastore API.
from google.appengine.ext import ndb
ModuleNotFoundError: No module named 'google.appengine'
One way to solve my problem would be to manually install the ndb package, however I don't believe that it open-source at this point in time.
我很可能会使用google-api-python-client
与数据存储交互。不过,我仍然有兴趣解决下面概述的问题,以便更好地理解 python 模块在 App Engine 中的工作方式。
Python27
在 python27 运行时中,requirements.txt 被忽略。我的解决方法是包含所有依赖项源代码的最新版本。我通过填充一个名为的目录来做到这一点dependencies
与源,然后添加符号链接到根模块。 (这也是学习如何导入 python 模块的一个很好的练习)
项目结构
目录
.
├── api.py
├── app
│ ├── build
│ ├── config
│ ├── dist
│ ├── index.html
│ ├── node_modules
│ ├── package.json
│ ├── package-lock.json
│ ├── src
│ └── static
├── app.yaml
├── dependencies
│ ├── click-7.0
│ ├── flask-1.0.2
│ ├── google-auth-library-python-1.6.3
│ ├── google-auth-library-python-oauthlib-0.3.0
│ ├── itsdangerous-1.1.0
│ ├── jinja-2.10.1
│ └── markupsafe-1.1.1
├── libs
│ ├── click -> ../dependencies/click-7.0/click/
│ ├── flask -> ../dependencies/flask-1.0.2/flask/
│ ├── google -> ../dependencies/google-auth-library-python-1.6.3/google/
│ ├── google_auth_oauthlib -> ../dependencies/google-auth-library-python-oauthlib-0.3.0/google_auth_oauthlib/
│ ├── __init__.py
│ ├── itsdangerous -> ../dependencies/itsdangerous-1.1.0/src/itsdangerous/
│ ├── jinja2 -> ../dependencies/jinja-2.10.1/jinja2/
│ └── markupsafe -> ../dependencies/markupsafe-1.1.1/src/markupsafe/
├── requirements.txt
└── spa.py
Python代码
在我的服务器文件中,spa.py
and api.py
,我包括libs
在python路径中如下:
import sys
import os
sys.path.append(os.path.join(os.path.dirname(__file__), 'libs'))
这适用于导入语句,例如
import google_auth_oauthlib.flow # works
and
from flask import Flask, request, session, redirect # works
但它失败了
import google.auth # does not work
有这个特例
Traceback (most recent call last):
File "<HOME>/google-cloud-sdk/platform/google_appengine/google/appengine/runtime/wsgi.py", line 240, in Handle
handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
File "<HOME>/google-cloud-sdk/platform/google_appengine/google/appengine/runtime/wsgi.py", line 299, in _LoadHandler
handler, path, err = LoadObject(self._handler)
File "<HOME>/google-cloud-sdk/platform/google_appengine/google/appengine/runtime/wsgi.py", line 85, in LoadObject
obj = __import__(path[0])
File "<HOME>/vue-scheduler/api.py", line 6, in <module>
import google.auth
File "<HOME>/google-cloud-sdk/platform/google_appengine/google/appengine/tools/devappserver2/python/runtime/sandbox.py", line 1149, in load_module
raise ImportError('No module named %s' % fullname)
ImportError: No module named google.auth
我相信我已经正确设置了 python 导入的所有内容auth
子模块。这__init__.py
文件存在并且should让 python 递归遍历嵌套的模块目录。然而它无法找到auth
.