Search code examples
pythonnginxmatplotlibweb2pydigital-ocean

Why does matplotlib not work on a digitalocean VPS with web2py?


I've got a digitalocean droplet and I deployed web2py using this script. I installed matplotlib as follows:

ssh root@ipdroplet
apt-get install python-matplotlib

and I can indeed import matplotlib if I simply run python on the command line after I've ssh'd. The problem is that when I run my app I get the following error:

<type 'exceptions.ImportError'> Cannot import module applications.app.modules.matplotlib

I'm guessing this has something to do with user www-data vs root but no idea how to resolve it. Any help much appreciated. The tips that are mentioned in this link unfortunately didn't help me.

Thanks

EDIT

I should also mention that I'm not using the binary version of web2py. I've also managed to run python as www-data by doing sudo -u www-data python and I can import matplotlib there just fine.

EDIT2

When I was running locally on web2py 2.11.2-stable it worked fine. On my server though I was under 2.12.3-stable. I'm guessing this is probably the reason.


Solution

  • So I figured out a solution.

    Since it wants to find the matplotlib module in the web2py application's module folder I created a symbolic link from there to the actual installation. I then needed to also change the backend as the little trick didn't work.

    ln -s modules/matplotlib path_to_matplotlib
    

    The way I got path_to_matplotlib was:

    ~#: python
    >>> import matplotlib
    >>> matplotlib
    <module 'matplotlib' from '/usr/lib/pymodules/python2.7/matplotlib>/__init__.pyc
    >>> print matplotlib.matplotlib_fname() # This will give the path to the backend settings
    /etc/matplotlibrc
    

    so the link command looks like:

    ln -s /home/www-data/web2py/applications/app/modules/matplotlib /usr/lib/pymodules/python2.7/matplotlib
    

    I also needed to change the backend, you get the path as shown above ( matplotlib_fname() ). Find the line where it says backend and make sure it is set to Agg.

    backend: Agg
    

    usually it's set to TkAgg.