在Virtualenv Numpy从Flask导入Numpy时会出现多阵列导入错误

时间:2017-05-09 17:38:52

标签: python numpy flask import virtualenv

我正试图在烧瓶__init__.py导入numpy,但它会出现此错误:

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.

当我从烧瓶__init__.py中删除导入时,一切正常。 当我在virtualenv中from numpy.core import multiarray时,一切正常但从wsgi导入它是行不通的。

这是apache / site-available配置文件:

<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

这是wsgi文件:

#!/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'

由于

其他信息: 我没有问题导入其他模块,如熊猫,烧瓶或操作系统。 最初我导入pandas所以错误来自pandas依赖性错误。 来自/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']

当我尝试直接从__init__.py导入numpy时,这是错误日志:

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.

当我尝试在python会话中导入venv中的numpy时,它没有问题。

(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'
>>> 

1 个答案:

答案 0 :(得分:4)

mod_wsgi下的导入可能很棘手,特别是对于Python C扩展库。

此处的问题原来是将WSGIDaemonProcess python-path=选项设置为带有已编译模块的venv。从那些文档:

  

如果使用Python虚拟环境,而不是使用此选项来引用Python虚拟环境的site-packages目录,则应使用python-home选项来指定Python虚拟环境的根目录。

     

在所有情况下,如果目录包含具有C扩展组件的Python包,则必须使用与编译mod_wsgi模块相同的基本Python版本安装这些包。您不应该混合来自不同Python版本或安装的包。

因此,python-home=选项必须用于给定的venv目录,要么将Python 2.7用于venv,要么必须安装mod_wsgi的Python 3版本(在Ubuntu上libapache2-mod-wsgi-py3

对于某些应用程序,WSGIApplicationGroup也需要配置如下:

WSGIApplicationGroup %{GLOBAL}

如果任何C扩展使用简化的线程api。

从类似问题αβ来看,这似乎是一个常见问题。