为pip提供NumPy site.cfg参数

2024-05-15 15:01:47 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

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

或者,是否有任何环境变量可以设置而不是在site.cfg文件中提供库路径?这是我使用的site.cfg文件。它几乎是从Intel's site一字不差地取下来的。

[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。


Tags: pip文件numpyincludesite数学cfglibs
3条回答

我最终制作了一个脚本来实现自动化。在这里,以防它能帮助别人。我已经在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/intel64\n'
        'include_dirs = /opt/intel/composer_xe_2013.1.117/mkl/include\n'
        'mkl_libs = mkl_rt\n'
        '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()

安装NumPy以使用英特尔数学内核库的目标现在比安装MKL+NumPy要容易得多,因为Intel created pips

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

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

注意:

If standard NumPy, SciPy and Scikit-Learn packages are already installed, the packages must be uninstalled before installing the Intel® variants of these packages(intel-numpy etc) to avoid any conflicts. As mentioned earlier, pydaal uses intel-numpy, hence it is important to first remove the standard Numpy library (if installed) and then install pydaal.

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

To assist automatic installation like easy_install, the user's home directory will also be checked for the file ~/.numpy-site.cfg .

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

相关问题 更多 >