sudo pip3 install numpy 不尊重 ~/.numpy-site.cfg

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

我正在尝试使用 pip3 在 python3 上安装 numpy 和 scipy。我想使用 MKL,所以我在 ~/.numpy-site.cfg 中指定了尽可能多的内容(按照 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

然后我通过

安装 numpy(成功)
$ sudo pip3 install numpy

但是,MKL 没有出现在配置中!

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

安装 scipy 随后失败(正如预期的那样)

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

但是,当我在同一台机器上的 virtualenv 中安装 numpy 和 scipy 而不更改任何其他内容时,找到了 MKL ,并且 scipy 有效很好。

我的第一个猜测是 sudo 没有选择 $HOME,但是 sudo echo $HOME returns 我的主目录是正确的。

可能出了什么问题?

事实证明,我的猜测确实是正确的。 sudo没有使用右边$HOMEsudo echo $HOME 起作用是因为 bash 在调用 sudo 到 运行 命令之前扩展了 $HOME

下面的测试成功了:

# In test.sh
echo "$HOME"

现在我得到了

$ sudo bash test.sh
/root

这证实了 $HOME 是不正确的。原来是/etc/sudoers设置了一堆设置(always_set_home和env_reset),也就是说sudo -E bash test.sh也没有效果。

我终于用

安装了它
$ sudo HOME=/path/to/my/home pip3 install numpy

有效。