---
# System prepended metadata

title: Python installation on Mac
tags: [python, shared, mac]

---

# Python installation on Mac
###### tags: `shared` `python` `mac`
[TOC]

+ Materials were mainly excertped from this tutorial: https://stringpiggy.hpd.io/mac-osx-python3-dual-install/
    + New version: https://stringpiggy.hpd.io/mac-osx-python3-multiple-pyenv-install/

## Steps of installing Python 2.x/3.x on Mac with virtual environment

### Step 1: Install Xcode and command line tool
+ Install the latest Xcode version from App Strore and agree the license after installation 
    + The license will be shown in the popup window when the first time you open Xcode
    + Just click *Accept* and continue the following steps
+ Install Xcode command line tool
    + (terminal) `xcode-select --install`

### Step 2: Install Homebrew
+ From my perspective, it's like *pip install* in Linux; for more information: https://docs.brew.sh/Installation.html
+ You can find more detailed information from: https://github.com/Homebrew/install
    + (install) `ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"`
    + (uninstall) `ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/uninstall)"`
+ Run `brew doctor` to see whether its ready to go
    + If there's no problem, you will see something like this `Your system is ready to brew.`
    + There might be a lot of warnings, but if you see something like `If everything you use Homebrew for is working fine: please don’t worry or file an issue; just ignore this. Thanks!`, then it's OK to go to the next step
    + However, it's recommended to fix the errors according to the instructions

### Step 3: Install Python
+ To check the Python version and where it is installed in
    + `python --version` or `python3 --version`
    + `which python` or `which python3`
    + Or, you can try `brew search python` as recommended in the website
+ Install python
    + Usually, you would be wanting to install Python3 without making any changes to the original one (usually Python2.7 installed in `/Library/Frameworks/Python.framework/Versions/2.7/bin`)
    + Use `brew install python3` to install Python3 if not installed yet
    + The installed python (or should say, packages downloaded with `brew install`) will be located in `/usr/local/Cellar`
    + Type `python3` in the terminal, it should enter the python prompt and with version 3.x.x
    + And if you type `python`, you should see the version with 2.x.x

### Step 4: Create virtual encironment
+ It's recommended to create your own virtual environment to avoid conflicts with the original python version, and you will be less confronting with *permission denied* issue when using `pip`
+ More discussion here: https://github.com/googlesamples/assistant-sdk-python/issues/236
+ Create an environment and instal all packages you need there with `pip3`
    + `python3 -m venv env  # creating virtual environment named 'env'`
    + `source /usr/local/bin/env/bin/activate  # activate the environment`
    + `pip3 install [package_name]  # install packages`
    + `deactivate  # deactivate virtual environment`
