7

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?

Danny Beckett
  • 20,529
  • 24
  • 107
  • 134
brsbilgic
  • 11,613
  • 16
  • 64
  • 94
  • It's a path problem, see http://stackoverflow.com/questions/257616/sudo-changes-path-why – Fredrik Pihl Mar 15 '13 at 20:25
  • No it's not. He "added aliases… for new python". This means the new python isn't on the PATH in the first place. So it doesn't matter that `sudo` throws away the PATH. – abarnert Mar 15 '13 at 20:28

4 Answers4

16

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

abarnert
  • 354,177
  • 51
  • 601
  • 671
  • a little easier than `sudo /path/to/other/python foo.py` is `sudo \`which python\` foo.py` – anthonybell Apr 04 '15 at 16:35
  • 2
    @anthonybell: That will work if the other python is on your `$PATH`, but not if it's an alias, and that's what the OP was asking about. I suppose you could use `/usr/bin/which` instead of the `bash` builtin; I believe the GNU tools version that comes with CentOS 6 has a `--read-aliases` flag or something like that, so you can do something like `sudo $(alias | which --read-aliases python)`. (That's just off the top of my head; I don't have a machine to test on at the moment.) – abarnert Apr 10 '15 at 22:59
0

I would suggest using sudo -i when calling the script. Straight from man sudo:

-i [command]

The -i (simulate initial login) option runs the shell specified in the passwd(5) entry of the target user as a login shell. This means that login-specific resource files such as .profile or .login will be read by the shell. If a command is specified, it is passed to the shell for execution. Otherwise, an interactive shell is executed. sudo attempts to change to that user's home directory before running the shell. It also initializes the environment, leaving DISPLAY and TERM unchanged, setting HOME , MAIL , SHELL , USER , LOGNAME , and PATH , as well as the contents of /etc/environment on Linux and AIX systems. All other environment variables are removed.

Community
  • 1
  • 1
dmg
  • 7,438
  • 2
  • 24
  • 33
  • 1
    This obviously requires you to have access (in your sudoers) file to run a shell. If you already have/need that access, that's not a problem. But I wouldn't suggest adding it just to make it easier to sudo a particular Python installation… – abarnert Mar 15 '13 at 20:37
0

The problem is that your $PATH is changing upon execution.

You can either use sudo -E:

-E The -E (preserve environment) option will override the env_reset option in sudoers(5)). It is only available when either the matching command has the SETENV tag or the setenv option is set in sudoers(5).

... or you can specify the full path to your executable. sudo `which python`.

(It looks like you used alias per the docs. This method would handle that too.)

Jeff Ferland
  • 17,832
  • 7
  • 46
  • 76
  • The problem is _not_ that his PATH is changing on execution. See the comments to the question, or F.J's deleted answer, or just read the question. And `-E` doesn't help with aliases; you need to `sudo` a shell to get shell aliases (either explicitly, or via `-s` or `-i`). – abarnert Mar 15 '13 at 21:14
  • @abarnert the use of "sudo `which python`" does handle it, though. – Jeff Ferland Mar 15 '13 at 21:16
0

The bin directory is probably referencing the old version of Python

> ls -la /usr/bin/python
lrwxrwxrwx 1 root root 9 Dec 21  2013 /usr/bin/python -> python2.7

Redirect the default python version to the one you want.

ppostma1
  • 3,616
  • 1
  • 27
  • 28