Skip to content

Instantly share code, notes, and snippets.

@tylergannon
Created February 16, 2023 14:49
Show Gist options
  • Save tylergannon/90841fb1cb1dc24c318ba345fcc94f4c to your computer and use it in GitHub Desktop.
Save tylergannon/90841fb1cb1dc24c318ba345fcc94f4c to your computer and use it in GitHub Desktop.
Building Python C extensions on macOS

Setting up and building a Python C extension project on macOS

These instructions were developed for a project using vscode on macOS 13.0.1.

Install needed software

If you haven't already, you'll need an updated xcode command-line tools. Do this by running xcode-select --install or by going to https://developer.apple.com and downloading the XCode command line tools. I wound up with version 14.2.

cmake

if test -e /opt/homebrew/bin/cmake
  brew update && brew upgrade cmake
else
  brew update && brew install cmake
end

Build Python with its shared library

I manage my Python versions with asdf, which in turn uses the pyenv installer. By default it didn't build libpython3.11.dylib. Simply adding PYTHON_CONFIGURE_OPTS=--enable-shared should be sufficient, but I took the opportunity to also build Python with explicit optimizations for my M1 cpu.

As of this writing, Miniconda offers an M1-specific build of Python 3.10, which includes the dylib. I didn't check homebrew.

set PY_VERSION "3.11"
set PY_MINOR_VER $PY_VERSION".0"
set -x PYTHON_CFLAGS "-mtune=native -mcpu=apple-m1"
set -x PYTHON_CONFIGURE_OPTS "--enable-shared --enable-optimizations --with-lto"
asdf install python $PY_MINOR_VER
if test -e ~/.asdf/installs/python/$PY_MINOR_VER/lib/libpython$PY_VERSION.dylib
    echo "Confirmed that the dylib file has been built."
else
    echo "Check your settings, I don't see a dylib file."
end

Configure VSCode

Install cmake and clang extensions:

for ext in mitaki28.vscode-clang twxs.cmake ms-vscode.cmake-tools
  code --install-extension $ext
end

Add the following to your workspace's ./.vscode/settings.json

{
    "cmake.configureArgs": [
        "-DPYTHON_INCLUDE_DIR=/Users/your_name/.asdf/installs/python/3.11.0/include/python3.11",
        "-DPYTHON_LIBRARY=/Users/your_name/.asdf/installs/python/3.11.0/lib/libpython3.11.dylib"
    ]
}

When that is done, then you can run CMake: Select a kit and choose the latest clang. Then run CMake: configure.

Watch the terminal for output. If you have to reset your build, delete the build directory and run CMake: reset tools extension state (for troubleshooting).

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