Re: ImportError: dynamic module does not define module export function

Robert Simmons <[email protected]> Thu, 21 Nov 2024 16:47:02 -0500
Newsgroups gmane.comp.programming.swig
Message-ID <CA+QLa9DYN8=rvFyP+mPbB9Q_JGQiLJQojMT-zXDCunX7M9HCTA@mail.gmail.com>
--===============6910473905956694274==
Content-Type: multipart/alternative; boundary="0000000000008d2d2f062773357f"

--0000000000008d2d2f062773357f
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

No response needed. I found the problem. The module name in pyproject.toml
needed to have a _ prefix. I'm reading up on why.

h/t here:
https://stackoverflow.com/questions/79212853/swig-hello-world-importerror-d=
ynamic-module-does-not-define-module-export-func

On Thu, Nov 21, 2024 at 4:26=E2=80=AFPM Robert Simmons <[email protected]=
> wrote:

> 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.ht=
ml
>
> 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 =3D ["setuptools>=3D75.6"]
> build-backend =3D "setuptools.build_meta"
>
> [project]
> name =3D "helloc"
> version =3D "0.0.1"
> authors =3D [
>     { name =3D "Foo" }
> ]
> description =3D "Hello world SWIG C"
> readme =3D "README.md"
> requires-python =3D ">=3D3.13"
> classifiers =3D [
>     "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 =3D [
>   { name =3D "hello", sources =3D ["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
>

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

<div dir=3D"ltr">No response needed. I found the problem. The module name i=
n pyproject.toml needed to have a _ prefix. I&#39;m reading up on why.<div>=
<br></div><div>h/t here:</div><div><a href=3D"https://stackoverflow.com/que=
stions/79212853/swig-hello-world-importerror-dynamic-module-does-not-define=
-module-export-func">https://stackoverflow.com/questions/79212853/swig-hell=
o-world-importerror-dynamic-module-does-not-define-module-export-func</a></=
div></div><br><div class=3D"gmail_quote"><div dir=3D"ltr" class=3D"gmail_at=
tr">On Thu, Nov 21, 2024 at 4:26=E2=80=AFPM Robert Simmons &lt;<a href=3D"m=
ailto:[email protected]">[email protected]</a>&gt; wrote:<br></div><blo=
ckquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-left=
:1px solid rgb(204,204,204);padding-left:1ex"><div dir=3D"ltr">When importi=
ng a hello world, I&#39;m seeing an exception being raised. Any help=C2=A0w=
ould be appreciated.<div><br></div><div>&gt;&gt;&gt; import hello<br>Traceb=
ack (most recent call last):<br>=C2=A0 File &quot;&lt;python-input-0&gt;&qu=
ot;, line 1, in &lt;module&gt;<br>=C2=A0 =C2=A0 import hello<br>ImportError=
: dynamic module does not define module export function (PyInit_hello)</div=
><div><br></div><div>I see someone had a similar issue and was directed to =
ask here:</div><div><a href=3D"https://github.com/swig/swig/discussions/296=
1" target=3D"_blank">https://github.com/swig/swig/discussions/2961</a></div=
><div><br></div><div>I also see the following:</div><div><a href=3D"https:/=
/pythonextensionpatterns.readthedocs.io/en/latest/miscellaneous.html" targe=
t=3D"_blank">https://pythonextensionpatterns.readthedocs.io/en/latest/misce=
llaneous.html</a></div><div><br></div><div>I am on macOS, so I&#39;ve exami=
ned the .so in the same way below.</div><div><br></div><div>Here is the dir=
ectory structure:</div><div><br></div><div>README.md<br>pyproject.toml<br>s=
rc<br>src/hc<br>src/hc/hello.c<br>src/hc/hello.i</div><div><br></div><div>H=
ere is 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></d=
iv><div>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>requires =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 Stat=
us :: 1 - Planning&quot;,<br>=C2=A0 =C2=A0 &quot;License :: Public Domain&q=
uot;,<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 &qu=
ot;Programming Language :: Python&quot;,<br>=C2=A0 =C2=A0 &quot;Programming=
 Language :: Python :: 3&quot;,<br>=C2=A0 =C2=A0 &quot;Programming Language=
 :: Python :: 3 :: Only&quot;,<br>=C2=A0 =C2=A0 &quot;Programming Language =
:: Python :: 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&q=
uot;, &quot;src/hc/hello.i&quot;] }<br>]</div><div><br></div><div>Here is n=
m output:</div><div><br></div><div>% nm -m lib.macosx-14.0-x86_64-cpython-3=
13/<a href=3D"http://hello.cpython-313-darwin.so" target=3D"_blank">hello.c=
python-313-darwin.so</a> | grep Init<br>0000000000001140 (__TEXT,__text) ex=
ternal _PyInit__hello<br>0000000000004120 (__DATA,__data) non-external _PyI=
nit__hello.SWIG_module</div></div>
</blockquote></div>

--0000000000008d2d2f062773357f--


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


--===============6910473905956694274==
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

--===============6910473905956694274==--