Search code examples
pythonnumpyscipypipsudo

sudo pip3 install numpy does not respect ~/.numpy-site.cfg


I'm trying to install numpy and scipy on python3 using pip3. I want to use MKL, so I've specified as much in ~/.numpy-site.cfg (as suggested here and here):

[mkl]
library_dirs = /opt/intel/mkl/lib/intel64
include_dirs = /opt/intel/mkl/include
mkl_libs = mkl_rt
lapack_libs =
extra_compile_args = -march=native

I then install numpy (successfully) via

$ sudo pip3 install numpy

However, MKL does not show up in the config!

>>> np.show_config()
...
mkl_info:
  NOT AVAILABLE
...

Installing scipy subsequently fails (as expected) with

numpy.distutils.system_info.NotFoundError: no lapack/blas resources found

However, when I install numpy and scipy in a virtualenv on the same machine without changing anything else, MKL is found, and scipy works fine.

My first guess was that sudo was not picking up $HOME, but sudo echo $HOME returns my home directory correctly.

What could be going wrong?


Solution

  • It turns out that my guess was indeed correct. sudo isn't using the right $HOME. sudo echo $HOME worked because bash expanded $HOME before calling sudo to run the command.

    The following test did the trick though:

    # In test.sh
    echo "$HOME"
    

    And now I get

    $ sudo bash test.sh
    /root
    

    which confirms that $HOME is incorrect. It turns out that a bunch of settings had been set in /etc/sudoers (always_set_home and env_reset), which meant that sudo -E bash test.sh had no effect either.

    I finally just installed it with

    $ sudo HOME=/path/to/my/home pip3 install numpy
    

    which worked.