向pip提供NumPy site.cfg参数



我使用的是针对英特尔数学内核库构建的NumPy。我使用virtualenv,通常使用pip来安装软件包。

然而,为了让NumPy找到MKL库,有必要在编译之前在NumPy源目录中创建一个site.cfg文件,然后手动构建和安装。我可以编写整个过程的脚本,但我希望有一个更简单的解决方案。

我有一个标准的site.cfg文件,可以在版本控制下用于此目的。有没有任何pip命令行选项可以告诉它在构建包之前将特定文件复制到源目录?

或者,是否有任何环境变量可以设置,而不是在site.cfg文件中提供库路径?这是我使用的site.cfg文件。它几乎是逐字逐句地从英特尔网站上截取的。

[mkl]
library_dirs = /opt/intel/composer_xe_2013.1.117/mkl/lib/intel64
include_dirs = /opt/intel/composer_xe_2013.1.117/mkl/include
mkl_libs = mkl_rt
lapack_libs =

作为参考,我运行的是Ubuntu、Python 2.7和NumPy 1.6。

来源(https://github.com/numpy/numpy/blob/master/site.cfg.example):

为了帮助像easy_install这样的自动安装,用户的主目录还将检查文件~/.numpy-site.cfg.

这是可行的解决方案吗?您仍然需要使用global.numpy-site.cfg预加载主目录,但之后就不必再进行构建或安装了。

我最终编写了一个脚本来实现自动化。它在这里,以防它能帮助其他人。我已经在Python2.7中测试过它,但它应该可以在其他地方工作,而无需进行重大修改。

from __future__ import unicode_literals
import io
import os.path
import re
import subprocess
import urllib2
# This downloads, builds, and installs NumPy against the MKL in the
# currently active virtualenv
file_name = 'numpy-1.6.2.tar.gz'
url = ('http://sourceforge.net/projects/numpy/files/NumPy/1.6.2/'
'numpy-1.6.2.tar.gz/download')
def main():
# download NumPy and unpack it
file_data = urllib2.urlopen(url).read()
with io.open(file_name, 'wb') as fobj:
fobj.write(file_data)
subprocess.check_call('tar -xvf {0}'.format(file_name), shell=True)
base_name = re.search(r'(.*).tar.gz$', file_name).group(1)
os.chdir(base_name)
# write out a site.cfg file in the build directory
site_cfg = (
'[mkl]n'
'library_dirs = /opt/intel/composer_xe_2013.1.117/mkl/lib/intel64n'
'include_dirs = /opt/intel/composer_xe_2013.1.117/mkl/includen'
'mkl_libs = mkl_rtn'
'lapack_libs =n')
with io.open('site.cfg', 'wt', encoding='UTF-8') as fobj:
fobj.write(site_cfg)
# build and install NumPy
subprocess.check_call('python setup.py build', shell=True)
subprocess.check_call('python setup.py install', shell=True)

if __name__ == '__main__':
main()

自从英特尔创建了安装MKL+NumPy:的pip后,安装NumPy以使用英特尔数学内核库的目标现在变得更加容易了

pip uninstall numpy -y  # if the standard numpy is present
pip install intel-numpy

以及intel-scipyintel-scikit-learnpydaaltbb4pymkl_fftmkl_random和较低级别的软件包(如果您只需要它们的话)。同样,如果标准软件包已经安装在您的virtualenv中,则必须首先卸载它们。

注意:

如果已安装标准NumPy、SciPy和Scikit Learn软件包,则必须先卸载这些软件包,然后再安装这些软件包的英特尔®变体(英特尔NumPy等),以避免任何冲突。如前所述,pydaal使用intel numpy,因此首先删除标准numpy库(如果已安装),然后安装pydaal非常重要。

或者,是否有任何环境变量可以设置,而不是在site.cfg文件中提供库路径?

NumPy1.21为此引入了环境变量。

例如

NPY_BLAS_ORDER=MKL NPY_LAPACK_ORDER=MKL pip install numpy --no-binary numpy

从源代码安装NumPy时自动检测MKL库。如果需要,您可以将环境变量NPY_BLAS_LIBSNPY_CBLAS_LIBSNPY_LAPACK_LIBS设置为链接器CLI选项,这些选项将您选择的库置于链接器路径上。

对于脚本来说,这比创建~/.numpy-site.cfg文件更容易

[openblas]
libraries = openblas
library_dirs = /usr/local/opt/openblas/lib
include_dirs = /usr/local/opt/openblas/include
runtime_library_dirs = /usr/local/opt/openblas/lib

然后运行

pip install numpy --no-binary numpy

顺便说一句,当从源代码安装scipy时,文件~/.numpy-site.cfg也可以工作

pip install scipy --no-binary scipy

注意:如果您仍在使用Python 2.7,请安装numpy,然后安装scipy。尝试将它们安装在一起将:

  • 调用请求NumPy的SciPy easy_install安装程序
  • 加载最新的NumPy安装程序(即使您特别要求pip使用install numpy==1.14.6 scipy==1.0.1 --no-binary numpy,scipy),然后
  • RuntimeError: Python version >= 3.5 required失败,因为最新的NumPy不支持Python 2.7

最新更新