Solving Python Error – Symbol not found. Expected in: flat namespace.

Symbol not found. Expected in: flat namespace. is an error which occurs when importing custom modules who have objects compiled with libc++ and objects compiled with libstdc++.

In this article I am going to help you solve this error and understand the root of the problem, also I am presenting other possible solutions that may work if the main solution does not work for you.

Exploring the Error : Symbol not found. Expected in: flat namespace.

This error occurs when importing custom modules who have objects compiled with libc++ and objects compiled with libstdc++.

The error message bellow should be the same as the error message you are having. Beware of mixing between errors.

                                                                       #
File "<string>", line 1, in <module>
ImportError: dlopen(./myMod.so, 2): Symbol not found: __ZN5boost6python7objects15function_objectERKNS1
_11py_functionERKSt4pairIPKNS0_6detail7keywordES9_E
Referenced from: ./myMod.so
Expected in: flat namespace
in ./myMod.so
                                                                       #

Bellow are the solutions which have worked for me and will help you to successfully solve your issue.

Solution 1 : remove native python and work with python virtualenv

This solution is only for people with pyenv, if you do not have pyenv skip to the next solution.

If you are using pyenv or python virtualenv and you do not need your native python installation, what might fix your problem is entirely removing your native python installation.

If removing native python and working with virtual env did not fix the issue, try the solution bellow.

Solution 2 : recompile the libraries

Mixing two version of the same API is a very dangerous thing to do, this error occurs mostly because of mixing objects compiled with libc++ and those compiled with libstdc++.

The solution is to match the libraries by recompiling them and making them match each other.

I hope this solution solved your problem, if that is not the case try the solution bellow.

Solution 3 : Link as a static library

If the solutions above did not work you should try this solution.

Link boost-python in our particular case or any other package to your library as a static library.

Even if your machine doesn’t have the package installed (In this case boost-python installed).

I hope this solution solved your problem and I hope this guide solved your issue, thank you for reading.

Summing-up

The end of this article is here, please make sure to support the team by clicking the Kofi button, it is the red button on the top of this page, it is for donations and will help us help more developers in our error solving journey. Please keep learning and keep coding, cheers.

If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/