pythonpython-3.xmacosterminal

Completely uninstall Python 3 on Mac


I installed Python 3 on Mac and installed some packages as well. But then I see AWS lamda does not support Python 3 so I decided to downgrade. I removed Python3 folder in Applications and cleared the trash. But still I see a folder named 3 in /Library/Frameworks/Python.framework/Versions which is causing problems, such as this:

  $ python3 -m pip install virtualenv
 Requirement already satisfied: virtualenv in      /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (20.14.1)
 Requirement already satisfied: platformdirs<3,>=2 in /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages (from virtualenv) (2.5.2) 

So my question is how do I completely uninstall python 3 from my Mac?


Solution

  • ⚠️ WARNING: macOS comes with a system Python installation in /usr/bin/python3 that should NOT be removed as it's required for system functionality. Only remove Python versions you've manually installed.

    Please read the end of 5.1.1 for official insight: https://docs.python.org/3/using/mac.html#installation-steps

    Legacy Python

    The previous edit for this question suggested removing /Library/Frameworks/Python.framework/Versions/2.x/bin/python as a way of completely removing Python. This flexibility has since changed with newer macOS updates.

    Identify Your Python Installations

    Before removing anything, determine which Python installations exist and where they're located:

    # List all Python installations
    which -a python python3
    
    # Check Python versions
    python3 --version
    
    # Find installation locations
    ls -l /opt/homebrew/bin/python*
    ls -l /usr/local/bin/python*
    ls -l /usr/bin/python*
    

    For Homebrew Python

    # Uninstall Python
    brew uninstall python@3.x
    # Remove orphaned files
    brew cleanup
    # List any broken symlinks, etc.
    brew doctor
    

    For Python.org installer

    # Remove the application
    sudo rm -rf /Applications/Python\ 3.x
    
    # Check for framework files
    ls -la /Library/Frameworks/Python.framework/Versions/
    # If found, remove the specific version
    sudo rm -rf /Library/Frameworks/Python.framework/Versions/3.x
    
    # Check for and remove symbolic links
    # For Intel Macs:
    cd /usr/local/bin
    ls -l | grep '../Library/Frameworks/Python.framework/Versions/3.x' | awk '{print $9}' | xargs rm -f
    # For Apple Silicon:
    cd /opt/homebrew/bin
    ls -l | grep '../Library/Frameworks/Python.framework/Versions/3.x' | awk '{print $9}' | xargs rm -f
    
    # Check for user-specific packages
    ls -l ~/Library/Python/
    # Remove if needed
    rm -rf ~/Library/Python/3.x/
    

    For pyenv-managed Python

    # List all installed Python versions
    pyenv versions
    
    # Show the current active version
    pyenv version
    
    # Uninstall a specific version
    pyenv uninstall 3.x.y
    
    # Check pyenv installation location
    ls -la ~/.pyenv/versions/
    
    # Remove pyenv shims and rehash
    pyenv rehash
    
    # If you want to completely remove pyenv itself
    # For Homebrew installation:
    brew uninstall pyenv
    # For manual installation:
    rm -rf ~/.pyenv
    # Don't forget to remove pyenv initialization from your shell profile
    # (.bash_profile, .zshrc, etc.)
    

    Clean Up Remaining Files

    # Remove Python cache files
    find ~/ -name "__pycache__" -type d -exec rm -rf {} +
    find ~/ -name "*.pyc" -delete
    
    # Check for any remaining Python-related directories
    ls -la ~/Library/Application\ Support/ | grep -i python
    ls -la ~/Library/Caches/ | grep -i python
    

    Please