Search code examples
pythonmacospiphomebrewsymlink

Brew symbolic linking for Python is not working. What can I do?


Today, I tried installing Python (3) with the brew package manager. I did it using the normal procedure: running the command brew install python. Installing seemed to go well; I don't get any errors. It finishes with this message:

Python has been installed as /opt/homebrew/bin/python3

Unversioned symlinks python, python-config, pip etc. pointing to python3, python3-config, pip3 etc., respectively, have been installed into /opt/homebrew/opt/[email protected]/libexec/bin

However; when I run: pip3 show pip or pip show pip I get this result:

Name: pip Version: 22.2.2 Summary: The PyPA recommended tool for installing Python packages. Home-page: https://pip.pypa.io/ Author: The pip developers Author-email: [email protected] License: MIT Location: /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages

That location is clearly wrong. So I tried the brew doctor command to see if anything is wrong. It returns me:

Your system is ready to brew.

I searched Stackoveflow for issues related to symbolic links and I noticed you can re-apply symbolic links by running: brew unlink [email protected] && brew link [email protected]. It returns:

Unlinking /opt/homebrew/Cellar/[email protected]/3.10.6_1... 25 symlinks removed. Linking /opt/homebrew/Cellar/[email protected]/3.10.6_1... 25 symlinks created.

Yet running pip3 show pip or pip show pip still yields the same, wrong, result as before. Maybe any of you has had a similar situation before and is willing to explain to me a possible cause and or solution for this problem.


Solution

  • Running the command rehash fixed the issue. I tried it because of this stack overflow post recommending it.

    I wasn't aware of this command existing. According to this source it does this:

    Recompute the internal hash table for the PATH variable. Use rehash whenever a new command is created during the current session. This allows the PATH variable to locate and execute the command. (If the new command resides in a directory not listed in PATH, add this directory to PATH before rehashing.) See also unhash.

    After reading that explanation, it's clearer to me what has happened. The table of symbolic links for my terminal session was not updated yet. Therefore, it was stuck giving me the wrong symlink. The rehash command fixed it.