Same Hassle on Ubuntu.
As of a recent change circa a month or two ago my
bpy build also started to fail importing with same error. (Test import from bin folder, after successful build, before install)
>>> from bin import bpy
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: dynamic module does not define module export function (PyInit_bpy)
As the error message suggests, and confirmed by running
$ nm -D bpy.so | grep PyInit_bpy
returning nada, whereas would expect something like
0000000002146787 T PyInit_bpy
Getting the result above and finally importing were woohoo moments indeed.
This came after searching high and low, throwing back quite a few red herrings, grinding through numerous updates, and so many re-builds, finally came across a couple of leads on stack overflow.
You need to either use a plain C compiler, or force C linkage
throughout your module with an extern "C" directive:
leading me to the python link flags. Image from
The generated value for cmake entry
-Xlinker --export-dynamic -Xlinker -init=PyInit_bpy
fixed the issue for me and I can now import
bpy in a python 3.8 shell.
Python 3.8.2 (default, Apr 2 2020, 03:24:36)
[GCC 7.5.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import bpy
I feel tho, that this is a bit "hackish".. anyone reading with a "cleaner" way to do same please edit into answer or add comment.