Skip to content Skip to sidebar Skip to footer

Osx: Using Bash To Find Python Is Different Than Using /usr/bin/env Python

I don't know what to make of this. If I ask my shell (bash) to launch python without giving it an exact path, it somehow launches the wrong program (or possibly the right program,

Solution 1:

Bash keeps a cached lookup table in memory, which you can examine with hash. If you add a program to your PATH with the same name as a program you have used before at a different location, bash will still remember the old location. env obviously bypasses this cache; or you can remove it with hash -r python.

Also note that which is nonportable and often not the correct tool to use; type is specified by POSIX, and is built-in to any modern shell.

Solution 2:

When you run program from $PATH ( so, like $ python ) then it's argv[0] is set to just it's name ( eg. python ), not full path. And python 2.7 version of sys.executablelooks at $PATH if argv[0] doesn't have backslash on beggining ( so, eg. when you run it like in last example ). And it seems that it's not resolving symlinks, just printing first found path.

As we can see in python's code, on OS X it uses NSGetExecutablePath to itself in $PATH. And on Apple's site they say that NSGetExecutablePath

That is, the path may be a symbolic link and not the real file.

Post a Comment for "Osx: Using Bash To Find Python Is Different Than Using /usr/bin/env Python"