sudo -H 的作用
在用 pip 安装 python 库时候,经常会看到警告"The directory or its parent directory is not owned by the current user and caching wheels has been disabled..........
,然后就是建议使用sudo -H
。那么这个问题的具体原因到底是什么呢?
查阅 sudo 的文档
查阅:https://linux.die.net/man/8/sudo,其中对-H 参数的解释如下:
-H’ The -H (HOME) option requests that the security policy set the HOME environment variable to the home directory of the target user (root by default) as specified by the password database. Depending on the policy, this may be the default behavior.
意思就是加了-H 的参数以后会设置HOME
这个环境变量到 root 的目录(默认情况下),那么我们手动测试一下:
sudo python3
Python 3.6.7 (default, Oct 22 2018, 11:32:17)
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.environ['HOME']
'/home/hacker'
>>> os.environ['USER']
'root'
加了-H 参数之后
sudo -H python3
Python 3.6.7 (default, Oct 22 2018, 11:32:17)
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.environ['HOME']
'/root'
>>> os.environ['USER']
'root'
所以结果就很明显了,确实环境变量里的HOME
被改成了 root 的目录。
查看 pip 源码
那么到底 pip 为什么需要这个变量呢,我查阅了位于 github 上的 pip 的源码,在这里https://github.com/pypa/pip/blob/3a77bd667cc68935040563e1351604c461ce5333/src/pip/_internal/commands/download.py我找到了这段提示信息。
if options.cache_dir and not check_path_owner(options.cache_dir):
logger.warning(
"The directory '%s' or its parent directory is not owned "
"by the current user and caching wheels has been "
"disabled. check the permissions and owner of that "
"directory. If executing pip with sudo, you may want "
"sudo's -H flag.",
options.cache_dir,
)
options.cache_dir = None
根据代码变量的命名,很容易就可以知道这段代码的意思。如果说缓存目录存在,但是不属于当前用户的话,就发出警告,并且不缓存 wheels。