Skip to main content

A python library that extends setuptools for binary extensions.

Project description

# Milksnake

<a href=""><img src="" alt=""></a>
<a href=""><img src="" alt=""></a>
<a href=""><img src="" alt=""></a>

Milksnake is an extension for setuptools that allows you to distribute
dynamic linked libraries in Python wheels in the most portable way imaginable.

It gives you a hook to invoke your own build process and to then take the
resulting dynamic linked library.

## Why?

There are already other projects that make Python and native libraries play
along but this one is different. Unlike other projects that build Python
extension modules the goal of this project is to build regular native libraries
that are then loaded with CFFI at runtime. Why not just use CFFI? Because
CFFI's setuptools support alone does not properly work with such wheels (it
does not provide a way to build and properly tag wheels for shared libraries) and
it does not provide a good way to invoke an external build process (like a
makefile, cargo to build rust binaries etc.)

In particular you will most likely only need two wheels for Linux, one for macs
and soon one for Windows independently of how many Python interpreters you want
to target.

## What is supported?

* Platforms: Linux, Mac, Windows
* setuptools commands: `bdist_wheel`, `build`, `build_ext`, `develop`
* `pip install --editable .`
* Universal wheels (`PACKAGE-py2.py3-none-PLATFORM.whl`); this can be disabled
with `milksnake_universal=False` in `setup()` in case the package also contains
stuff that does link against libpython.

## How?

This example shows how to build a rust project with it:

This is what a `` file looks like:

from setuptools import setup

def build_native(spec):
# build an example rust library
build = spec.add_external_build(
cmd=['cargo', 'build', '--release'],

dylib=lambda: build.find_dylib('example', in_path='target/release'),
header_filename=lambda: build.find_header('example.h', in_path='target'),
rtld_flags=['NOW', 'NODELETE']


You then need a `rust/` folder that has a Rust library (with a crate type
of `cdylib`) and a `example/` python package.

Example `example/` file:

from example._native import ffi, lib

def test():
return lib.a_function_from_rust()

And a `rust/src/`:

pub unsafe extern "C" fn a_function_from_rust() -> i32 {

And the `rust/Cargo.toml`:

name = "example"
version = "0.1.0"
build = ""

name = "example"
crate-type = ["cdylib"]

cbindgen = "0.4"

And finally the file:

extern crate cbindgen;

use std::env;

fn main() {
let crate_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
let mut config: cbindgen::Config = Default::default();
config.language = cbindgen::Language::C;
cbindgen::generate_with_config(&crate_dir, config)

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for milksnake, version 0.1.5
Filename, size File type Python version Upload date Hashes
Filename, size milksnake-0.1.5-py2.py3-none-any.whl (9.6 kB) File type Wheel Python version py2.py3 Upload date Hashes View
Filename, size (11.9 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page