# Install Python on macOS in 30 seconds #### Install Python on OS X / macOS 10.9 - 10.15+ (64 bit) - Catalina, Mojave, High Sierra, Sierra, El Capitan, Yosemite or Mavericks - Usage: copy one of the three following lines and paste it into your terminal. Type the sudo password. ##### Python 3.8 ``` sudo true; v=3.8.4; f=python-${v}-macosx10.9.pkg; cd $TMPDIR; { curl -LfOsS https://www.python.org/ftp/python/${v}/$f ; cd ~-; } && sudo installer -pkg $TMPDIR/$f -target / && v=$(echo "$v"|cut -d'.' -f1-2); "/Applications/Python ${v}/Update Shell Profile.command" && . ~/.bash_profile ``` ##### Python 2.7 ``` sudo true; v=2.7.18; f=python-${v}-macosx10.9.pkg; cd $TMPDIR; { curl -LfOsS https://www.python.org/ftp/python/${v}/$f ; cd ~-; } && sudo installer -pkg $TMPDIR/$f -target / && v=$(echo "$v"|cut -d'.' -f1-2); "/Applications/Python ${v}/Update Shell Profile.command" && . ~/.bash_profile ``` ##### Python x.y.z (input the version) - Among the current choices: `2.7.18` and `3.6.8` `3.7.8` `3.8.4` ``` sudo true; read -p 'Version? ' v; f=python-${v}-macosx10.9.pkg; cd $TMPDIR; { curl -LfOsS https://www.python.org/ftp/python/${v}/$f; cd ~-; } && sudo installer -pkg $TMPDIR/$f -target / && v=$(echo "$v"|cut -d'.' -f1-2) && "/Applications/Python ${v}/Update Shell Profile.command" && . ~/.bash_profile ``` --- ### Create a virtual environment ``` mkdir ~/PyProject1 && cd ~/PyProject1 && pip3 install --upgrade pip && pip3 install pipenv==2020.5.28 && pipenv install && pipenv shell env | grep -i pip python3 ``` --- #### Remove ALL python versions from Python.org ``` sudo rm -rf /Library/Frameworks/Python.framework/Versions/?.*; sudo rm -rf "/Applications/Python "?.?; cd /usr/local/bin && { ls -l | grep '/Library/Frameworks/Python.framework/Versions/*' | awk '{print $9}' | tr -d @ | xargs sudo rm -rfv; cd ~-; }; b="$HOME/.bash_profile"; sed -i.bak '/Frameworks\/Python*/d' $b; sed -i '' '/PATH for Python/d' $b; sed -i '' '/bash_profile\.pysave/d' $b && nano ~/.bash_profile ``` - check with `nano ~/.bash_profile` and clean up any remaining lines from Python.org