ImportError: dynamic module does not define module export function

Robert Simmons <[email protected]> Thu, 21 Nov 2024 16:26:29 -0500
Newsgroups gmane.comp.programming.swig
Message-ID <CA+QLa9DZL_6Pm1F2Y9KRv8QsvOAzCfmu-LkE4msMtPLAgSQFjA@mail.gmail.com>
--===============8399119697311934270==
Content-Type: multipart/alternative; boundary="000000000000124a98062772ecb7"

--000000000000124a98062772ecb7
Content-Type: text/plain; charset="UTF-8"

When importing a hello world, I'm seeing an exception being raised. Any
help would be appreciated.

>>> 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)

I see someone had a similar issue and was directed to ask here:
https://github.com/swig/swig/discussions/2961

I also see the following:
https://pythonextensionpatterns.readthedocs.io/en/latest/miscellaneous.html

I am on macOS, so I've examined the .so in the same way below.

Here is the directory structure:

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

Here is the SWIG interface file:

%module hello

%{
void say_hello();
%}

void say_hello();

Here is the C:

#include <stdio.h>

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

here is the contents of 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 nm output:

% nm -m lib.macosx-14.0-x86_64-cpython-313/hello.cpython-313-darwin.so |
grep Init
0000000000001140 (__TEXT,__text) external _PyInit__hello
0000000000004120 (__DATA,__data) non-external _PyInit__hello.SWIG_module

--000000000000124a98062772ecb7
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">When importing a hello world, I&#39;m seeing an exception =
being raised. Any help=C2=A0would be appreciated.<div><br></div><div>&gt;&g=
t;&gt; import hello<br>Traceback (most recent call last):<br>=C2=A0 File &q=
uot;&lt;python-input-0&gt;&quot;, line 1, in &lt;module&gt;<br>=C2=A0 =C2=
=A0 import hello<br>ImportError: dynamic module does not define module expo=
rt function (PyInit_hello)</div><div><br></div><div>I see someone had a sim=
ilar issue and was directed to ask here:</div><div><a href=3D"https://githu=
b.com/swig/swig/discussions/2961">https://github.com/swig/swig/discussions/=
2961</a></div><div><br></div><div>I also see the following:</div><div><a hr=
ef=3D"https://pythonextensionpatterns.readthedocs.io/en/latest/miscellaneou=
s.html">https://pythonextensionpatterns.readthedocs.io/en/latest/miscellane=
ous.html</a></div><div><br></div><div>I am on macOS, so I&#39;ve examined t=
he .so in the same way below.</div><div><br></div><div>Here is the director=
y structure:</div><div><br></div><div>README.md<br>pyproject.toml<br>src<br=
>src/hc<br>src/hc/hello.c<br>src/hc/hello.i</div><div><br></div><div>Here i=
s the SWIG interface file:</div><div><br></div><div>%module hello<br><br>%{=
<br>void say_hello();<br>%}<br><br>void say_hello();</div><div><br></div><d=
iv>Here is the C:</div><div><br></div><div>#include &lt;stdio.h&gt;<br><br>=
void say_hello() {<br>=C2=A0 =C2=A0 printf(&quot;Hello, World!\n&quot;);<br=
>}<br><br>here is the contents of pyproject.toml<br><br>[build-system]<br>r=
equires =3D [&quot;setuptools&gt;=3D75.6&quot;]<br>build-backend =3D &quot;=
setuptools.build_meta&quot;<br><br>[project]<br>name =3D &quot;helloc&quot;=
<br>version =3D &quot;0.0.1&quot;<br>authors =3D [<br>=C2=A0 =C2=A0 { name =
=3D &quot;Foo&quot; }<br>]<br>description =3D &quot;Hello world SWIG C&quot=
;<br>readme =3D &quot;README.md&quot;<br>requires-python =3D &quot;&gt;=3D3=
.13&quot;<br>classifiers =3D [<br>=C2=A0 =C2=A0 &quot;Development Status ::=
 1 - Planning&quot;,<br>=C2=A0 =C2=A0 &quot;License :: Public Domain&quot;,=
<br>=C2=A0 =C2=A0 &quot;Natural Language :: English&quot;,<br>=C2=A0 =C2=A0=
 &quot;Operating System :: OS Independent&quot;,<br>=C2=A0 =C2=A0 &quot;Pro=
gramming Language :: Python&quot;,<br>=C2=A0 =C2=A0 &quot;Programming Langu=
age :: Python :: 3&quot;,<br>=C2=A0 =C2=A0 &quot;Programming Language :: Py=
thon :: 3 :: Only&quot;,<br>=C2=A0 =C2=A0 &quot;Programming Language :: Pyt=
hon :: 3.13&quot;<br>]<br><br>[tool.setuptools]<br>ext-modules =3D [<br>=C2=
=A0 { name =3D &quot;hello&quot;, sources =3D [&quot;src/hc/hello.c&quot;, =
&quot;src/hc/hello.i&quot;] }<br>]</div><div><br></div><div>Here is nm outp=
ut:</div><div><br></div><div>% nm -m lib.macosx-14.0-x86_64-cpython-313/<a =
href=3D"http://hello.cpython-313-darwin.so">hello.cpython-313-darwin.so</a>=
 | grep Init<br>0000000000001140 (__TEXT,__text) external _PyInit__hello<br=
>0000000000004120 (__DATA,__data) non-external _PyInit__hello.SWIG_module</=
div></div>

--000000000000124a98062772ecb7--


--===============8399119697311934270==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline


--===============8399119697311934270==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
Swig-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/swig-user

--===============8399119697311934270==--