Skip to content

Instantly share code, notes, and snippets.

@uogbuji
Last active January 1, 2024 06:30
Show Gist options
  • Save uogbuji/9224717315c1566b7359c87041b8a7c0 to your computer and use it in GitHub Desktop.
Save uogbuji/9224717315c1566b7359c87041b8a7c0 to your computer and use it in GitHub Desktop.

Python dev environment on Mac

User local install as much as possible, leaving system-wide kit alone, and no mega-distributions such as homebrew. Tested all the way through Ventura.

SSH

Make sure SSH is set up. Allow a remote computer to access your Mac and generate a new SSH Key. See appendix below.

There is automatically an ssh agent. Just use ssh-add.

XCode

Install XCode from the App Store, then install Xcode Command Line Tools

xcode-select --install

Shell

Catalina's default shell is zsh, no longer bash. You'll probably want Oh My Zsh:

sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

Git

(H/T SaKKo)

Git comes with XCode, but you might have to follow the prompt to install XCode cmdline tools when you first run it. You'll want to configure it as in ~/.gitconfig. Replace name & email in the following terminal commands:

$ git config --global user.name "John Doe"
$ git config --global user.email "john.doe@gmail.com"

You may just want to copy in or create a ~/.gitconfig. If so Tania Rascia has a sample with some nice, suggested aliases. (Scroll down to Git section).

You may also want to set up a ~/.gitignore (there is an example in the SaKKo link above), though I prefer to have these project by project, so the exclusions can be distributed to collaborators.

Terminal.app thoughts

Tried iTerm2, but meh! preferred to keep it simple. Tried Tmux (instructions for doing so owithout homebrew in appendix) but ran into annoyances such as skewed scrollback and selection. I just use the terminal within VS Code and regular Terminal.app.

Python

The downloads page should detect your OS and present you with a mac download of the latest

Launch the downloaded installer. Note the following in the notes, which we'll come back to:

This package includes its own private copy of OpenSSL 1.1.1. The trust certificates in system and user keychains managed by the Keychain Access application and the security command line utility are not used as defaults by the Python ssl module. A sample command script is included in /Applications/Python 3.8 to install a curated bundle of default root certificates from the third-party certifi package (https://pypi.org/project/certifi/). Double-click on Install Certificates to run it.

The bundled pip has its own default certificate store for verifying download connections.

The key file installed is /usr/local/bin/python3. Note: do not mess with the system-installed Python kit, e.g. /usr/bin/python and /usr/bin/python3. This will be easier if you do everything using virtual environments.

You'll also need to install the bundled certificate, The Python 3.x folder should should pop up in Finder after install. If not, you can go to Applications, double-click the version of Python you installed to reveal an Install Certificates.command and double-click that. You'll want this to avoid certificate verify failed errors at some point.

Clipboard manager

So many options! Picked iClip because it sems to have a nice UI & basic features, and at least offers the option of customizing clips before pasting via action scripts but on a closer look at this option, it seems quite fiddly so havent really explored it yet.

Free alternatives:

  • Maccy. Click the Download Now button, rather than the Get in App Store button, then enter 0 for the Name a fair price: box, then you can download the app for free.
  • Jumpcut.

VS Code

To be honest I haven't found an editor I've liked unreservedly since the days of the Turbo C IDE and Brief, but these days I'm mostly using Visual Studio Code. Download and install.

Create & activate a Python virtual envirnment

Prepare:

mkdir -p $HOME/.local/venv

Create:

export DEVENVTAG=main
/usr/local/bin/python3 -m venv $HOME/.local/venv/$DEVENVTAG

You only need to specify the full path to Python when creating the venv, as above.

Activate:

source $HOME/.local/venv/$DEVENVTAG/bin/activate
pip install --upgrade pip #Needed from time to time, to make sure pip itself is up to date
pip install wheel #One time per venv. Allows pip to install pre-compiled os-specific versions. Also you might get odd errors if you omit this.

In future you only need the first line above to activate the venv.

Appendices

GNU Automake & autoconf

You'll need these to build a lot of project sform scratch if, like me, you prefer to avoid homebrew & macports. Download latest (might be from 2021) from https://ftp.gnu.org/gnu/autoconf/

tar xvf /Users/uche/Downloads/autoconf-latest.tar.xz
cd autoconf-2.71
./configure --prefix=$HOME/.local
make
make install

TODO: Document the above also for flex, bison > 3.0, libtool, make & automake, in order to built jq

Goddam ESC key

If you are unfortunate enough to be on one of the Macs without a proper Escape key (models from ca. 2016-2019), your best bet might involve losing your Caps Lock. Go into System Preferences > Keyboard and look for the Modifier Keys button in the lower right of the dialog. Select the drop-down nxt to Caps Lock and reset it to Escape.

Generate new SSH Key

I prefer to use an Ed25519 key. Make sure you change the my_name int he first line.

ssh-keygen -o -a 100 -t ed25519 -f ~/.ssh/id_ed25519 -C "my_name.macbook"
# eval "$(ssh-agent -s)" # Not needed on Mac Catlina
ssh-add ~/.ssh/id_ed25519
cat .ssh/id_ed25519.pub
# Paste in GitHub/GitLab/etc.

GNU utils (e.g. Parallel)

I needed to use GNU Parallel and all the install guides I could find were for Homebrew. I did it without by following the Prerequisites section of the GNU Parallel tutorial.

tmux (just with plain old Terminal.app)

export CPPFLAGS=-'I$HOME/.local/include'
export LD_LIBRARY_PATH='-L$HOME/.local/lib'
export DYLD_LIBRARY_PATH='-L$HOME/.local/lib'

Alternative to above:

Create config.site in $HOME/.local and set following content:

CPPFLAGS=-I$HOME/.local/include
LDFLAGS=-L$HOME/.local/lib
DYLD_LIBRARY_PATH=-L$HOME/.local/lib

Then set CONFIG_SITE in environment to that file:

Edit $HOME/.zshenv and add:

export PATH="$HOME/.local/bin:$PATH"
export CONFIG_SITE="$HOME/.local/config.site"

And re-source:

source $HOME/.zshenv

Download and untar libevent & tmux. Go into the libevent then tmux dir & in each do:

./configure --prefix=$HOME/.local
make
make install

Highly recommended: tmux-continuum & tmux-resurrect to automatically persist tmux session state for post tmux server restart (or reboot).

To try: tmuxinator for managing tmux sessions, and e.g. nice recovery after tmux server restart (or reboot).

Notes:

Building your own Python

Note: it's become way to fiddly to build Python by hand on Mac OS X, what with SSL issues etc. Just install an updated framework version via .pkg on https://www.python.org/

mkdir -p $HOME/.local/venv
mkdir -p ~/src
cd ~/Downloads
# Haven't got wget working yet
curl -O https://www.python.org/ftp/python/3.10.6/Python-3.10.6.tar.xz
cd ~/src
tar xvf ~/Downloads/Python-3.10*
cd Python-3.10*
./configure --enable-optimizations --prefix=$HOME/.local
make
make install

Note: I was seeing at one point (on M1 Mac): configure: error: check config.log and use the '--with-universal-archs' option

Ended up having to reinstall XCode cmdline tools using following recipe:

sudo rm -rf /Library/Developer/CommandLineTools
xcode-select --install
sudo xcode-select --switch /Library/Developer/CommandLineTools

At which point configure worked.

wget

[Work on instructions]

If you work with web tech you want all the tools. Mac dev includes cURL, but not wget, so we remedy that. Unfortunately it's complex without using HomeBrew.

mkdir -p $HOME/.local/venv
mkdir -p ~/src
cd ~/Downloads
curl -O https://pkg-config.freedesktop.org/releases/pkg-config-0.29.2.tar.gz
curl -O https://ftp.gnu.org/gnu/wget/wget-latest.tar.lz
cd ~/src
tar xf ~/Downloads/pkg-config*
cd pkg-config*
LDFLAGS="-framework CoreFoundation -framework Carbon" ./configure --with-internal-glib --prefix=$HOME/.local
make
make install
cd ~/src
tar xf ~/Downloads/wget-latest.tar.lz
cd wget*
export OPENSSL_CFLAGS="-I$HOME/.local/venv/include"
export OPENSSL_LIBS="-L$HOME/.local/venv/lib -lssl -lcrypto -lz"
 ./configure --with-ssl=openssl --prefix=$HOME/.local

This ./configure step not yet working. Getting warnings about libpsl & fatal error "No package 'openssl' found". If run without --with-ssl=openssl, still dies with "No package 'gnutls' found"

Might be useful. Based on notes in "Mac OS X Sierra Install wget Network Downloader Utility"

cd ~/Downloads
curl -O https://www.openssl.org/source/openssl-1.1.1i.tar.gz
tar xvf openssl-*
cd openssl-*
./config
make
make install
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment