LinuxQuestions.org

LinuxQuestions.org (http://www.linuxquestions.org/questions/index.php)
-   Linux - General (http://www.linuxquestions.org/questions/forumdisplay.php?f=1)
-   -   Two executables with same name in $PATH: How do I force the right one to be used? (http://www.linuxquestions.org/questions/showthread.php?t=697224)

prasadbrg 01-14-2009 06:57 AM

Two executables with same name in $PATH: How do I force the right one to be used?
 
Hello everyone,
I just installed Python 2.6 on a system where I don't have root access. The system previously had Python 2.2 installed on it. While compiling from source, I used the
Code:

--prefix=$HOME/usr
option. So now I have a 'python' executable in $HOME/usr/bin, which runs Python 2.6. I added an
Code:

export PATH=$PATH:$HOME/usr/bin
line to my $HOME/.bashrc file and did 'source .bashrc'. Now 'echo $PATH' shows '$HOME/usr/bin' at the end. Still, the 'python' command brings up the old Python 2.6:
Code:

$which python
/usr/bin/python

How do I force the 'python' command to point to the Python 2.6 executable in '$HOME/usr/bin'?

Thanks in advance!
Cheers,
Guru

pixellany 01-14-2009 07:01 AM

You can simply use the full pathname.....

pwc101 01-14-2009 07:05 AM

Change the export line:
Code:

export PATH=$HOME/usr/bin:$PATH
so it sees your $HOME/usr/bin before the rest of the path.

edit: There is a possibility this will break some things.

colucix 01-14-2009 07:06 AM

The rule is: if two executables have the same name, the first one encountered through the path is executed. So you can just invert the assignment:
Code:

export PATH=$HOME/usr/bin:$PATH
or - better - create an alias to the executable specifying its absolute path (as suggested by pixellany). For example
Code:

alias my_phyton='$HOME/usr/bin/python'
Edit: pwc101 beat me! ;)

pwc101 01-14-2009 07:17 AM

Quote:

Originally Posted by colucix (Post 3408231)
Edit: pwc101 beat me! ;)

First time for everything, colucix! ;)

prasadbrg 01-14-2009 07:35 AM

Works!
 
Thanks, everyone, for the prompt replies!
pixellany, using the full path is what I've been doing so far... I was worried that some script or program that I might use would call 'python' and it would use the wrong one... for the same reason, did not use the alias (in fact, there is also a 'python2.6' executable in $HOME/usr/bin, which works fine).
I did not know about the path order rule. I inverted the assignment as suggested, and 'which python' now reveals the right one.
Thanks once again, everyone!
Cheers,

Guru

P.S. It's nice to have people racing to solve your problems - this is truly a wonderful forum!

GazL 01-14-2009 07:55 AM

Quote:

Originally Posted by prasadbrg (Post 3408260)
(in fact, there is also a 'python2.6' executable in $HOME/usr/bin, which works fine).

You'll probably find that python is a symbolic link to python2.6. At least that's how it's installed on my slackware box.

Code:

bash-3.1$ ls -l /usr/bin/python*
lrwxrwxrwx 1 root root    9 2008-12-05 12:26 /usr/bin/python -> python2.5
lrwxrwxrwx 1 root root  16 2008-12-05 12:26 /usr/bin/python-config -> python2.5-config
-rwxr-xr-x 1 root root 3320 2008-09-11 19:44 /usr/bin/python2.5
-rwxr-xr-x 1 root root 1418 2008-09-11 19:43 /usr/bin/python2.5-config


sundialsvcs 01-14-2009 08:21 AM

You can even go one step further...

PATH=my/path/this/time python blah

This is a one-time declaration of the symbol "PATH," known only to this instance of (say...) Python and any of its descendents. It supersedes any exported version of that same symbol, and only in this very-specific context.

prasadbrg 01-14-2009 10:33 AM

Quote:

Originally Posted by GazL (Post 3408280)
You'll probably find that python is a symbolic link to python2.6. At least that's how it's installed on my slackware box.

Gazl, that's what I thought too, at first. I was surprised to see this:
Code:

$ls-l  $HOME/usr/bin/
total 7184
-rwxr-xr-x  1 user    125 2009-01-12 19:27 2to3
-rwxr-xr-x  1 user    672 2009-01-13 23:19 f2py
-rwxr-xr-x  1 user    675 2009-01-12 22:14 f2py2.6
-rwxr-xr-x  1 user    113 2009-01-12 19:27 idle
-rwxr-xr-x  1 user      69 2009-01-13 23:27 nosetests
-rwxr-xr-x  1 user      98 2009-01-12 19:27 pydoc
-rwxr-xr-x  2 user 3651483 2009-01-12 19:41 python
-rwxr-xr-x  2 user 3651483 2009-01-12 19:41 python2.6
-rwxr-xr-x  1 user    1438 2009-01-12 19:42 python2.6-config
lrwxrwxrwx  1 user      16 2009-01-12 19:42 python-config -> python2.6-config
-rwxr-xr-x  1 user  18068 2009-01-12 19:27 smtpd.py

sundialsvcs, my apprehension was more about calls to python from scripts, or other programs that I intend to use, which I haven't written. To be honest, I don't have any solid grounds for this apprehension - while I intend to use code that makes calls to python, I'm not sure whether such calls would go through the executable itself (I'm a newbie to these things!). On the other hand, executable python scripts do have this line at the beginning: '#!/usr/bin/env python'. I just wanted to be doubly sure, that all subsequent programs that I run as user, as a rule use the new Python 2.6 executable.
Cheers,
Guru

GazL 01-14-2009 11:32 AM

Code:

-rwxr-xr-x  2 user 3651483 2009-01-12 19:41 python
-rwxr-xr-x  2 user 3651483 2009-01-12 19:41 python2.6

Ahhhh, what you seem to have there is a hardlink. Note the '2'.
(I guess whoever packaged it or did the make file forgot the '-s' on the ln command). I don't know if this is the norm with python installs or not.

To confirm that is what has happened you can do a ls -i on both and see if it returns the same inode value, but I'm fairly sure that's it.

Interestingly, the python-config command uses symbolics as expected.

colucix 01-14-2009 12:04 PM

On a machine running CentOS 5.2 I have
Code:

$ ls -il /usr/bin/python*
487364 -rwxr-xr-x 2 root root 5632 May 24  2008 /usr/bin/python
487220 lrwxrwxrwx 1 root root    6 Oct  7 17:40 /usr/bin/python2 -> python
487364 -rwxr-xr-x 2 root root 5632 May 24  2008 /usr/bin/python2.4

this confirms the hard link issue. All the three items were installed by the python RPM. Idem in Fedora. On the contrary, on a OpenSuse system the situation is similar to that reported by GazL for Slackware.

pwc101 01-14-2009 12:09 PM

Seemingly Slackware has only symlinks (softlinks):
Code:

pwc101@linux:~> ls -il /usr/bin/python*
98765 lrwxrwxrwx 1 root root    9 2009-01-05 20:24 /usr/bin/python -> python2.5
98762 lrwxrwxrwx 1 root root  16 2009-01-05 20:24 /usr/bin/python-config -> python2.5-config
98759 -rwxr-xr-x 1 root root 3320 2008-09-11 19:44 /usr/bin/python2.5
98761 -rwxr-xr-x 1 root root 1418 2008-09-11 19:43 /usr/bin/python2.5-config



All times are GMT -5. The time now is 12:05 AM.