Ich habe diesen Udacity web dev course durchgearbeitet und wollte versuchen, ein einfaches Bokeh-Diagramm mit diesem Beispiel in eine Webseite einzubetten. Beim Ausführen von dev_appserver.py
wird der Fehler angezeigt: ImportError: No module named _ctypes
Ich habe:
Diese Antwort gibt an, dass Google App Engine den Import von C-Typen nicht zulässt. Ich bin mir jedoch nicht sicher, wie ich feststellen kann, ob dies bei Bokeh der Fall ist. Wird dieser Fehler dadurch verursacht, dass Bokeh ctypes importiert? Wenn ja, gibt es eine Arbeit?
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:
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
{% extends "base.html" %}
{% block content %}
<!-- Load BokehJS -->
<link
href="http://cdn.pydata.org/bokeh/release/bokeh-0.12.0.min.css"
rel="stylesheet" type="text/css">
<link
href="http://cdn.pydata.org/bokeh/release/bokeh-widgets-0.12.0.min.css"
rel="stylesheet" type="text/css">
<script src="http://cdn.pydata.org/bokeh/release/bokeh-0.12.0.min.js"></script>
<script src="http://cdn.pydata.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
UPDATE 1: Ich habe numpy 1.12.0 deinstalliert und numpy 1.6.1 installiert. Ich bekomme jetzt diesen Fehler:
NP_MS_DELTA = np.timedelta64(1, 'ms')
TypeError: function takes at most 1 argument (2 given)
This states numpy 1.6.1 kann keine Einheit im Skalar-Konstruktor angeben. Bedeutet das, dass Bokeh auf numpy> 1.6.1 setzt?
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:
Das erinnert mich daran, als ich Bokeh installiert habe. Beim Herunterladen seiner Abhängigkeitsanzahl schien es auf numpy> = 1.7.1 zu setzen:
Collecting numpy>=1.7.1 (from Bokeh)
Bearbeiten: In der folgenden Antwort finden Sie eine neue Problemumgehung von Google.
Ich kann kategorisch feststellen, dass Bokeh ctypes
nicht direkt in der Bibliothek verwendet. Aber es verwendet NumPy und es scheint, dass zumindest einige Versionen von NumPy ctypes
verwenden. Dieser Link:
http://kawahara.ca/using-numpy-on-google-app-engine-with-the-anaconda-python-distribution/
scheint zu vermuten, dass only Version 1.6 von NumPy auf GAE unterstützt wird. Ich könnte davon ausgehen, dass dies entweder daran liegt, dass nicht ctypes verwendet, oder weil Google diese Version irgendwie als akzeptabel markiert hat.
Der Vorschlag wäre also speziell NumPy 1.6 zu installieren und nicht die neueste Version (entweder mit Pip oder Conda oder was auch immer).
Ich konnte den Dev-Server mit 2 Hacks zum Laufen bringen
ImportError: No module named _ctypes
Dies wird tatsächlich durch die Flasche .__ verursacht. New Flask verwendet eine Bibliothek mit dem Namen click, die die ctypes Verwendet
pip install --target lib --upgrade click==5.1
Dies behebt ctypes, verursacht aber einen anderen Fehler
ImportError: No module named msvcrt
Dies kann leicht behoben werden, indem diese Zeilen zu appengine_config.py hinzugefügt werden (im selben Ordner wie 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"
Danach starten die Dev-Server und funktionieren
umgehung von Google hier herausgegeben:
https://issuetracker.google.com/issues/38290292
<sdk_root>\google\appengine\tools\devappserver2\python\sandbox.py
finde die Definition von _WHITE_LIST_C_MODULES = [xxx]
Fügen Sie der Liste folgende zwei Zeilen hinzu:
'_winreg',
'_ctypes',
arbeitete für mich.
Dies funktionierte gut für 14.04LTS und Basiskernel 4.1+ ... bei der Installation von python3.7
Sudo apt-get install libffi-dev
Sie können das Paket six in Ihre Anforderungen einfügen
pip install -t lib/ six