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

前端 未结 1 1331
臣服心动
臣服心动 2021-01-30 18:48

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/py         


        
相关标签:
1条回答
  • 2021-01-30 19:27

    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.

    0 讨论(0)
提交回复
热议问题