Thursday, July 2, 2020

Installing and configuring Python on MacOS (Catalina)

Installing Python (latest) on MacOS (Catalina)

New computer... and a new opportunity to setup python. This happens about as often as I update my blog. Things change, so here's what installing python on MacOS looks like circa July 2020. 

Set BASH as default SHELL

First of all... I prefer BASH as my SHELL. In Catalina ZSH is the default. Changing the default shell to BASH is a setting in Terminal:
Terminal--> Preferences --> General * Shells open with: Command (complete path): /bin/bash

Reference:

Suppress annoying ZSH warning 

Apple really would prefer I stop using BASH. When opening a Terminal by default a message tells me that ZSH is now the default shell. That's a quickly tiresome reminder that I really should be using ZSH, and why am I using BASH anyway?--BASH has been deprecated. To make matters worse, the version of BASH installed by default on Catalina is quite old, and ZSH is all up to date. But, I don't want to be deterred, and I don't want to see this warning every time I launch a Terminal window, so, I added this to my ~/.bash_profile (reminder: .bash_profile is used instead of .bashrc on Mac OS X).
export BASH_SILENCE_DEPRECATION_WARNING=1

Set BASH command prompt

Yeah, I have preferences (doesn't everybody?). I set my command prompt to show me which user is active and what directory I'm in. This goes in my .bash_profile:

PS1='\u:\w\$ '

Install brew (MacOS package manager)

There's little life in a laptop you want to use to write code that doesn't have a command line package manager. So... (do this with a user with 'admin' privileges):
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)" 
Reference:  

Install pyenv, pipenv

Reference: I'm starting this section with the reference, because I want to give a HT to Gioele Barabucci for an amusing nod to how convoluted this has been, and an excellent description of how to setup a modern python environment: 
tl;dr version: Segregate all your project dependencies and versioning by using pyenv and pipenv to manage different environments within a project/directory. This is a really nice way of doing things. It gets all the way around mucking about with the OS provided python installs and versions and your own dependency hell.  Use brew to install these tools. 
brew install pyenv 
brew install pipenv

Installing pyenv creates a .pyenv directory under your home directory where it stores and manages different versions of python so you can have them at your fingertips.

 Configure pyenv

We're getting close now. 

pyenv --list # shows you all the available versions of python that are available. There are a lot!
pyenv install 3.8.3 #install 3.8.3 version of python -- latest
pyenv global 3.8.3 # set 3.8.3 as the default version
pyenv init
This last statement, pyenv init, is the magic that connects your shell to the local versions of python installed and managed by pyenv. Really, all it does is adds the .pyenv directory to your PATH with a symlink to the version of python you want to use. Add this to your .bash_profile to have pyenv manage your python stuff by default.
eval "$(pyenv init -)"
Reference:  
https://realpython.com/intro-to-pyenv/   

 Configure pipenv

 pipenv is the glue between pyenv and your virtual python environments. pipenv install creates a file called Pipfile in your current directory or project where it tracks the python version for this project and any dependent packages you install for THIS project and only THIS project. For example:
pipenv install tweepy 
And as the output helpfully suggests: To activate this project's virtualenv, run:
pipenv shell 
Development environment segregation FTW!  

Install pycharm

Honestly, I might keep using vim for the most part--old habits. I may be an old dog, but I want to learn new tricks, so, installing an IDE. Download the installer here:


EOJ


No comments:

Post a Comment