Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Supplying NumPy site.cfg arguments to pip

I'm using NumPy built against Intel's Math Kernel Library. I use virtualenv, and typically use pip to install packages.

However, in order for NumPy to find the MKL libraries, it's necessary to create a site.cfg file in the NumPy source directory prior to compiling it, then manually build and install. I could script this whole process, but I was hoping for a simpler solution.

I have a standard site.cfg file that can be used for this purpose under version control. Are there any pip command line options that will tell it to copy a particular file to the source directory before building a package?

Alternatively, are there any environment variables that can be set instead of supplying the library paths in a site.cfg file? Here is the site.cfg file that I use. It was taken almost verbatim from 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 = 

For reference, I'm running Ubuntu, Python 2.7, and NumPy 1.6.

like image 918
joshayers Avatar asked Dec 07 '12 19:12

joshayers


People also ask

Does NumPy use OpenBLAS?

NumPy does not require any external linear algebra libraries to be installed. However, if these are available, NumPy's setup script can detect them and use them for building. A number of different LAPACK library setups can be used, including optimized LAPACK libraries such as OpenBLAS or MKL.

Can you pip install NumPy?

NumPy can be installed with conda , with pip , with a package manager on macOS and Linux, or from source.

Does NumPy require C compiler?

You will need a C compiler that complies with the C99 standard. Part of Numpy is now written in C++. You will also need a C++ compiler that complies with the C++11 standard. While a FORTRAN 77 compiler is not necessary for building NumPy, it is needed to run the numpy.

How long does it take to build NumPy?

Building numpy on a Raspberry Pi 3 takes about 20 minutes. Building numpy on a Raspberry Pi 1 takes about 2 hours 30 minutes.


2 Answers

From the source (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 .

Is that a workable solution? You'd still need to preload the home directories with the global .numpy-site.cfg, but you wouldn't have to muck with the build or installation after that.

like image 192
mjk Avatar answered Sep 17 '22 15:09

mjk


I ended up putting together a script to automate this. Here it is, in case it can help someone else. I've tested it in Python 2.7, but it should work elsewhere without significant modifications.

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() 
like image 25
joshayers Avatar answered Sep 21 '22 15:09

joshayers