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 Answer
Reset to default 2Your 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
版权声明:本文标题:python - SWIG Hello World; ImportError: dynamic module does not define module export function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736307334a1933273.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
ext-modules = [{ name = "_hello",...
(_hello)? – CristiFati Commented Nov 21, 2024 at 21:06