Trying to build Python extension (module) with Dlang and pyd with dub + ldc2

stpettersens s.stpettersen+github at gmail.com
Fri Nov 14 23:06:00 UTC 2025


Hi there.

I am trying to build a Python extension (module) with D and the 
pyd (https://github.com/ariovistus/pyd) and dub and ldc2 compiler 
on Windows.

I have the following dub.sdl:

```
name "hello"
description "Python bindings module for human-datetime.d library."
authors "Sam Saint-Pettersen"
copyright "Copyright 2025 Sam Saint-Pettersen"
license "MIT"
dependency "pyd" version="~>0.14.5"
subConfiguration "pyd" "python313"
targetType "dynamicLibrary"
dflags "-g" "-w"
sourcePaths "source"
lflags "/LIBPATH:C:\\Dev\\Python313\\libs" platform="windows"
libs "python313" platform="windows"
```

source/hello.d (module code):

```d
module hello;

import pyd.pyd;
import std.stdio;

extern(C) void hello() {
     writefln("Hello, world!");
}

extern(C) void PydMain() {
     def!(hello);
     module_init();
}
```

Build batch file (build.bat):

```cmd
@cls
@set PYTHON_INCLUDE_DIR=C:\Dev\Python313\include
@set PYTHON_LIBRARY=C:\Dev\Python313\libs\python313.lib
@dub clean
@dub build --compiler=ldc2 --force
@dumpbin /exports hello.dll | findstr PyInit_hello
@copy druntime-ldc-shared.dll C:\Dev\Python313\Lib\site-packages
@copy phobos2-ldc-shared.dll C:\Dev\Python313\Lib\site-packages
@copy hello.dll C:\Dev\Python313\Lib\site-packages\hello.pyd
```

The problem I'm having is:

```
ImportError: dynamic module does not define module export 
function (PyInit_hello)
```

This is because the built **hello.pyd** does not seem to be 
exporting the entry function.
As evident by empty output from `dumpbin /exports hello.dll | 
findstr PyInit_hello`.

Does anyone who has used **ariovistus/pyd** here know what I'm 
doing wrong?
I understand that maybe this is a niche question.

Thanks in advance.

~ Sam.


More information about the Digitalmars-d-learn mailing list