使用 Google 云平台的本地开发服务器导入内置模块“_subprocess”时出错

Error importing built-in module "_subprocess" using Google Cloud Platform's Local Development Server

有谁知道如何解决以下错误? Error Message: "Import Error

C:\Users\MicroSilicon\Desktop\hello_world>python2 "C:\Users\MicroSilicon\AppData\Local\Google\Cloud SDK\google-cloud-sdk\bin\dev_appserver.py" app.yaml
INFO     2019-12-16 09:23:23,341 devappserver2.py:285] Skipping SDK update check.
INFO     2019-12-16 09:23:23,506 api_server.py:282] Starting API server at: http://localhost:60054
INFO     2019-12-16 09:23:23,509 dispatcher.py:263] Starting module "default" running at: http://localhost:8080
INFO     2019-12-16 09:23:23,512 admin_server.py:150] Starting admin server at: http://localhost:8000
INFO     2019-12-16 09:23:25,522 instance.py:294] Instance PID: 7284
INFO     2019-12-16 09:23:37,250 module.py:434] [default] Detected file changes:
  main.pyc
WARNING  2019-12-16 15:23:37,354 sandbox.py:1104] 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    2019-12-16 15:23:37,355 wsgi.py:269]
Traceback (most recent call last):
  File "C:\Users\MicroSilicon\AppData\Local\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\google\appengine\runtime\wsgi.py", line 240, in Handle
    handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
  File "C:\Users\MicroSilicon\AppData\Local\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\google\appengine\runtime\wsgi.py", line 311, in _LoadHandler
    handler, path, err = LoadObject(self._handler)
  File "C:\Users\MicroSilicon\AppData\Local\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\google\appengine\runtime\wsgi.py", line 85, in LoadObject
    obj = __import__(path[0])
  File "C:\Users\MicroSilicon\Desktop\hello_world\main.py", line 16, in <module>
    import subprocess
  File "C:\Users\MicroSilicon\AppData\Local\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\google\appengine\tools\devappserver2\python\runtime\sandbox.py", line 1043, in load_module
    return self.import_stub_module(fullname)
  File "C:\Users\MicroSilicon\AppData\Local\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\google\appengine\tools\devappserver2\python\runtime\sandbox.py", line 1049, in import_stub_module
    __import__(fullname, {}, {})
  File "C:\Users\MicroSilicon\AppData\Local\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\google\appengine\dist27\subprocess.py", line 8, in <module>
    from python_std_lib import subprocess
  File "C:\Users\MicroSilicon\AppData\Local\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\google\appengine\dist27\python_std_lib\subprocess.py", line 417, in <module>
    import _subprocess
  File "C:\Users\MicroSilicon\AppData\Local\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\google\appengine\tools\devappserver2\python\runtime\sandbox.py", line 1113, in load_module
    raise ImportError('No module named %s' % fullname)
ImportError: No module named _subprocess

我已经为 Windows 安装了 Google Cloud SDK 并勾选了复选框以获得捆绑的 python 安装(版本 2.7.13)。 Python Installation Check

基本上按照下面 link 中的说明让 Hello World 应用程序在本地环境中工作(直到步骤 "Make a change")。 https://cloud.google.com/appengine/docs/standard/python/quickstart

现在,当我将语句 import subprocess 添加到 main.py 文件时出现了问题。

请注意,确切的问题出在模块 "subprocess.py" 中的第 import _subprocess 行。这对我来说很奇怪,因为如果我尝试 运行 任何基本 python 脚本(不使用 dev_appserver.py app.yaml 来部署 Google 云环境)或者如果我只是使用 python 解释器直接从控制台(Windows 命令提示符)我在尝试 import subprocess 或直接 import _subprocess 时都没有收到任何错误。 Import Statement from Console Python.

这是 Hello World 代码(添加了子流程导入):

# Copyright 2016 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import webapp2
import subprocess

class MainPage(webapp2.RequestHandler):
    def get(self):
        self.response.headers['Content-Type'] = 'text/plain'
        self.response.write('Hello, World!')



app = webapp2.WSGIApplication([
    ('/', MainPage),
], debug=True)

最后,我的熟人之前安装了相同的软件,并且在 运行 任何使用此导入语句的应用程序时都没有出现此类错误。

注意:我正在 Windows 10 机器上工作。

我在想,据我们所知,可能是你的安装有问题,可能是你的SDK安装和你的Python安装有问题,你能重新安装并再次检查吗?

只是我试过了没有问题,正如你指定的其他人也做了同样的事情并且没有问题,也许问题出在你的电脑上安装了这些产品中的任何一个。

[更新]

我无法在我安装的 Google Cloud SDK 版本中解决这个问题。

但是,安装旧版本(特别是捆绑了 python 的版本 220.0.0)解决了 import subprocess 错误。就我而言,这是可以接受的。

以下是我的工作安装的详细信息:

C:\Users\MicroSilicon>gcloud version
Google Cloud SDK 220.0.0
app-engine-python 1.9.77
app-engine-python-extras 1.9.74
bq 2.0.34
cloud-datastore-emulator 2.0.2
core 2018.10.08
gsutil 4.34

我 运行 也对此进行了调查,发现 GAE 正在使用驻留在 dist27 文件夹中的子进程的修改版本。查看文件 subprocess.py 我发现它监听了一个环境变量 GAE_USE_SUBPROCESS.

GAE版本(gcloud版本

>gcloud version
Google Cloud SDK 308.0.0
app-engine-python 1.9.91
app-engine-python-extras 1.9.90
beta 2020.08.28
bq 2.0.60
cloud-datastore-emulator 2.1.0
core 2020.08.28
gsutil 4.53
kubectl 1.15.11

解决方案

通过将以下内容添加到您的 app.yaml 文件来禁用子进程的使用:

env_variables:
  GAE_USE_SUBPROCESS: 0

这至少对我有用(运行 in win64/windows 10)