Skip to content
Expose build variables obtained with built as a PyDict
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.
ci/travis Setup Travis-CI to build the crate and the example May 11, 2018
example Add example project using pyo3-built May 11, 2018
src Implement `pyo3_built!` macro May 11, 2018
.travis.yml Setup Travis-CI to build the crate and the example May 11, 2018
Cargo.toml Add `Cargo.toml` with minimal package metadata May 11, 2018


Simple macro to expose metadata obtained with the built crate as a PyDict


Add the following to your Cargo.toml manifest:

built = "^0.3"

pyo3-built = "^0.1"

Create your as you normally would with built, but activate dependencies metadata as well:

extern crate built;

fn main() {
    let src = std::env::var("CARGO_MANIFEST_DIR").unwrap();
    let dst = std::path::Path::new(&std::env::var("OUT_DIR").unwrap()).join("");
    let mut opts = built::Options::default();
    built::write_built_file_with_opts(&opts, src, dst)
        .expect("Failed to acquire build-time information");

Then, include the generated file anywhere in a dedicated module in your Python extension:


extern crate pyo3_built;
extern crate pyo3;

use pyo3::prelude::*;
use pyo3::py::modinit;

mod build {
    include!(concat!(env!("OUT_DIR"), "/"));

/// Module documentation string
fn init(py: Python, m: &PyModule) -> PyResult<()> {
    // ... //
    m.add("__build__", pyo3_built!(py, build))?;

That's it ! After compiling your extension module, the __build__ attribute will contain the following metadata:

>>> import mymodule
>>> mymodule.__build__
   "build-time": datetime.datetime(2018, 5, 11, 16, 43, 28),
   "debug": true,
   "dependencies": {
      "pyo3": "0.2.6",
      "pyo3-built": "0.1.0",
      "pyo3cls": "0.2.1",
   "features": [
   "host": {
      "triple": "x86_64-unknown-linux-gnu"
   "opt-level": "0",
   "rustc": "rustc",
   "rustc-version": "rustc 1.27.0-nightly (acd3871ba 2018-05-10)",
   "target": {
      "arch": "x86_64",
      "endianness": "little",
      "env": "gnu",
      "family": "unix",
      "os": "linux",
      "pointer-width": "64",
      "profile": "debug",
      "triple": "x86_64-unknown-linux-gnu"
You can’t perform that action at this time.