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

flask, import, numpy, python, virtualenv

Solution

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.

Problem

I'am trying to import numpy at flask `__init__.py` but it gives this error : ``` Importing the multiarray numpy extension module failed. Most likely you are trying to import a failed build of numpy. If you're working with a numpy git repo, try `git clean -xdf` (removes all files not under version control). Otherwise reinstall numpy. ``` When I remove the import from flasks `__init__.py` everything works. When I do `from numpy.core import multiarray` in virtualenv everything works but importing it from wsgi is not working. here is the apache/site-available config file: ``` <VirtualHost *:80> ServerName 192.168.0.1 ServerAdmin hello@world.com WSGIScriptAlias / /home/bar/FlaskApp/FlaskApp/FlaskApp.wsgi <Directory /home/bar/FlaskApp/FlaskApp/> Require all granted </Directory> Alias /static /home/bar/FlaskApp/FlaskApp/static <Directory /home/bar/FlaskApp/FlaskApp/static/> Require all granted </Directory> ErrorLog ${APACHE_LOG_DIR}/error.log LogLevel warn CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost> WSGIDaemonProcess FlaskApp python-path=/home/bar/FlaskApp:/home/bar/FlaskApp/FlaskApp/venv/lib/python3.5/site-packages WSGIProcessGroup FlaskApp ``` and here is wsgi file: ``` #!/usr/bin/python import sys import logging logging.basicConfig(stream=sys.stderr) sys.path.insert(0,"/home/bar/FlaskApp") from FlaskApp import app as application application.secret_key = 'FlaskApp' ``` Thanks Additional Info: I have no problems importing other modules like pandas, flask or os. Originally i import pandas so error comes as a pandas dependency error. from `/var/log/apache/error.log`: ``` Traceback (most recent call last): File "/home/bar/FlaskApp/FlaskApp/FlaskApp.wsgi", line 7, in <module> from FlaskApp import app as application File "/home/bar/FlaskApp/FlaskApp/__init__.py", line 3, in <module> from myscript import myclass File "/home/bar/FlaskApp/FlaskApp/myscript.py", line 1, in <module> import pandas as pd File "/home/bar/FlaskApp/FlaskApp/venv/lib/python3.5/site-packages/pandas/__init__.py", line$ "Missing required dependencies {0}".format(missing_dependencies)) ImportError: Missing required dependencies ['numpy'] ``` Here is the error log when i tried to import numpy directly from `__init__.py`: ``` Traceback (most recent call last): File "/home/bar/FlaskApp/FlaskApp/FlaskApp.wsgi", line 7, in <module> from FlaskApp import app as application File "/home/bar/FlaskApp/FlaskApp/__init__.py", line 3, in <module> import numpy as np File "/home/bar/FlaskApp/FlaskApp/venv/lib/python3.5/site-packages/numpy/__init__.py", line 142, in <module> from . import add_newdocs File "/home/bar/FlaskApp/FlaskApp/venv/lib/python3.5/site-packages/numpy/add_newdocs.py", line 13, in <module> from numpy.lib import add_newdoc File "/home/bar/FlaskApp/FlaskApp/venv/lib/python3.5/site-packages/numpy/lib/__init__.py", line 8, in <module> from .type_check import * File "/home/bar/FlaskApp/FlaskApp/venv/lib/python3.5/site-packages/numpy/lib/type_check.py", line 11, in <module> import numpy.core.numeric as _nx File "/home/bar/FlaskApp/FlaskApp/venv/lib/python3.5/site-packages/numpy/core/__init__.py", line 24, in <module> raise ImportError(msg) ImportError: Importing the multiarray numpy extension module failed. Most likely you are trying to import a failed build of numpy. If you're working with a numpy git repo, try `git clean -xdf` (removes all files not under version control). Otherwise reinstall numpy. ``` It works without a problem when i try to import numpy in venv in a python session. ``` (venv) bar@bar:~/FlaskApp/FlaskApp$ python3 Python 3.5.2 (default, Nov 17 2016, 17:05:23) [GCC 5.4.0 20160609] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import numpy as np >>> from numpy.core import multiarray >>> multiarray.__file__ '/home/bar/FlaskApp/FlaskApp/venv/lib/python3.5/site-packages/numpy/core/multiarray.cpython-35m-x86_64-linux-gnu.so' >>> ```

Original source