3

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'
>>> 
gz.
  • 6,661
  • 1
  • 23
  • 34
Barslmn
  • 83
  • 7

1 Answers1

4

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.

Community
  • 1
  • 1
gz.
  • 6,661
  • 1
  • 23
  • 34
  • Still same error but I understand the problem better. Thanks – Barslmn May 09 '17 at 18:12
  • So I need pyton2.7 modules in my virtualenv. I will let you know. Thanks – Barslmn May 09 '17 at 18:42
  • 2
    I created virtualenv with python 2.7 and installed the modules and change the config file's path to it. Everthing works. Thanks a lot. I will import some print functions now. – Barslmn May 09 '17 at 18:59
  • Great, glad it works, I'll edit my answer to be a bit clearer given that. :) – gz. May 09 '17 at 19:00
  • Any idea how to get this to work using python3? I'm having the same issue, but don't want to revert to python2.7. I have libapache2-mod-wsgi-py3 installed, but still get the same error. – Brandon Sep 10 '18 at 16:21
  • @Brandon Are you sure your venv is using the same Python 3 version as the apache extension was compiled with? For instance, on xenial the distro package uses 3.5 but you may well have a 3.6 virtual env. – gz. Sep 11 '18 at 00:28