How to undo messed up Python3 setup

Photo by Cedrik Wesche / Unsplash

I’ve recently got myself a new shiny Apple MacBook Air (M1) and wanted to setup everything I normally use in a proper way from the start. I am a person who uses different programming languages for different purposes and the usual set includes NodeJS, Ruby, Python with/for different tools.

Then, as it usually happens, life got in the way …

Got in a little rush starting on a new project with a new client and messed up my Python setup 🤦‍♂️

The background story

Apple Mac OS has a pre-installed version of Python 🐍 (which is 2.7.16 on Big Sur v11.6 in my case).

Whenever you run $ python --version in the terminal you get the system’s default Python 🐍 version.

I did, for whatever reason, go for installation of Python 🐍 with Homebrew:
$ brew install python
This is the way you would get the latest stable installed (3.9.7 in my case).

There is absolutely nothing wrong with this approach.
What bothers me here, is that process leaves you with TWO snaky-snakes 🐍 on your Mac 🤷‍♂️ with different commands to call each:

  • python for default
  • python3 for the latest 3.x

System’s 🐍:
$ python --version » 2.7.16
and it apparently has no PIP 😧

Brew’d 🐍:
$ python3 --version » 3.9.7
with that you get PIP working as $ pip3 --version

Basically, I'm lazy and juggling with python and python3, remember which is what and fail every other install pip install <whatever> (because it should have been pip3 install <whatever> ffs) is not for me.

Some people may think it could be resolved through aliases! Yes, it could be =)
However, Homebrew manages versions and keeps them separately, therefore, you have to remember to update your alias every time Python 🐍 gets updated.

“Updates, they don’t happen to often!” one may say.
“Yes! ... ”,  the reply would be, “... and that’s exactly the point!”
You are likely to forget after a long time and then put an effort to figure out why your stuff is still running under the previous version, etc 🧐 Again, I'm lazy for this kind of stuff ¯\_(ツ)_/¯

There is a way to make it more convenient to use but I will have to clean up the mess first!

Here goes the link to the next post :)

Clean-up 

undo the hairy Python setup

Current state of things

Let’s see what we’ve got at the moment:
$ brew info python

python@3.9: stable 3.9.7 (bottled)
Interpreted, interactive, object-oriented programming language
https://www.python.org/
/opt/homebrew/Cellar/python@3.9/3.9.7 (3,082 files, 56.6MB) * 👈

...

==> Caveats
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/python@3.9/libexec/bin

You can install Python packages with
  pip3 install <package> 👈
They will install into the site-package directory
  /opt/homebrew/lib/python3.9/site-packages
...

Clean-up

Homebrew

Start with Homebrew’s uninstall
$ brew uninstall python
Uninstalling /opt/homebrew/Cellar/python@3.9/3.9.7... (3,082 files, 56.6MB)

Check brew info again

$ brew info python

python@3.9: stable 3.9.7 (bottled)
Interpreted, interactive, object-oriented programming language
https://www.python.org/
Not installed 👈

...

==> Caveats
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/python@3.9/libexec/bin

You can install Python packages with
  pip3 install <package> 👈
They will install into the site-package directory
  /opt/homebrew/lib/python3.9/site-packages
...

It seems that Python 🐍 has gone but python3 and pip3 are still there.

$ which python3
/usr/local/bin/python3

$ which pip3
/usr/local/bin/pip3

$ python3 -VV
Python 3.9.7 (v3.9.7:1016ef3790, Aug 30 2021, 16:39:15)
[Clang 6.0 (clang-600.0.57)]

$ pip3 -VV
pip 21.2.3 from /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pip (python 3.9)

There are python3 and pip3 symlink that have to be removed.
However, it will be better to start with removing Python 🐍 framework first (/Library/Frameworks/Python.framework/Versions/3.9)

NOTE: rm will require super user permissions
$ sudo rm -rf /Library/Frameworks/Python.framework

Now, it’s time to see what symlinks exist ls -l /usr/local/bin | grep '<HERE_GOES/PATH/TO/PYTHON.FRAMEWORK>'
where <HERE_GOES/PATH/TO/PYTHON.FRAMEWORK> is the PATH PYTHON.FRAMEWORKwhich may be different for you. In my case it was /Library/Frameworks/Python.framework

There are quite a few:

> 2to3 -> ../../../Library/Frameworks/Python.framework/Versions/3.9/bin/2to3
> 2to3-3.9 -> ../../../Library/Frameworks/Python.framework/Versions/3.9/bin/2to3-3.9
> idle3 -> ../../../Library/Frameworks/Python.framework/Versions/3.9/bin/idle3
> idle3.9 -> ../../../Library/Frameworks/Python.framework/Versions/3.9/bin/idle3.9
> pip3 -> ../../../Library/Frameworks/Python.framework/Versions/3.9/bin/pip3
> pip3.9 -> ../../../Library/Frameworks/Python.framework/Versions/3.9/bin/pip3.9
> pydoc3 -> ../../../Library/Frameworks/Python.framework/Versions/3.9/bin/pydoc3
> pydoc3.9 -> ../../../Library/Frameworks/Python.framework/Versions/3.9/bin/pydoc3.9
> python3 -> ../../../Library/Frameworks/Python.framework/Versions/3.9/bin/python3
> python3-config -> ../../../Library/Frameworks/Python.framework/Versions/3.9/bin/python3-config
> python3.9 -> ../../../Library/Frameworks/Python.framework/Versions/3.9/bin/python3.9
> python3.9-config -> ../../../Library/Frameworks/Python.framework/Versions/3.9/bin/python3.9-config

Let’s change directory to /usr/local/bin and remove them (you might need to run 👇 command from under sudo)

ls -l /usr/local/bin | grep '<HERE_GOES/PATH/TO/PYTHON.FRAMEWORK>' | awk '{print $9}' | tr -d @ | xargs rm

where <HERE_GOES/PATH/TO/PYTHON.FRAMEWORK> is the PATH to PYTHON.FRAMEWORK which may be different for you. In my case it was /Library/Frameworks/Python.framework

Let’s see what we have left with:

$ python3 --version
Python 3.8.9

$ pip3 --version
pip 20.2.3 from /Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/site-packages/pip (python 3.8)

Right, everything appears to be in order now

NOTE: Python 3.8 as well as pip3 now belongs to XCode which I’m not going to remove because I wasn’t the one to install it in the first place.

Final word

I’m back to square one now: whatever was installed by Homebrew is gone and I’m ready to do a setup which will alow me to manage Python versions better and use Python3 as a default one!

Take a look at the next post to find out how to setup Python a way you can easily update to the latest version and keep any other vesroins rigth where you can reach them at any given time if the need be.

💡
There is always a way out!
Artem Dvornichenko

Artem Dvornichenko