Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to restore python on OS X Yosemite after I've deleted something?

I think I previously installed python through homebrew. It was not a good idea but I did:

$ which python
/Library/Frameworks/Python.framework/Versions/2.7/bin/python
$ sudo rm -R /Library/Frameworks/Python.framework/Versions/2.7/bin/python

.. and then the terminal told me...

$ which python
/usr/local/bin/python

But when I run python again

$ python
-bash: /Library/Frameworks/Python.framework/Versions/2.7/bin/python: No such file or directory

So I did this:

export PATH=/usr/local/bin:/usr/local/sbin:$PATH

Now python does run on my terminal but I was wondering if it would be better to put something back where I deleted it, and how I could "restore" however python was before I messed with it?

Thanks!

like image 367
ngc Avatar asked Nov 13 '14 20:11

ngc


1 Answers

You've got a multitude of problems here.

Let's start off with this:

/Library/Frameworks/Python/2.7 is neither the Apple Python nor the Homebrew Python. You apparently installed a third Python, maybe the one from the official python.org binary installers. Removing that one won't affect the Homebrew one.

/usr/local/bin/python is not the Apple Python either. It may be a symlink to your third Python or to the Homebrew Python, but it's not from Apple.

Here's where each Python goes:

  • Apple's Python is in /System/Library/Frameworks/Python/2.7. It also includes various wrapper executables in /usr/bin, including /usr/bin/python, that point at the /System framework. Any extra stuff you install with that Python (e.g., via easy_install or pip) that includes executables or scripts will go into /usr/local/bin, not /usr/bin, but Apple's pre-installed stuff never does.

  • Most third-party binary installers install into /Library/Frameworks/Python/2.7. Different versions can optionally add the framework's bin directory to your path, or symlink the binaries into /usr/local/bin.

  • Homebrew installs to somewhere like /usr/local/Cellar/python/2.7.8, then symlinks various executables and scripts into /usr/local/bin.

So, the fact that you're trying to get back to the Apple Python by making sure /usr/local/bin is on your PATH is already heading in the wrong direction.


Meanwhile, never manually delete something installed by Homebrew unless brew doctor tells you to. Just use brew uninstall python—or, if you want to move it out of the way temporarily, with the option of restoring it later, brew unlink python.


Finally, even after changing your PATH, the shell may have cached the best location to find python, so either read up on the hash command or, if you don't want to learn more about bash, just make sure to open a new shell (e.g., by opening a new tab in Terminal.app).


Anyway, how do you get back to where you were?

You need to cleanly uninstall both extra Pythons. I already explained how to do that with the Homebrew one above. For the third one, you've done most of it, but there are apparently things left behind in /usr/local/bin. If they're all dangling symlinks, as seems most likely, you can find them pretty easily with, e.g., ls -l /usr/local/bin |grep /Library/Frameworks/Python.framework |grep -v /System.

Having done that, just fire up a new shell, and which python should tell you /usr/bin/python, and everything will be happy again.

like image 60
abarnert Avatar answered Oct 24 '22 00:10

abarnert