Definitive guide to python on Mac OSX

Photo by Chris Ried on Unsplash

Why this way?

  • Installed PyPi packages pollute your system python, potentially causing system problems
  • OpenSSL used by the system python is old and vulnerable. Old as in, doesn’t understand websites that require TLS 1.1 or later
  • You’re stuck with whatever python 2.7 version comes pre-installed, and no python 3.x

Install Xcode (optional)

Install Xcode from the App Store

Install Xcode Command Line Tools (required)

xcode-select --install
sudo installer -pkg /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg -target /
echo "export SDKROOT=/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk" >> ~/.bash_profile

Install Homebrew

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"

Install pyenv

brew install pyenv
brew install openssl readline sqlite3 xz zlib
echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n  eval "$(pyenv init -)"\nfi' >> ~/.bash_profile

Install Python with Homebrew

  • Reason 1: This Homebrew-installed python becomes a safety net in case we forget to switch to a pyenv-installed python, we won’t accidentally pollute the system python.
  • Reason 2: It becomes a place to install global packages with pipx. This allows you to switch between python versions at-will using pyenv while maintaining a consistent set of global utilities like flake8 and black that are always available and don’t pollute the global python.
# Install python 3.x
brew install python
# Install pipx to manage global packages
python3 -m pip install --user pipx
python3 -m userpath append ~/.local/bin
# Install global packages
python3 -m pipx install flake8
python3 -m pipx install black
brew install python@2

Install python!

$ pyenv install --list
...
3.6.1
3.6.2
3.6.3
3.6.4
3.6.5
3.6.6
3.6.7
3.6.8
3.7.0
3.7-dev
3.7.1
3.7.2
3.7.3
3.8-dev
3.9-dev
<snip>
anaconda3-5.0.0
anaconda3-5.0.1
anaconda3-5.1.0
anaconda3-5.2.0
anaconda3-5.3.0
anaconda3-5.3.1
anaconda3-2018.12
anaconda3-2019.03
...
pyenv install 3.7.3
pyenv global 3.7.3
$ python -V
Python 3.7.3

Install Poetry

  • Obsoletes virtualenv, virtualenvwrapper, pipenv, setup.py, requirements.txt, and more.
  • Lives outside python, so you don’t need to install it with pip or worry about package conflicts.
  • Separates runtime dependencies like requests from development dependencies like pytest.
  • It uses the simple and easy-to-understand pypackage.toml file from pep-518 to contain everything about the project.
  • CLI interface similar to well-known package managers like npm and yarn.
curl -sSL https://raw.githubusercontent.com/sdispater/poetry/master/get-poetry.py | python
poetry config virtualenvs.in-project true

Prevent accidents

Go forth!

poetry new myproject
cd myproject
pyenv local 3.7.3
poetry add requests
poetry add --dev pytest
poetry run python myscript.py

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Antmons Weekly Update

Test your SCIM implementation

Code Your First Alexa Skill in 30-ish Seconds (Using Clay). Ready? Go

Algorithm Cheatsheet (continuously updated)

Pointer: Hunting Cobalt Strike globally

The book contains a chapter called ‘The Tears’. At its conclusion, John Glenn is

Leverage your Python unit tests with Nox and Docker

Jenkins Interview Questions For Intermediate

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Brian Torres-Gil

Brian Torres-Gil

More from Medium

How to access to Raspberry Pi Desktop using the remote desktop protocol.

How I manage my Python environments on a Mac with two simple tools

My VSCode Setup

How I Make Separate Python Environments for Different Projects