admin管理员组

文章数量:1122832

This is supposed to be the absolute minimum Hello World using SWIG, C, and setuptools. But the following exception is raised when the module is imported:

>>> import hello
Traceback (most recent call last):
  File "<python-input-0>", line 1, in <module>
    import hello
ImportError: dynamic module does not define module export function (PyInit_hello)

This is the directory structure:

README.md
pyproject.toml
src
src/hc
src/hc/hello.c
src/hc/hello.i

Here is the pyproject.toml

[build-system]
requires = ["setuptools>=75.6"]
build-backend = "setuptools.build_meta"

[project]
name = "helloc"
version = "0.0.1"
authors = [
    { name = "Foo" }
]
description = "Hello world SWIG C"
readme = "README.md"
requires-python = ">=3.13"
classifiers = [
    "Development Status :: 1 - Planning",
    "License :: Public Domain",
    "Natural Language :: English",
    "Operating System :: OS Independent",
    "Programming Language :: Python",
    "Programming Language :: Python :: 3",
    "Programming Language :: Python :: 3 :: Only",
    "Programming Language :: Python :: 3.13"
]

[tool.setuptools]
ext-modules = [
  { name = "hello", sources = ["src/hc/hello.c", "src/hc/hello.i"] }
]

Here is hello.c

#include <stdio.h>

void say_hello() {
    printf("Hello, World!\n");
}

And here is the interface file:

%module hello

%{
void say_hello();
%}

void say_hello();

This is supposed to be the absolute minimum Hello World using SWIG, C, and setuptools. But the following exception is raised when the module is imported:

>>> import hello
Traceback (most recent call last):
  File "<python-input-0>", line 1, in <module>
    import hello
ImportError: dynamic module does not define module export function (PyInit_hello)

This is the directory structure:

README.md
pyproject.toml
src
src/hc
src/hc/hello.c
src/hc/hello.i

Here is the pyproject.toml

[build-system]
requires = ["setuptools>=75.6"]
build-backend = "setuptools.build_meta"

[project]
name = "helloc"
version = "0.0.1"
authors = [
    { name = "Foo" }
]
description = "Hello world SWIG C"
readme = "README.md"
requires-python = ">=3.13"
classifiers = [
    "Development Status :: 1 - Planning",
    "License :: Public Domain",
    "Natural Language :: English",
    "Operating System :: OS Independent",
    "Programming Language :: Python",
    "Programming Language :: Python :: 3",
    "Programming Language :: Python :: 3 :: Only",
    "Programming Language :: Python :: 3.13"
]

[tool.setuptools]
ext-modules = [
  { name = "hello", sources = ["src/hc/hello.c", "src/hc/hello.i"] }
]

Here is hello.c

#include <stdio.h>

void say_hello() {
    printf("Hello, World!\n");
}

And here is the interface file:

%module hello

%{
void say_hello();
%}

void say_hello();
Share Improve this question asked Nov 21, 2024 at 20:29 UtkonosUtkonos 79510 silver badges27 bronze badges 3
  • 1 ext-modules = [{ name = "_hello",... (_hello)? – CristiFati Commented Nov 21, 2024 at 21:06
  • That worked! Please make that into an answer! I will vote it. – Utkonos Commented Nov 21, 2024 at 21:31
  • You don't need to, but if you can explain what happened, that would be awesome as well. – Utkonos Commented Nov 21, 2024 at 21:32
Add a comment  | 

1 Answer 1

Reset to default 2

Your extension module should be named _hello (notice the leading "_").

pyproject.toml:

# ...

[tool.setuptools]
ext-modules = [
  { name = "_hello", sources = ["src/hc/hello.c", "src/hc/hello.i"] }
]

Check:

  • [SO]: c program SWIG to python gives 'ImportError: dynamic module does not define init function' (@CristiFati's answer) contains the SWIG related details

  • [SO]: How to solve Python-C-API error "This is an issue with the package mentioned above, not pip."? (@CristiFati's answer) contains info about building with SetupTools

本文标签: pythonSWIG Hello World ImportError dynamic module does not define module export functionStack Overflow