ImportError: cannot import name sandbox

2019-08-17 03:18发布

Over the past couple days, this has started cropping up whenever I run dev_appserver.py:

from google.appengine.tools.devappserver2.python import sandbox
ImportError: cannot import name sandbox

Since I was primarily coming across this in the setup of a new environment, I figured it must be a mistake of mine during setup. After enough head-scratching over the past 3 hours, I figured it couldn't be in the new setup, so I loaded up dev_appserver.py in a known-to-be-working environment.

Yet again:

from google.appengine.tools.devappserver2.python import sandbox
ImportError: cannot import name sandbox

None of my app code had been changed, so it had to be something else.

1条回答
淡お忘
2楼-- · 2019-08-17 04:16

It turned out the SDK had changed.

I had a file named appengine_config.py that (specifically for the development server) whitelisted a couple of C modules.

from google.appengine.tools.devappserver2.python import sandbox
sandbox._WHITE_LIST_C_MODULES += ['_ssl', '_socket']

When I disabled these lines, this issue was replaced with another (the one that was the reason those lines were there to mitigate):

File "[...]/devappserver2/python/runtime/sandbox.py", line 1091, in load_module
    raise ImportError('No module named %s' % fullname)
ImportError: No module named _socket

Notice the runtime piece in the filepath? Apparently the location of the sandbox module had changed. So I added .runtime to the import path:

from google.appengine.tools.devappserver2.python.runtime import sandbox

Then, after re-enabling those lines, everything was working again! ✅ Mission accomplished.

查看更多
登录 后发表回答