sudo python 运行旧的python版本

时间:2023-04-16
本文介绍了sudo python 运行旧的python版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我已经按照这些说明在 CentOS 6 上安装了 python 2.7.3

I have installed python 2.7.3 on CentOS 6 with thse instructions

http://villaroad.com/2010/10/rolling-python-2-6-2-on-centos-5-3/

我已经为新 python 的 root 和 myuser 的 bash_profile 添加了别名.现在,当我将 python 写入 shell 时,它可以从两个用户正确运行 python2.7.3.

I have added aliases to bash_profile of both root and myuser for new python. Now when I write python to shell, it runs python2.7.3 correctly from both users.

但是,如果我写 sudo python 它仍然运行旧版本 python2.6.6

However, if I write sudo python it stills runs old version python2.6.6

可能是什么问题?

推荐答案

sudo 不使用你的 shell 来运行命令,它只是 exec 直接执行命令.这意味着 (a) 没有任何源代码来自 root 的 bash_profile,因此放在那里的内容无关紧要,并且 (b) 即使设置了 shell 别名也无关紧要.

sudo doesn't use your shell to run commands, it just execs the command directly. This means (a) there's nothing that sources root's bash_profile, so it doesn't matter what you put there, and (b) shell aliases wouldn't matter even if they were set.

因此,如果您想使用 aliases 指定与 PATH 中的 Python 不同的 Python,则不能使用 sudo python 来运行相同的 Python一个.

So, if you want to use aliases to specify a different python than the one that's on your PATH, you can't use sudo python to run that same one.

最简单,也可能是最安全的修复方法是明确的:运行 sudo/path/to/other/python.如果您需要经常这样做,您可以随时为其创建别名.

The easiest, and probably safest, fix is to be explicit: run sudo /path/to/other/python. If you need to do this often enough, you can always create an alias for that.

如果你真的想要,可以sudo 使用 shell.显式生成运行 pythonbash 命令行,或者(更简单地)只使用 -s-i标志.(在这种情况下,如果你试图让 root 的 ~/.bash_profile 运行,-s 不会这样做,但 -i 会.)但是 sudo 执行 shell 不如 sudo 执行程序安全.您的 sudoers 甚至可能被显式配置为阻止您这样做.(如果你愿意,你可以用 visudo 来解决这个问题,但是在不确切了解你正在打开什么的情况下打开一个安全漏洞通常被认为是一件坏事.)

If you really want to, you can make sudo use a shell. Either explicitly generate the bash command line that runs python, or (more simply) just use the -s or -i flags. (In this case, if you're trying to get root's ~/.bash_profile run, -s won't do it, but -i will.) But sudoing shells is not as safe as sudoing programs. Your sudoers may even be explicitly configured to prevent you from doing it. (You can fix that with visudo if you want, but opening a security hole without understanding exactly what you're opening is generally considered a bad thing to do.)

这篇关于sudo python 运行旧的python版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!