問(wèn)題描述
我已經(jīng)按照這些說(shuō)明在 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/
我已經(jīng)為新 python 的 root 和 myuser 的 bash_profile 添加了別名.現(xiàn)在,當(dāng)我將 python 寫入 shell 時(shí),它可以從兩個(gè)用戶正確運(yùn)行 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 它仍然運(yùn)行舊版本 python2.6.6
However, if I write sudo python it stills runs old version python2.6.6
可能是什么問(wèn)題?
推薦答案
sudo
不使用你的 shell 來(lái)運(yùn)行命令,它只是 exec
直接執(zhí)行命令.這意味著 (a) 沒(méi)有任何源代碼來(lái)自 root 的 bash_profile
,因此放在那里的內(nèi)容無(wú)關(guān)緊要,并且 (b) 即使設(shè)置了 shell 別名也無(wú)關(guān)緊要.
sudo
doesn't use your shell to run commands, it just exec
s 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.
因此,如果您想使用 alias
es 指定與 PATH 中的 Python 不同的 Python,則不能使用 sudo python
來(lái)運(yùn)行相同的 Python一個(gè).
So, if you want to use alias
es to specify a different python than the one that's on your PATH, you can't use sudo python
to run that same one.
最簡(jiǎn)單,也可能是最安全的修復(fù)方法是明確的:運(yùn)行 sudo/path/to/other/python
.如果您需要經(jīng)常這樣做,您可以隨時(shí)為其創(chuàng)建別名.
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.顯式生成運(yùn)行 python
的 bash
命令行,或者(更簡(jiǎn)單地)只使用 -s
或 -i代碼>標(biāo)志.(在這種情況下,如果你試圖讓 root 的
~/.bash_profile
運(yùn)行,-s
不會(huì)這樣做,但 -i
會(huì).)但是 sudo
執(zhí)行 shell 不如 sudo
執(zhí)行程序安全.您的 sudoers
甚至可能被顯式配置為阻止您這樣做.(如果你愿意,你可以用 visudo
來(lái)解決這個(gè)問(wèn)題,但是在不確切了解你正在打開什么的情況下打開一個(gè)安全漏洞通常被認(rèn)為是一件壞事.)
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 sudo
ing shells is not as safe as sudo
ing 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.)
這篇關(guān)于sudo python 運(yùn)行舊的python版本的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!