简体   繁体   English

导入错误:没有名为 _ctypes 的模块。 带有散景图的 Google 应用引擎

[英]ImportError: No module named _ctypes. Google app engine with bokeh plot

  • Python 2.7.13蟒蛇 2.7.13
  • Windows 10 64 bit视窗 10 64 位

I've been working through this Udacity web dev course and wanted to try embedding a simple bokeh plot into a web page using this example .我一直在学习这个Udacity 网络开发课程,并想尝试使用这个示例将一个简单的散景图嵌入到网页中。 Running dev_appserver.py gives the error: ImportError: No module named _ctypes运行dev_appserver.py给出错误: ImportError: No module named _ctypes

I have:我有:

  • Installed Bokeh and Numpy via pip通过 pip 安装 Bokeh 和 Numpy
  • Included Numpy in app.yaml在 app.yaml 中包含 Numpy

This answer states Google App Engine doesn't allow importing ctypes. 此答案指出 Google App Engine 不允许导入 ctypes。 However I'm unsure how to confirm if this is the case with Bokeh.但是,我不确定如何确认 Bokeh 是否属于这种情况。 Is this error caused by Bokeh importing ctypes?这个错误是由 Bokeh 导入 ctypes 引起的吗? If so is there a work around?如果是这样,是否有解决方法?

ERROR    2017-01-21 19:14:53,799 wsgi.py:263]
Traceback (most recent call last):
  File "C:\Users\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\AppData\Local\Google\Cloud SDK\google-cloud-sdk\platform
\google_appengine\google\appengine\runtime\wsgi.py", line 299, in _LoadHandler
    handler, path, err = LoadObject(self._handler)
  File "C:\Users\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\Google Drive\Udacity web development 2017\udacit
y-cs253\bokeh\1_docs_example\main.py", line 2, in <module>
    from bokeh.plotting import figure
  File "C:\Users\Google Drive\Udacity web development 2017\udacit
y-cs253\bokeh\1_docs_example\lib\bokeh\plotting\__init__.py", line 2, in <module
>
    from ..document import Document; Document
  File "C:\Users\Google Drive\Udacity web development 2017\udacit
y-cs253\bokeh\1_docs_example\lib\bokeh\document.py", line 45, in <module>
    from .core.json_encoder import serialize_json
  File "C:\Users\Google Drive\Udacity web development 2017\udacit
y-cs253\bokeh\1_docs_example\lib\bokeh\core\json_encoder.py", line 43, in <modul
e>
    import numpy as np
  File "C:\Users\AppData\Local\Google\Cloud SDK\google-cloud-sdk\platform
\google_appengine\google\appengine\tools\devappserver2\python\sandbox.py", line
706, in load_module
    module = self._find_and_load_module(fullname, fullname, [module_path])
  File "C:\Users\AppData\Local\Google\Cloud SDK\google-cloud-sdk\platform
\google_appengine\google\appengine\tools\devappserver2\python\sandbox.py", line
447, in _find_and_load_module
    return imp.load_module(fullname, source_file, path_name, description)
  File "D:\Python27\lib\numpy\__init__.py", line 142, in <module>
    from . import add_newdocs
INFO     2017-01-21 19:14:53,859 module.py:806] default: "GET / HTTP/1.1" 500 -
  File "D:\Python27\lib\numpy\add_newdocs.py", line 13, in <module>
    from numpy.lib import add_newdoc
  File "D:\Python27\lib\numpy\lib\__init__.py", line 8, in <module>
    from .type_check import *
  File "D:\Python27\lib\numpy\lib\type_check.py", line 11, in <module>
    import numpy.core.numeric as _nx
  File "D:\Python27\lib\numpy\core\__init__.py", line 33, in <module>
    from . import _internal  # for freeze programs
  File "D:\Python27\lib\numpy\core\_internal.py", line 14, in <module>
    import ctypes
  File "D:\Python27\lib\ctypes\__init__.py", line 7, in <module>
    from _ctypes import Union, Structure, Array
  File "C:\Users\AppData\Local\Google\Cloud SDK\google-cloud-sdk\platform
\google_appengine\google\appengine\tools\devappserver2\python\sandbox.py", line
964, in load_module
    raise ImportError('No module named %s' % fullname)
ImportError: No module named _ctypes

app.yaml:应用程序.yaml:

runtime: python27
api_version: 1
threadsafe: true

handlers:
- url: /.*
  script: main.app

libraries:
- name: jinja2
  version: latest
- name: numpy
  version: latest

main.py主文件

import os, webapp2, jinja2
from bokeh.plotting import figure
from bokeh.embed import components

plot = figure()
plot.circle([1,2], [3,4])
script, div = components(plot)

template_dir = os.path.join(os.path.dirname(__file__), 'templates')
jinja_env = jinja2.Environment(loader = jinja2.FileSystemLoader(template_dir),
                                autoescape = True)

class Handler(webapp2.RequestHandler):
    def write(self, *a, **kw):
        self.response.write(*a, **kw)

    def render_str(self, template, **kw):
        t = jinja_env.get_template(template)
        return t.render(kw)

    def render(self, template, **kw):
        self.write(self.render_str(template, **kw))

class MainPage(Handler):
    def get(self):
        self.render("chart.html", script = script, div = div)

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

chart.html图表.html

{% extends "base.html" %}

{% block content %}

<!-- Load BokehJS -->
<link
    href="http://cdn.bokeh.org/bokeh/release/bokeh-0.12.0.min.css"
    rel="stylesheet" type="text/css">
<link
    href="http://cdn.bokeh.org/bokeh/release/bokeh-widgets-0.12.0.min.css"
    rel="stylesheet" type="text/css">

<script src="http://cdn.bokeh.org/bokeh/release/bokeh-0.12.0.min.js"></script>
<script src="http://cdn.bokeh.org/bokeh/release/bokeh-widgets-0.12.0.min.js"></script>

{{ script }}
{{ div }}

{% endblock %}

base.html基本文件

<!DOCTYPE html>
<html>
<head>
    <title>Udacity Templates!</title>
</head>
<body style="margin: 0">
    <h1 style="background-color: #ddd; color: #888; margin: 0; height: 50px">Udacity Templates</h1>
    {% block content %}
    {% endblock %}
</body>
</html>

gcloud version gcloud 版本

  • Google Cloud SDK 139.0.1谷歌云 SDK 139.0.1
  • app-engine-python 1.9.49应用引擎python 1.9.49
  • bq 2.0.24烧烤 2.0.24
  • bq-win 2.0.24 bq-win 2.0.24
  • bundled-python 2.7.10捆绑python 2.7.10
  • core 2017.01.12核心 2017.01.12
  • core-win 2016.11.07芯赢 2016.11.07
  • gcloud
  • gsutil 4.22 gsutil 4.22
  • gsutil-win 4.20 gsutil-win 4.20
  • powershell 1.0.0.1 PowerShell 1.0.0.1
  • windows-ssh-tools 2016.05.13 windows-ssh-工具 2016.05.13

UPDATE 1: I have uninstalled numpy 1.12.0 and installed numpy 1.6.1.更新 1:我已经卸载了 numpy 1.12.0 并安装了 numpy 1.6.1。 I now get this error:我现在收到此错误:

NP_MS_DELTA = np.timedelta64(1, 'ms')
TypeError: function takes at most 1 argument (2 given)

This states numpy 1.6.1 can't specify unit in scalar constructor. 表明 numpy 1.6.1 不能在标量构造函数中指定单位。 Does this mean Bokeh relies on numpy>1.6.1?这是否意味着 Bokeh 依赖于 numpy>1.6.1?

ERROR    2017-01-22 10:37:45,980 wsgi.py:263]
Traceback (most recent call last):
  File "C:\Users\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\AppData\Local\Google\Cloud SDK\google-cloud-sdk\platform
\google_appengine\google\appengine\runtime\wsgi.py", line 299, in _LoadHandler
    handler, path, err = LoadObject(self._handler)
  File "C:\Users\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\Google Drive\Udacity web development 2017\udacit
y-cs253\bokeh\1_docs_example\main.py", line 2, in <module>
    from bokeh.plotting import figure
  File "C:\Users\Google Drive\Udacity web development 2017\udacit
y-cs253\bokeh\1_docs_example\lib\bokeh\plotting\__init__.py", line 2, in <module
>
    from ..document import Document; Document
  File "C:\Users\Google Drive\Udacity web development 2017\udacit
y-cs253\bokeh\1_docs_example\lib\bokeh\document.py", line 45, in <module>
    from .core.json_encoder import serialize_json
  File "C:\Users\Google Drive\Udacity web development 2017\udacit
y-cs253\bokeh\1_docs_example\lib\bokeh\core\json_encoder.py", line 53, in <modul
e>
    NP_MS_DELTA = np.timedelta64(1, 'ms')
TypeError: function takes at most 1 argument (2 given)
INFO     2017-01-22 10:37:46,170 module.py:806] default: "GET / HTTP/1.1" 500 -

UPDATE 2:更新 2:

This reminds me of when I pip installed Bokeh.这让我想起了我 pip 安装 Bokeh 的时候。 When downloading it's dependency numpy it seemed to rely on numpy>=1.7.1:下载它的依赖 numpy 时,它似乎依赖于 numpy>=1.7.1:

Collecting numpy>=1.7.1 (from Bokeh)

I managed to get the dev server running using 2 hacks我设法使用 2 个 hacks 使开发服务器运行

ImportError: No module named _ctypes

This is actually caused by flask New Flask uses library named click which uses ctypes Gae doesn't allow ctypes Solution: Install and older version of click with这实际上是由flask 引起的 New Flask 使用名为 click 的库,它使用 ctypes Gae 不允许 ctypes 解决方案:安装旧版本的 click with

pip install --target lib --upgrade click==5.1

This fixes ctypes but causes another error这修复了 ctypes 但会导致另一个错误

ImportError: No module named msvcrt

This can be easily fixed by adding this lines to appengine_config.py (located in the same folder as app.yaml )这可以通过将此行添加到 appengine_config.py(与 app.yaml 位于同一文件夹中)轻松解决

import os, sys

on_appengine = os.environ.get('SERVER_SOFTWARE','').startswith('Development')
if on_appengine and os.name == 'nt':
    sys.platform = "Not Windows"

After this dev servers starts and works在此开发服务器启动并工作后

work around issued from google here:从谷歌这里发出的解决方法:

https://issuetracker.google.com/issues/38290292 https://issuetracker.google.com/issues/38290292

  • goto <sdk_root>\\google\\appengine\\tools\\devappserver2\\python\\sandbox.py转到<sdk_root>\\google\\appengine\\tools\\devappserver2\\python\\sandbox.py
  • find the definition of _WHITE_LIST_C_MODULES = [xxx]找到_WHITE_LIST_C_MODULES = [xxx]的定义
    add following two lines to the list:将以下两行添加到列表中:

     '_winreg', '_ctypes',
  • try your app again.再次尝试您的应用。

worked for me.为我工作。

Edit: Please see the answer below for a new workaround from Google.编辑:请参阅下面的答案以了解来自 Google 的新解决方法。


I can state categorically that Bokeh itself does not use ctypes directly, anywhere in the library.我可以明确地说 Bokeh 本身不会在库中的任何地方直接使用ctypes But it does use NumPy, and it seems that at least some versions of NumPy do use ctypes ?但它确实使用了 NumPy,而且似乎至少某些版本的 NumPy 确实使用了ctypes This link:这个链接:

http://kawahara.ca/using-numpy-on-google-app-engine-with-the-anaconda-python-distribution/ http://kawahara.ca/using-numpy-on-google-app-engine-with-the-anaconda-python-distribution/

seems to suggest that only version 1.6 of NumPy is supported on GAE.似乎表明 GAE支持 NumPy 1.6 版。 I might assume this is either because that version does not use ctypes, or because Google has specifically whitelisted that version as acceptable somehow.我可能会认为这可能是因为该版本使用ctypes的,或者是因为谷歌已经明确列入白名单那个版本可以接受的某种方式。

So the suggestion would be specifically install NumPy 1.6, and not the latest version (either using pip or conda or whatever).所以建议是专门安装 NumPy 1.6,而不是最新版本(使用 pip 或 conda 或其他)。

这对我来说在 14.04LTS 和基本内核 4.1+ 上运行良好......安装 python3.7 时:

sudo apt-get install libffi-dev

您可以将包6添加到您的 requirements.txt 为我修复

pip install -t lib/ six

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM