Forums

error when importing matplotlib to flask app

I added the following three lines to my python 3.7 flask app


import matplotlib matplotlib.use("Agg") import matplotlib.pyplot as plt


and I get this error when I run the app

Traceback (most recent call last): File "/home/openDataAdvocate/pavement/flask_app.py", line 5, in <module> import matplotlib File "/usr/lib/python3.7/site-packages/matplotlib/init.py", line 138, in <module> from . import cbook, rcsetup File "/usr/lib/python3.7/site-packages/matplotlib/cbook/init.py", line 31, in <module> import numpy as np File "/usr/lib/python3.7/site-packages/numpy/init.py", line 142, in <module> from . import core File "/usr/lib/python3.7/site-packages/numpy/core/init.py", line 17, in <module> from . import multiarray File "/usr/lib/python3.7/site-packages/numpy/core/multiarray.py", line 14, in <module> from . import overrides File "/usr/lib/python3.7/site-packages/numpy/core/overrides.py", line 47, in <module> """) RuntimeError: implement_array_function method already has a docstring

These seems like it should be straight forward. What am I missing?

Thanks for your help

This is because of an issue with numpy https://github.com/numpy/numpy/issues/14384 that prevents it from running in our web server software. From the issue it appears that it also causes trouble for various other pieces of software that use numpy. It appears to have been introduced after version 1.16.0 of numpy, so if you use that version it should be ok.

Thank you for your response. I'm not sure how to force 1.16.0 to be used.

I tried this:

import pkg_resources pkg_resources.require("numpy==`1.16.0") # modified to use specific numpy 1.16.0

but generates a new error:

Traceback (most recent call last): File "/home/openDataAdvocate/pavement/flask_app.py", line 6, in <module> pkg_resources.require("numpy==1.16.0") # modified to use specific numpy 1.16.0 File "/usr/lib/python3.7/site-packages/pkg_resources/__init__.py", line 900, in require needed = self.resolve(parse_requirements(requirements)) File "/usr/lib/python3.7/site-packages/pkg_resources/__init__.py", line 791, in resolve raise VersionConflict(dist, req).with_context(dependent_req) pkg_resources.VersionConflict: (numpy 1.17.3 (/usr/lib/python3.7/site-packages), Requirement.parse('numpy==1.16.0'))

Do you know how to force the server to use 1.16.0 from within my flask app?

Thank you

Install the version that you want to use. You'll need to use a virtualenv. See http://help.pythonanywhere.com/pages/InstallingNewModules/

I setup a python 3.6 virtual environment named open_roads and it can be found at:

/home/openDataAdvocate/.virtualenvs/open_roads

I loaded flask, flask_login, flask_sqlalchemy, mysql, numpy==1.16.0, matplotlib

I updated the Web App to point to the virtual environment and reloaded the web app.

in my flask_app.py file, I added this line:

import pandas as pd

and I'm getting the exact same error

Traceback (most recent call last):
  File "/home/openDataAdvocate/pavement/flask_app.py", line 5, in <module>
    import pandas as pd
  File "/usr/lib/python3.7/site-packages/pandas/__init__.py", line 11, in <module>
    __import__(dependency)
  File "/usr/lib/python3.7/site-packages/numpy/__init__.py", line 142, in <module>
    from . import core
  File "/usr/lib/python3.7/site-packages/numpy/core/__init__.py", line 17, in <module>
    from . import multiarray
  File "/usr/lib/python3.7/site-packages/numpy/core/multiarray.py", line 14, in <module>
    from . import overrides
  File "/usr/lib/python3.7/site-packages/numpy/core/overrides.py", line 47, in <module>
    """)
RuntimeError: implement_array_function method already has a docstring

and I get the exact same error:

Traceback (most recent call last):
  File "/home/openDataAdvocate/pavement/flask_app.py", line 5, in <module>
    import pandas as pd
  File "/usr/lib/python3.7/site-packages/pandas/__init__.py", line 11, in <module>
    __import__(dependency)
  File "/usr/lib/python3.7/site-packages/numpy/__init__.py", line 142, in <module>
    from . import core
  File "/usr/lib/python3.7/site-packages/numpy/core/__init__.py", line 17, in <module>
    from . import multiarray
  File "/usr/lib/python3.7/site-packages/numpy/core/multiarray.py", line 14, in <module>
    from . import overrides
  File "/usr/lib/python3.7/site-packages/numpy/core/overrides.py", line 47, in <module>
    """)
RuntimeError: implement_array_function method already has a docstring

I noticed that the interpreter still says python 3.7. Could this be the problem?

Based on this forum post,

https://help.pythonanywhere.com/pages/SaveAndRunPythonVersion/

I tried to add

1
#!/home/myusername/.virtualenvs/myvenv/bin/python

or

1
#!/usr/bin/python3.6

at the top of my file, but it didn't make any differences.

Any other suggestions?

[edit by admin: formatting]

Where are you seeing that error message? Remember, for websites, you don't need to run your code from the editor. The website is always running, and any errors that it generates will be displayed in the website's error log, which is linked from the "Web" page inside PythonAnywhere. The most recent error message will be at the bottom of the file.

I'm getting this error when clicking the ">>> Run" button while editing the /home/openDataAdvocate/pavement/flask_app.py file.

My webapp will not run when this error occurs.

If I comment out the "import pandas as pd" line, and press ">>> Run" again, the app runs.

The log file at:

https://www.pythonanywhere.com/user/openDataAdvocate/files/var/log/opendataadvocate.pythonanywhere.com.error.log

is blank in both cases.

Thanks for your help.

If you've changed the shebang in your file, you will need to close the console after the change so that the interpreter gets reloaded.

If your error log is empty, it's possible that the problem was too early in the process for it to reach the error log. In that case, there may be a traceback or some other message in your server log.