Skip to content

Instantly share code, notes, and snippets.

@wilmoore
Last active December 10, 2021 13:25
Show Gist options
  • Save wilmoore/52bceca761f4c69047fd52d0d636d950 to your computer and use it in GitHub Desktop.
Save wilmoore/52bceca761f4c69047fd52d0d636d950 to your computer and use it in GitHub Desktop.
Installing Python on a MacBook

Installing Python on a MacBook

Made with ♥ by Polyglot.

A short guide curating the best information on how to install a working python on a MacBook without completely breaking the entire fucking system. Seems like it would be easy to do, but it's not as easy as one would think. Ultimately, you need a way to manage the installed python version and depedencies per project.

This guide assumes you've got a MacBook with a relatively recent OS (YMMV), homebrew (brew), and you understand how $PATH and your $SHELL works at the most basic level

Caveats

  • In order to connect to PostgreSQL servers, SQL Alchemy needs libpg; however, the best known way to do this is to brew install postgresql; however, that includes the server. Sometimes this is OK; however, there are legitimate reason for not wanting to do this, so in that case, instead of installing sqlalchemy + psycopg2 you'd install sqlalchemy + pg8000 and your connection string prefix changes from postgresql:// to postgresql+pg8000://.

Tools

Vocabulary

  • Module: a file containing python code
  • Package: a directory of python modules
  • Distribution: an archived package

Install Pyenv

❯ brew update
❯ brew install pyenv

Install Poetry

configures poetry to create virtual environments inside the project's root directory

❯ curl -fsSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python
❯ poetry config virtualenvs.in-project true

Shell Configuration

Place the following or the equivalent into your shell's equivalent initialization file

❯ cat ~/.config/fish/conf.d/python-setup.fish
source ~/.poetry/env
status --is-interactive; and source (pyenv init -|psub)

Install Latest Version of Python via pyenv

releases

❯ pyenv install 3.8.0

Set Global Version of Python (optional)

❯ pyenv global 3.8.0
❯ pyenv version

Create a new Python Project with Poetry, add production and development libraries

❯ poetry new yet-another-api-client
❯ cd yet-another-api-client
❯ poetry add requests sqlalchemy pg8000 tabulate
❯ poetry add --dev pytest black flake8

REPL

prints table data in ascii/pgsql format to screen

poetry run python3
~
Python 3.8.0 (default, Oct  7 2020, 17:23:33)
[Clang 11.0.3 (clang-1103.0.32.62)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from sqlalchemy import create_engine, text, inspect
>>> from tabulate import tabulate
>>> engine = create_engine('postgresql+pg8000://scott:tiger@localhost/todos', future=True, echo=True, echo_pool='debug')
>>> connection = engine.connect()
>>> stmt = text('SELECT * FROM todos')
>>> result = connection.execute(stmt)
>>> records = result.fetchall()
>>> print(tabulate(records, headers=result.keys(), tablefmt='psql'))
~

Troubleshooting

List Configuration

⪼ poetry config --list
~
cache-dir = "/Users/wilmoore/Library/Caches/pypoetry"
experimental.new-installer = true
virtualenvs.create = true
virtualenvs.in-project = true
virtualenvs.path = "{cache-dir}/virtualenvs"  # /Users/wilmoore/Library/Caches/pypoetry/virtualenvs
/var/folders/nr/8s8lg_7s0q581yjmssrvywph0000gn/T
~

Resources

Inspiration & Props

Resources

SQLAlchemy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment