macOS Big Sur; Command Line Tools and Python 3.8.2

Viewed 2723

I am experiencing difficulty with installing Python correctly on my Mac (Fresh Install, I wiped my PC clean). I installed firstly

  1. Home Brew
  2. Xcode through App Store
  3. Command line tools using command: "xcode-select --install"

On typing the following command: which python3

Output is as follows:

gaurangsmacbookpro@Gaurangs-New-MacBook-Pro ~ % which python

/usr/bin/python

gaurangsmacbookpro@Gaurangs-New-MacBook-Pro ~ % which python3

/usr/bin/python3

upon running the following command which I was following off a blog from this site: https://www.pyimagesearch.com/2016/12/05/macos-install-opencv-3-and-python-3-5/

brew install eigen tbb

Home brew installed python 3.9 dependencies so no when I type the following commands:

brew list python3

I get the following output:

gaurangsmacbookpro@Gaurangs-New-MacBook-Pro ~ % brew list python3
/usr/local/Cellar/python@3.9/3.9.1_8/bin/2to3
/usr/local/Cellar/python@3.9/3.9.1_8/bin/2to3-3.9
/usr/local/Cellar/python@3.9/3.9.1_8/bin/easy_install-3.9
/usr/local/Cellar/python@3.9/3.9.1_8/bin/idle3
/usr/local/Cellar/python@3.9/3.9.1_8/bin/idle3.9
/usr/local/Cellar/python@3.9/3.9.1_8/bin/pip3
/usr/local/Cellar/python@3.9/3.9.1_8/bin/pip3.9
/usr/local/Cellar/python@3.9/3.9.1_8/bin/pydoc3
/usr/local/Cellar/python@3.9/3.9.1_8/bin/pydoc3.9
/usr/local/Cellar/python@3.9/3.9.1_8/bin/python3
/usr/local/Cellar/python@3.9/3.9.1_8/bin/python3-config
/usr/local/Cellar/python@3.9/3.9.1_8/bin/python3.9
/usr/local/Cellar/python@3.9/3.9.1_8/bin/python3.9-config
/usr/local/Cellar/python@3.9/3.9.1_8/bin/wheel3
/usr/local/Cellar/python@3.9/3.9.1_8/Frameworks/Python.framework/ (3019 files)
/usr/local/Cellar/python@3.9/3.9.1_8/IDLE 3.app/Contents/ (8 files)
/usr/local/Cellar/python@3.9/3.9.1_8/lib/pkgconfig/ (4 files)
/usr/local/Cellar/python@3.9/3.9.1_8/libexec/bin/ (7 files)
/usr/local/Cellar/python@3.9/3.9.1_8/libexec/pip/ (480 files)
/usr/local/Cellar/python@3.9/3.9.1_8/libexec/setuptools/ (334 files)
/usr/local/Cellar/python@3.9/3.9.1_8/libexec/wheel/ (44 files)
/usr/local/Cellar/python@3.9/3.9.1_8/Python Launcher 3.app/Contents/ (16 files)
/usr/local/Cellar/python@3.9/3.9.1_8/share/man/ (2 files)

The contents of my ~./bash_profile file are as follows:

# Add Homebrew's executable directory to the front of the PATH

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

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

export PATH="/usr/local/opt/python@3.8/bin:$PATH"

What am I doing wrong? I just want a fresh clean install of Python 3.8 not 3.9 as there is no support for TF Lite on 3.9 yet. Can someone help me fix this mess? I will be very grateful!

1 Answers

I'm a bit late at the game, but I hope this will be useful to somebody. The best solution here is to use a virtual environment, but for the sake of argument and because it is a little bit more involved, I won't discuss that as an option here and will assume you just want a specific version installed on your machine where you can just add all the dependencies you need.

From looking at the page OP was using, the easiest solution here is probably to use a distribution like Anaconda if you are particularly interested in OpenCV or that type of libraries. Then it's just a case of setting up your path like export PATH=$HOME/anaconda/bin:$PATH or whatever path you used to install Anaconda with your shell of choice.

But let's imagine that Anaconda doesn't help you and you still need a specific version of Python, and you figure 3.8.2 is good enough for your needs and a newer version wouldn't work. Using the Python version that comes with the Xcode command line tools is probably a bad idea, just like using the system installed Python is. That version is really meant to support the Apple toolchain and while I've never run into any issues with it, I would not recommend it.

Next option is is to use homebrew. Great option since most people will already be using it, and you might already have python installed as part of a dependency but maybe like OP it's not the version you were hoping for. You can still make it work by installing an older version like so brew install python@3.8 however, it comes with a few caveats. Whenever one of the python dependencies is updated, you might need to unlink and relink it so that you can continue to use the correct version. Believe me, it's no way to live.

What I've found works (in combination with virtual environments is to use pyenv. Install it through homebrew, set your shell as described in the install doc and use it to install the python version you want (very easy) and then set the global interpreter to be whatever version you want and your system will stick to it until you set it to something else. It also works great in combination with tools like pipenv which allow you to manage virtual environments.

Related