【问题标题】:Cannot import six on GAE local development environment无法在 GAE 本地开发环境中导入六个
【发布时间】:2018-12-18 06:17:47
【问题描述】:

尝试运行我的 GAE Python 2.7 应用时出现以下错误。

ImportError: No module named six

我已关注this 页面并设置了我的app.yaml 文件,如下所示。

...
libraries:
  - name: six
    version: "1.9.0"
  - name: flask
    version:  "0.12"
  - name: jinja2
    version: "2.6"
  - name: pycrypto
    version: "2.6.1"
  - name: markupsafe
    version: "0.23"
  - name: werkzeug
    version: "0.11.10"
  - name: pytz
    version: "2017.3"

当我尝试导入 flask 包时,我第一次遇到此错误。所以我写了一些调试代码来调查这个问题,我发现我也不能直接导入six

我打印出sys.path 变量。

['C:\\Users\\user\\PycharmProjects\\gaeapp\\dist', 'C:\\Users\\user\\PycharmProjects\\gaeapp\\dist\\lib', 'C:\\Program Files (x86)\\Google\\google_appengine', 'C:\\Program Files (x86)\\Google\\google_appengine', 'C:\\Users\\user\\AppData\\Local\\conda\\conda\\envs\\py27-base\\DLLs', 'C:\\Users\\user\\AppData\\Local\\conda\\conda\\envs\\py27-base\\lib', 'C:\\Users\\user\\AppData\\Local\\conda\\conda\\envs\\py27-base', 'C:\\Program Files (x86)\\Google\\google_appengine\\lib\\flask-0.12', 'C:\\Program Files (x86)\\Google\\google_appengine\\lib\\jinja2-2.6', 'C:\\Program Files (x86)\\Google\\google_appengine\\lib\\pycrypto-2.6.1', 'C:\\Program Files (x86)\\Google\\google_appengine\\lib\\markupsafe-0.23', 'C:\\Program Files (x86)\\Google\\google_appengine\\lib\\werkzeug-0.11.10', 'C:\\Program Files (x86)\\Google\\google_appengine\\lib\\pytz-2017.3', 'C:\\Program Files (x86)\\Google\\google_appengine\\lib\\six-1.9.0', 'C:\\Program Files (x86)\\Google\\google_appengine\\lib\\click-6.6', 'C:\\Program Files (x86)\\Google\\google_appengine\\lib\\itsdangerous-0.24', 'C:\\Program Files (x86)\\Google\\google_appengine\\lib\\setuptools-0.6c11', 'C:\\Program Files (x86)\\Google\\google_appengine\\lib\\protorpc-1.0', 'C:\\Program Files (x86)\\Google\\google_appengine\\lib\\webapp2-2.3', 'C:\\Program Files (x86)\\Google\\google_appengine\\lib\\webob-1.1.1', 'C:\\Program Files (x86)\\Google\\google_appengine\\lib\\yaml-3.10']

我还检查了 C:\Program Files (x86)\Google\google_appengine\lib\six-1.9.0 存在。

我也试过直接在python shell中导入six包,成功了。

(py27-base) C:\>set PYTHONPATH=C:\Program Files (x86)\Google\google_appengine\lib\six-1.9.0

(py27-base) C:\>python
Python 2.7.15 |Anaconda, Inc.| (default, Nov 13 2018, 17:33:26) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import six
>>> six
<module 'six' from 'C:\Program Files (x86)\Google\google_appengine\lib\six-1.9.0\six\__init__.py'>
>>> exit()

所以我想知道是什么阻止了我的应用在 dev_appserver 环境中导入 six

环境细节

  • Windows 10 64 位,内部版本 1809
  • Python 2.7.15 :: Anaconda, Inc.
  • GAE Python 标准 SDK,版本 1.9.80。 不是 GCloud SDK

日志详情

2018-12-19 14:00:18 Running command: "['C:\\Users\\user\\AppData\\Local\\conda\\conda\\envs\\py27-base\\pythonw.exe', 'C:\\Program Files (x86)\\Google\\google_appengine\\dev_appserver.py', '--skip_sdk_update_check=yes', '--port=8080', '--admin_port=8000', 'C:\\Users\\user\\PycharmProjects\\gaeapp\\dist']"
INFO     2018-12-19 14:00:22,907 devappserver2.py:278] Skipping SDK update check.
INFO     2018-12-19 14:00:23,483 api_server.py:275] Starting API server at: http://localhost:54700
INFO     2018-12-19 14:00:23,493 dispatcher.py:256] Starting module "default" running at: http://localhost:8080
INFO     2018-12-19 14:00:23,497 admin_server.py:150] Starting admin server at: http://localhost:8000
INFO     2018-12-19 14:00:28,579 instance.py:294] Instance PID: 11908
INFO     2018-12-19 14:00:30,862 module.py:861] default: "GET / HTTP/1.1" 404 -
INFO     2018-12-19 14:00:35,223 module.py:434] [default] Detected file changes:
  blog\__init__.pyc
WARNING  2018-12-19 06:00:35,575 sandbox.py:1086] The module _winreg is whitelisted for local dev only. If your application relies on _winreg, it is likely that it will not function properly in production.

WARNING  2018-12-19 06:00:35,953 sandbox.py:1086] The module _ctypes is whitelisted for local dev only. If your application relies on _ctypes, it is likely that it will not function properly in production.

WARNING  2018-12-19 06:00:36,165 sandbox.py:1086] The module msvcrt is whitelisted for local dev only. If your application relies on msvcrt, it is likely that it will not function properly in production.

ERROR    2018-12-19 06:00:36,315 wsgi.py:263] 

Traceback (most recent call last):

  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\runtime\wsgi.py", line 240, in Handle

handler = _config_handle.add_wsgi_middleware(self._LoadHandler())

  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\runtime\wsgi.py", line 299, in _LoadHandler

handler, path, err = LoadObject(self._handler)

  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\runtime\wsgi.py", line 85, in LoadObject

obj = __import__(path[0])

  File "C:\Users\user\PycharmProjects\gaeapp\dist\blog\__init__.py", line 6, in <module>

from blog.resources import bloglist, blog, comment

  File "C:\Users\user\PycharmProjects\gaeapp\dist\blog\resources\__init__.py", line 7, in <module>

from flask_restful import fields

  File "C:\Users\user\PycharmProjects\gaeapp\dist\lib\flask_restful\fields.py", line 6, in <module>

import six

  File "C:\Program Files (x86)\Google\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 six

INFO     2018-12-19 14:00:36,362 module.py:861] default: "GET /blog/resources/blogs HTTP/1.1" 500 -
INFO     2018-12-19 14:00:41,301 instance.py:294] Instance PID: 19088
INFO     2018-12-19 14:00:41,301 module.py:434] [default] Detected file changes:
  blog

【问题讨论】:

  • @DanCornilescu,感谢您的指出。实际上,我的代码中的缩进是正确的。但不知何故在帖子中搞砸了。
  • 你能显示完整的回溯吗?您使用的是 GAE 还是 Cloud SDK?您似乎正在通过 Pycharm 运行开发服务器 - 您是否也可以尝试从 shell 运行它 - 以防万一这是 pycharm 配置问题?也许准确地展示你是如何运行开发服务器的(我的意思是命令行)。
  • 除了上面Dan的问题,你运行的是什么版本的Cloud SDK?您可以通过运行命令gcloud component list 找到它。
  • 在帖子中添加了更多详细信息。 @DanCornilescu,我在 PyCharm 中运行时观察到了这个错误,并使用 GAE 启动器重现了它。

标签: python google-app-engine google-cloud-platform


【解决方案1】:

我遇到了生产中存在的随机库在我的本地主机中不存在的问题(即 osx 上的 pycrypto)

所以,除了为我的所有第三方库提供一个 lib 文件夹之外:https://cloud.google.com/appengine/docs/standard/python/tools/using-libraries-python-27

我为这些有问题的库设置了一个名为 localhost_libs 的第二个文件夹

然后在我的appengine_config.py 我有这个:

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

if os.environ.get('SERVER_SOFTWARE', '').startswith('Development'):
    vendor.add('localhost_libs')

【讨论】:

【解决方案2】:

我按照您上面的描述创建了一个测试环境,但是在导入您在app.yaml 文件中指定的模块时没有遇到问题。另一个可能的故障排除方法是创建一个演示应用程序,以便仔细检查 GAE 启动器是否能够在 dev_appserver 环境中获取这些内置库。为此,通过进入 Google App Engine Launcher,File &gt; Add Demo Application &gt; python &gt; guestbook 并编辑 app.yamlguestbook.py 文件以导入(例如)six 模块并在留言簿页面中打印 var = six.__version__,您将能够检查是否引发了异常。

正如 Alex 所建议的,一种可能的解决方案是将第三方库直接复制到本地(子)目录,方法是将它们添加到 requirements.txt 文件,运行 pip install -t lib -r requirements.txt 并创建相应的 appengine_config.py文件,如this article 中所述。但请记住,仅将库导入“开发”环境,问题仍可能出现在生产环境中,在这种情况下,appengine_config.py 文件为:

# appengine_config.py
from google.appengine.ext import vendor

# Add any libraries install in the "lib" folder.
vendor.add('lib')

将是理想的(如 Alex 的回复中提供的文章所示)。

我还要补充一点,如果项目也有其他本地库(从回溯中似乎是这种情况),请确保将这些依赖项安装在您的应用程序中内部,如 @987654322 所述@。

【讨论】:

  • 实际上,我删除了lib 目录中的所有内容,重新安装了所有软件包,然后删除了运行时中包含的软件包。这一次,一切正常。我认为真正的解决方案只是重新安装软件包......
猜你喜欢
  • 1970-01-01
  • 2019-08-16
  • 1970-01-01
  • 2017-09-25
  • 2011-11-28
  • 2015-05-07
  • 1970-01-01
  • 1970-01-01
  • 2011-06-20
相关资源
最近更新 更多