When importing Numpy from Flask in Virtualenv Numpy gives multiarray import error When importing Numpy from Flask in Virtualenv Numpy gives multiarray import error flask flask

When importing Numpy from Flask in Virtualenv Numpy gives multiarray import error


Imports under mod_wsgi can be tricky, particularly with Python C extension libraries.

The issue here turned out to be setting the WSGIDaemonProcess python-path= option to a venv with the compiled module. From those docs:

If using a Python virtual environment, rather than use this option to refer to the site-packages directory of the Python virtual environment, you should use the python-home option to specify the root of the Python virtual environment instead.

In all cases, if the directory contains Python packages which have C extension components, those packages must have been installed using the same base Python version as was used to compile the mod_wsgi module. You should not mix packages from different Python versions or installations.

So, the python-home= option must be used for the venv dir given, and either Python 2.7 should be used for the venv, or the Python 3 build of mod_wsgi must be installed (libapache2-mod-wsgi-py3 on Ubuntu).

For some applications, the WSGIApplicationGroup needs to be configured as follows too:

WSGIApplicationGroup %{GLOBAL}

In the case that any of the C extensions use the simplified threading api.

From similar questions, α, β, this seems to be a common issue.