1

I have the following strange situation on my (freshly installed El Capitan, otherwise empty) Mac.

I noticed that /usr/bin/python defaults to python 2.7 (still), and the new SIP security system "protects" me from replacing it with a symlink to point to python3 (which I installed in /usr/local/bin). I noticed that /usr/local/bin shows up first on the path:

$ echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

So I thought great, I'll just add a symlink in /usr/local/bin called python and point it to python3.

$ ls -al /usr/local/bin
...
lrwxr-xr-x   ... python -> /usr/local/bin/python3
lrwxr-xr-x   ... python3 -> ../Cellar/python3/3.5.0/bin/python3

And this is reflected by which

$ which python
/usr/local/bin/python

But when I actually use it, it goes back to the native python2.7

$ python --version
Python 2.7.10

What gives? Is OS X tinkering with my $PATH and not telling me? What steps can I take to ensure I know what command is actually going to be run?

2

Try closing your terminal window, and making a new one. The shell only actually checks where commands are located on launch, and doesn't check again if it thinks it knows where the command is located. It's a performance boost in scripts, so generally a good idea.

  • Is this specific to OS X? it's not like I changed the path or anything... – JeremyKun Oct 31 '15 at 23:02
  • No. Scripts would take much longer to run if it had to check where each command is located every single time a command is used, so it generally creates a list at runtime, then just reuses that list for any commands processed, unless it doesn't find something in the list, then it checks again. I don't believe there's a way to convince the shell that it's wrong, if it thinks it knows where Python is located, without restarting it. – William T Froggard Oct 31 '15 at 23:04
  • The caching is a property of bash – Mark Dec 22 '15 at 19:05
2

If your shell is bash, then you can use the type and hash builtins to manipulate its cache of executable paths (as shown in How do I clear Bash's cache of paths to executables?):

# to clear the whole cache
hash -r

# to only remove one cached command
hash -d python

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .