問題描述
我已經按照這些說明在 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 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
來運行相同的 Python一個.
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.
最簡單,也可能是最安全的修復方法是明確的:運行 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.顯式生成運行 python
的 bash
命令行,或者(更簡單地)只使用 -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 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.)
這篇關于sudo python 運行舊的python版本的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!