Python on Mac

I started this piece in March 2018, with a MacBook Pro running macOS High Sierra 10.13.3, where no previous Python work had been done. By default Apple ship Python 2.7.10 in macOS, however it does not have the Python module installer, pip installed and is not really suitable for development work. However it is recommended you leave it alone, it is Apple controlled and used by Apple and possibly other software. At this stage it is probably worth reading 4. Using Python on a Macintosh — Python 2.7.14 documentation before you go much further.

Whilst you can download newer versions of Python direct from Python Releases for Mac OS X | Python.org it is much easier to use brew. I needed two extra versions of Python, the latest Python 2, which is 2.7.14 at the time of typing and Python 3.6.4, I installed these by issuing two brew commands as follows:

# install Python 2
brew install python@2

# install Python 3
brew install python
After this completed and within the same terminal session I could execute the following:
# macOS default
$ python --version
Python 2.7.10

# newer Python 2 from Brew
$ python2 --version
Python 2.7.14

# Python 3 from Brew
$ python3 --version
Python 3.6.4
It is worth adding that the pip commands will only impact the Brew installed versions of Python. This means pip and pip2 both work on Python 2.7.14 and pip3 works on Python 3.6.4.

The macOS system Python is installed under /System/Library/Frameworks/Python.framework/Versions and I believe should be the default Python 2 but the installation of Python 2.7.14 via Brew changes this, so I probably need to fix that. However in general it is probably better if you don't install another Python 2 and focus your work on Python 3.x. The Brew installed Pythons are installed under /usr/local/Cellar/python and /usr/local/Cellar/python@2 respectively.

I believe Visual Studio Code is a good development environment for Python work, however you will want to install the Python extension Python - Visual Studio Marketplace

In VSCode use "Python: Select Interpreter" to choose which Python will be used, however it will default to not using the standard macOS Python if possible. When working with Python in VS Code, it is worth reading Get Started Tutorial with Python in Visual Studio Code first. Note that in the status bar of VSCode you should see the Python version, if you do click this and you can select the interpreter from here. Another point worth mentioning is that you cannot install any Python linter in the Apple Python as it does not have pip but you can with a Brew installed Python.

I believe that concludes all Mac specific Python stuff and it certainly contains all the notes I made. Happy Pythoning....