#build-dependencies #python #windows #msvc #mingw


Standalone python3(y).dll import library generator

7 releases

Uses new Rust 2021

0.2.3 May 17, 2022
0.2.2 May 10, 2022
0.2.1 Apr 17, 2022
0.2.0 Mar 22, 2022
0.1.0 Feb 21, 2022

#34 in Build Utils

Download history 49/week @ 2022-03-10 39/week @ 2022-03-17 11/week @ 2022-03-24 6/week @ 2022-03-31 191/week @ 2022-04-07 326/week @ 2022-04-14 343/week @ 2022-04-21 697/week @ 2022-04-28 764/week @ 2022-05-05 823/week @ 2022-05-12 623/week @ 2022-05-19 904/week @ 2022-05-26 864/week @ 2022-06-02 801/week @ 2022-06-09 1169/week @ 2022-06-16 616/week @ 2022-06-23

3,646 downloads per month
Used in 3 crates (via pyo3-build-config)

MIT license

275 lines

Standalone python3(y).dll import library generator

Generates import libraries for the Python DLL (either python3.dll or python3y.dll) for MinGW-w64 and MSVC (cross-)compile targets.

This crate does not require Python 3 distribution files to be present on the (cross-)compile host system.

Note: MSVC cross-compile targets require either LLVM binutils or Zig to be available on the host system. More specifically, python3-dll-a requires llvm-dlltool executable to be present in PATH when targeting *-pc-windows-msvc from Linux.

Alternatively, ZIG_COMMAND environment variable may be set to e.g. "zig" or "python -m ziglang", then zig dlltool will be used in place of llvm-dlltool (or MinGW binutils).

PyO3 integration

Since version 0.16.5, the pyo3 crate implements support for both the Stable ABI and version-specific Python DLL import library generation via its new generate-import-lib feature.

In this configuration, python3-dll-a becomes a pyo3 crate dependency and is automatically invoked by its build script in both native and cross compilation scenarios.

Example Cargo.toml usage for an abi3 PyO3 extension module

pyo3 = { version = "0.16.5", features = ["extension-module", "abi3-py37", "generate-import-lib"] }

Example Cargo.toml usage for a standard PyO3 extension module

pyo3 = { version = "0.16.5", features = ["extension-module", "generate-import-lib"] }

Standalone build script usage

If an older pyo3 crate version is used, or a different Python bindings library is required, python3-dll-a can be used directly from the crate build script.

The examples below assume using an older version of PyO3.

Example build.rs script for an abi3 PyO3 extension

The following cargo build script can be used to cross-compile Stable ABI PyO3 extension modules for Windows (64/32-bit x86 or 64-bit ARM) using either MinGW-w64 or MSVC target environment ABI:

fn main() {
    if std::env::var("CARGO_CFG_TARGET_OS").unwrap() == "windows" {
        let cross_lib_dir = std::env::var_os("PYO3_CROSS_LIB_DIR")
            .expect("PYO3_CROSS_LIB_DIR is not set when cross-compiling");
        let arch = std::env::var("CARGO_CFG_TARGET_ARCH").unwrap();
        let env = std::env::var("CARGO_CFG_TARGET_ENV").unwrap();

        let libdir = std::path::Path::new(&cross_lib_dir);
        python3_dll_a::generate_implib_for_target(libdir, &arch, &env)
            .expect("python3.dll import library generator failed");

A compatible python3.dll import library file named python3.dll.a or python3.lib will be automatically created in the directory pointed by the PYO3_CROSS_LIB_DIR environment variable.

Example cargo build invocation

PYO3_CROSS_LIB_DIR=target/python3-dll cargo build --target x86_64-pc-windows-gnu

Generating version-specific python3y.dll import libraries

As an advanced feature, python3-dll-a can generate Python version specific import libraries such as python39.lib.

See the ImportLibraryGenerator builder API description for details.


This crate embeds Module-Defitions based on the stable_abi.toml file from CPython.

The upstream version of this file is located in the CPython project repository under the path Misc/stable_abi.toml.