2

I want to use matplotlib in my Google App Engine project. I followed the steps, described here in the official docs. What I did:

1) Created a directory named lib in my application root directory.

2) Created a file appengine_config.py in my application root directory and added there these lines:

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

3) Since the docs say, that the only version of matplotlib working is 1.2.0, I executed the following command in the Terminal:

pip install -t lib matplotlib==1.2.0

There is also step 0 in the docs, which says

Use pip to install the library and the vendor module to enable importing packages from the third-party library directory.

But I don't understand what it actually means. If this is something essential, please, explain to me what it's meant here. I found this answer here on stackoverflow, and It seems there is nothing different from what I have done.

Also, I added

libraries:
- name: matplotlib
  version: "1.2.0"

to app.yaml.

So, after all these steps I add the line

import matplotlib

to main.py and start a local server with

python ~/path/google_appengine/dev_appserver.py app.yaml

But when I try to access http://localhost:8080/, the error is raised:

raise ImportError('No module named %s' % fullname)
ImportError: No module named _ctypes

The whole output, if needed, looks like this:

ERROR    2016-08-11 16:26:51,621 wsgi.py:263] 
Traceback (most recent call last):
  File "/home/magnitofon/Загрузки/google_appengine/google/appengine/runtime/wsgi.py", line 240, in Handle
    handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
  File "/home/magnitofon/Загрузки/google_appengine/google/appengine/runtime/wsgi.py", line 299, in _LoadHandler
    handler, path, err = LoadObject(self._handler)
  File "/home/magnitofon/Загрузки/google_appengine/google/appengine/runtime/wsgi.py", line 85, in LoadObject
    obj = __import__(path[0])
  File "/home/magnitofon/realec-inspector/main.py", line 20, in <module>
    import matplotlib
  File "/home/magnitofon/realec-inspector/lib/matplotlib/__init__.py", line 151, in <module>
    from matplotlib.rcsetup import (defaultParams,
  File "/home/magnitofon/realec-inspector/lib/matplotlib/rcsetup.py", line 20, in <module>
    from matplotlib.colors import is_color_like
  File "/home/magnitofon/realec-inspector/lib/matplotlib/colors.py", line 52, in <module>
    import numpy as np
  File "/home/magnitofon/Загрузки/google_appengine/google/appengine/tools/devappserver2/python/sandbox.py", line 705, in load_module
    module = self._find_and_load_module(fullname, fullname, [module_path])
  File "/home/magnitofon/Загрузки/google_appengine/google/appengine/tools/devappserver2/python/sandbox.py", line 446, in _find_and_load_module
    return imp.load_module(fullname, source_file, path_name, description)
  File "/usr/local/lib/python2.7/dist-packages/numpy/__init__.py", line 180, in <module>
    from . import add_newdocs
  File "/usr/local/lib/python2.7/dist-packages/numpy/add_newdocs.py", line 13, in <module>
    from numpy.lib import add_newdoc
  File "/usr/local/lib/python2.7/dist-packages/numpy/lib/__init__.py", line 8, in <module>
    from .type_check import *
  File "/usr/local/lib/python2.7/dist-packages/numpy/lib/type_check.py", line 11, in <module>
    import numpy.core.numeric as _nx
  File "/usr/local/lib/python2.7/dist-packages/numpy/core/__init__.py", line 22, in <module>
    from . import _internal  # for freeze programs
  File "/usr/local/lib/python2.7/dist-packages/numpy/core/_internal.py", line 14, in <module>
    import ctypes
  File "/usr/lib/python2.7/ctypes/__init__.py", line 10, in <module>
    from _ctypes import Union, Structure, Array
  File "/home/magnitofon/Загрузки/google_appengine/google/appengine/tools/devappserver2/python/sandbox.py", line 963, in load_module
    raise ImportError('No module named %s' % fullname)
ImportError: No module named _ctypes

What am I doing wrong?

Community
  • 1
  • 1

2 Answers2

1

matplotlib is one of the Google-provided 3rd party libs, so you should be following just the Requesting a library instructions and not the Installing a library ones.

Sadly they're now both on the same documentation page, called Using Built-in Libraries in Python 2.7 - very confusing for the unaware as the vendoring technique should be used for libraries which are not GAE built-in/provided. Filed Issue 13202.

Note: pay attention to the Using libraries with the local development server section, it applies to matplotlib. You may need to install some packages on your system, but not in the application itself (which could negatively affect your deployment on GAE) - they need to be accessible by the development server, not directly by your application.

Duh, I just noticed the Using matplotlib section, on the same page :)

It mentions:

Note: The experimental release of matplotlib is not supported on the development server. You can still add matplotlib to the libraries list, but it will raise an ImportError exception when imported.

Dan Cornilescu
  • 39,470
  • 12
  • 57
  • 97
  • So, I can't use matplotlib with the local server, because there will always be an ImportError. Although, I am still able to use matplotlib in my application code, then deploy the application to Google App Enging, and It is going to work there? Do I understand you correctly? – Ilya Golubev Aug 12 '16 at 11:41
  • Thank you for the answer. I used [this](http://gae-matplotlib-demo.appspot.com/) example, and matplotlib succesfully works after deploying. Even though it is not much of a conviniece not to be able to run it locally, still I'm glad it runs. – Ilya Golubev Aug 12 '16 at 12:38
  • I'd still give it a shot. The only known related issues I could find are https://code.google.com/p/googleappengine/issues/detail?id=12176 and https://code.google.com/p/googleappengine/issues/detail?id=12783 - workarounds appear available. Also be aware of this issue which can interfere as well: http://stackoverflow.com/questions/37755195/importerror-no-module-named-webapp2-after-linux-sdk-upgrade-1-9-35-1-9-38. – Dan Cornilescu Aug 12 '16 at 12:52
0

Some searching into this issue surfaced this (old) recipe:

http://code.activestate.com/recipes/578393-gae-matplotlib-demo/

be sure to read the comments.

However I tried, I was not able to make it work. It be this approach just does not work with the current version of the app engine sandbox anymore, or I just was not able to follow all the steps in the recipe correctly.

Maybe an even better read are the comments of Matt Giuca (a google engineer who ported matplotlib to work in GAE production env.) and his pull request in github.

https://github.com/matplotlib/matplotlib/issues/1823/

His comment of April 17, 2013 gives a recipe for matching the dev_appserver (version 1.77) to work with the matplotlib locally.

Maybe this helps someone dealing with this issue.

Matthijs
  • 1,484
  • 1
  • 14
  • 13