"Right" (or best) way to install Python in macOS (10.15 Catalina)

Hi. New to the forum and Python. I want to install Python in macOS 10.15 Catalina.

A Google search shows there are some strong opinions about the “right” and “wrong” way to install for macOS. One solution is to install Python via the package manager Homebrew so the Python is always kept current. The bottom line is it seems you can really get off to a bad start if you do not get it installed the right way from the beginning.

Any suggestions? Maybe you made a mistake when you first started out with Python with the install and can share some of your learned wisdom?

Hoping someone can help :smile:

Hi. New to the forum and Python. I want to install Python in macOS
10.15 Catalina.

A Google search shows there are some strong opinions about the “right” and “wrong” way to install for macOS. One solution is to install Python via the package manager Homebrew so the Python is always kept current. The bottom line is it seems you can really get off to a bad start if you do not get it installed the right way from the beginning.

Any suggestions? Maybe you made a mistake when you first started out
with Python with the install and can share some of your learned wisdom?

Go not to the elves for counsel, for they will say both no and yes.

  • Frodo, The Fellowship of the Ring

There isn’t a “correct” way to do this. Here are some choices, and
reasons why you might choose them. Also, you don’t have to pick just
one. What you want is some control over which Python3 you your
programmes run.

Here’s my Catalina Mac:

[~]fleet2*> ws python3
/Users/cameron/bin-cs/python3
/Users/cameron/var/venv/3/bin/python3
/usr/local/bin/python3
/usr/bin/python3

I have 4 presently installed ways to invoke Python 3. When I type
“python3” it finds the first one.

/Users/cameron/bin-cs/python3
This is just a tiny shell script which runs the python3 from my
default virtualenv. But I can hack this script if I ever want to
adjust that.

/Users/cameron/var/venv/3/bin/python3
This is my default Python 3 virtualenv, which is a little environment
with a set of third party modules I use a lot. The actualy Python 3
install it lives off is, at present, the one in usr/local/bin, below.

/usr/local/bin/python3
This is Python 3 from HomeBrew.

/usr/bin/python3
This is Python 3 which comes from the OS. Let’s run that one:

[~]fleet2*> /usr/bin/python3
Python 3.8.2 (default, Oct  2 2020, 10:45:42)
[Clang 12.0.0 (clang-1200.0.32.27)] on darwin
Type "help", "copyright", "credits" or "license" for more 
information.
>>>

So, that’s 3.8.2, which is pretty current. Unless you need something
very very recent, this one will be just fine for your needs - no need
for HomeBrew or anything else.

What about third party modules? These mostly come from PyPI, and are
installed using the “pip3” command. If you want to be sure of which
Python is fetching and installing then, invoke pip this way:

/usr/bin/python3 -m pip ...pip-command-here...

Adjust “/usr/bin/python3” to your choice - but start with that one - the
OS suplied one - its pretty current and already there for you. That
ensures you’re running pip for the particular Python3 you desire, just
in case the “pip3” command is itself hooked to a different Python.

Note: DO NOT run pip as root. Run as yourself. This avoids any risk of
having it install modules over whatever shipped with the system. That
is a bad thing.

My initial recommendation is: start with the OS python 3, and make
yourself a personal virtual environment:

/usr/bin/python3 -m venv $HOME/venv

That will make a little environment which uses the system python3 as its
basis, into which you can install extra modules by invoking the
“python3” from the environment, for example:

$HOME/venv/bin/python3 -m pip install -U pip wheel

which will install, in the virtual environment, shiny modern versions
of the “pip” and “wheel” modules. Then you can install more stuff as
needed.

Modify your $PATH to put $HOME/venv/bin near the front, maybe just after
$HOME/bin (for your own scripts). Something like this in your .profile:

PATH=$HOME/bin:$HOME/venv/bin:$PATH
export PATH

would set that up for new login shells (which most new terminal windows
are).

Then just typing “python3” will use your virtualenv one.

Also, putting this:

#!/usr/bin/env python3

at the top of your python scripts causes then to run whatever the
default “python3” command in, according to your $PATH above. Then they
all just track how your world is set up.

You can also install from HomeBrew as well if there’s some need (eg for
a newer Python for some new bugfix or feature). Or you could install
from source. Make sure not to install that over the system or homebrew
ones - build and install as yourself, and give “configure” a --prefix
option which specifies a nice distinct directory for the install.

Finally, if you want to experiment with different Pythons: you can make
as many virtualenvs as you like, each hooked to whatever “original”
Python you desire. Just invoke the “python3” executable from within a
particular virtualenv to make use of that virtualenv.

Cheers,
Cameron Simpson cs@cskk.id.au

1 Like

LOL… Oh wow! Thanks for the great advice and for taking the time to put together such a thorough response. Greatly appreciated!

hi, thanks for sharing helpful for me