我的问题是当我这样做时:
$ which python => 我把 /a/b/c/python 作为我的目录
但是如果我执行 $ sudo which python => 我会得到 /d/e/python 作为结果
如何更改 sudo 以匹配正常情况,这使得无法从源代码安装库。
我的问题是当我这样做时:
$ which python => 我把 /a/b/c/python 作为我的目录
但是如果我执行 $ sudo which python => 我会得到 /d/e/python 作为结果
如何更改 sudo 以匹配正常情况,这使得无法从源代码安装库。
根据https://askubuntu.com/questions/477987/two-python-distributions-sudo-picking-the-wrong-one ,这是 secure_path(在 /etc/sudoers 中指定)覆盖您的正常 PATH 的结果。
我通过提供我要运行的二进制文件的路径来解决它。例如:
$ which pip
/opt/local/bin/pip
$ sudo /opt/local/bin/pip install foo
这并不理想,但它可以工作并且不会破坏secure_path。
I would first try this:
sudo -i which python
which (indirectly) causes the root
user's profile to be run, including any non-default configuration of the path. (By default, sudo
doesn't bother with that.)
If that doesn't work, then that tells you that /usr/local/bin
isn't in the path set up by the root
user's profile (or isn't before /usr/bin
), so your options are either to change the root
user's profile and use the above, or else to use:
sudo -E which python
to preserve your path (and the rest of your environment). This may be less secure.
For full details on each of these options, see the sudo
man-page.
它使用在 $PATH 中找到的第一个
尝试做
echo $PATH
然后
sudo bash -c 'echo $PATH'
我敢打赌这些是不同的。
在任何情况下,您在 /root 和当前用户中使用的 shell 通常都有某种 rc 脚本,只需将环境变量中的路径重新排列为您想要的路径即可。