Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sudo python runs old python version

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/

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.

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

What could be the problem?

like image 685
brsbilgic Avatar asked Mar 15 '13 20:03

brsbilgic


People also ask

How do I switch between versions in Python?

Yes, you should be able to switch between python versions. As a standard, it is recommended to use the python3 command or python3. 7 to select a specific version. The py.exe launcher will automatically select the most recent version of Python you've installed.

What is Sudo Python?

python-sudoModular Python to execute any subprocess commands as another user (not necessarily superuser/root)


1 Answers

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.

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.

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.

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.)

like image 185
abarnert Avatar answered Sep 27 '22 01:09

abarnert