Python SciPy 是否需要 BLAS?

184 投票
7 回答
128233 浏览
提问于 2025-04-17 02:44
numpy.distutils.system_info.BlasNotFoundError: 
    Blas (http://www.netlib.org/blas/) libraries not found.
    Directories to search for the libraries can be specified in the
    numpy/distutils/site.cfg file (section [blas]) or by setting
    the BLAS environment variable.

我需要从这个网站下载哪个tar文件呢?

我试过下载fortran的版本,但每次都出现这个错误(当然,我已经设置了环境变量)。

7 个回答

66

在Fedora系统上,这样做是有效的:

 yum install lapack lapack-devel blas blas-devel
 pip install numpy
 pip install scipy

记得除了安装 'blas' 和 'lapack' 之外,还要安装 'lapack-devel' 和 'blas-devel',否则你会遇到你提到的错误,或者会出现 "numpy.distutils.system_info.LapackNotFoundError" 的错误。

340

如果你想用最新版本的SciPy,而不是系统自带的版本,但又不想费劲去安装BLAS和LAPACK这两个库,你可以按照下面的步骤来操作。

首先,从软件库里安装线性代数相关的库(适用于Ubuntu系统),

sudo apt-get install gfortran libopenblas-dev liblapack-dev

然后安装SciPy(在下载了SciPy的源代码之后):python setup.py install 或者

pip install scipy

根据具体情况来选择。

143

SciPy官网以前提供了构建和安装的说明,但现在的说明主要依赖于操作系统的二进制发行版。如果你想在没有预编译库的操作系统上构建SciPy(和NumPy),你需要先构建并静态链接Fortran库,分别是BLASLAPACK

mkdir -p ~/src/
cd ~/src/
wget http://www.netlib.org/blas/blas.tgz
tar xzf blas.tgz
cd BLAS-*

## NOTE: The selected Fortran compiler must be consistent for BLAS, LAPACK, NumPy, and SciPy.
## For GNU compiler on 32-bit systems:
#g77 -O2 -fno-second-underscore -c *.f                     # with g77
#gfortran -O2 -std=legacy -fno-second-underscore -c *.f    # with gfortran
## OR for GNU compiler on 64-bit systems:
#g77 -O3 -m64 -fno-second-underscore -fPIC -c *.f                     # with g77
gfortran -O3 -std=legacy -m64 -fno-second-underscore -fPIC -c *.f    # with gfortran
## OR for Intel compiler:
#ifort -FI -w90 -w95 -cm -O3 -unroll -c *.f

# Continue below irrespective of compiler:
ar r libfblas.a *.o
ranlib libfblas.a
rm -rf *.o
export BLAS=~/src/BLAS-*/libfblas.a

在五个g77/gfortran/ifort命令中只执行其中一个。我把其他的都注释掉了,只保留了我使用的gfortran。接下来的LAPACK安装需要一个Fortran 90编译器,而且两个安装都应该使用同一个Fortran编译器,所以不应该用g77来处理BLAS。

接下来,你需要安装LAPACK的相关内容。SciPy官网的说明对我也有帮助,但我需要根据我的环境进行一些修改:

mkdir -p ~/src
cd ~/src/
wget http://www.netlib.org/lapack/lapack.tgz
tar xzf lapack.tgz
cd lapack-*/
cp INSTALL/make.inc.gfortran make.inc          # On Linux with lapack-3.2.1 or newer
make lapacklib
make clean
export LAPACK=~/src/lapack-*/liblapack.a

更新于2015年9月3日: 今天验证了一些评论(感谢大家):在运行make lapacklib之前,先编辑make.inc文件,并在OPTSNOOPT设置中添加-fPIC选项。如果你使用的是64位架构,或者想为64位编译,也要添加-m64。确保BLAS和LAPACK都用相同的选项编译是很重要的。如果你忘记了-fPIC,SciPy会给你一个关于缺少符号的错误,并建议你使用这个选项。我的make.inc文件的相关部分看起来是这样的:

FORTRAN  = gfortran 
OPTS     = -O2 -frecursive -fPIC -m64
DRVOPTS  = $(OPTS)
NOOPT    = -O0 -frecursive -fPIC -m64
LOADER   = gfortran

在老旧的机器上(比如RedHat 5),gfortran可能是旧版本(比如4.1.2),不支持-frecursive选项。在这种情况下,直接把它从make.inc文件中删除即可。

在我的设置中,Makefile的lapack测试目标失败了,因为找不到blas库。如果你仔细的话,可以暂时把blas库移动到指定位置来测试lapack。我比较懒,所以我相信开发者已经做好了这个工作,只在SciPy中验证一下。

撰写回答