# 问题说明

2. test.so，该so链接了pythonx.x.so，并引入了pybind11库，加载下方的logic.py脚本，调用其一些方法。
3. logic.py，一些python逻辑，import了numpy库。

  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41  terminate called after throwing an instance of 'pybind11::error_already_set' what(): ImportError: IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE! Importing the numpy C-extensions failed. This error can happen for many reasons, often due to issues with your setup or how NumPy was installed. We have compiled some common reasons and troubleshooting tips at: https://numpy.org/devdocs/user/troubleshooting-importerror.html Please note and check the following: * The Python version is: Python3.7 from "/usr/bin/python3" * The NumPy version is: "1.21.2" and make sure that they are the versions you expect. Please carefully study the documentation linked above for further help. Original error was: /home/lighthouse/.local/lib/python3.7/site-packages/numpy/core/_multiarray_umath.cpython-37m-x86_64-linux-gnu.so: undefined symbol: PyExc_ImportError At: /home/lighthouse/.local/lib/python3.7/site-packages/numpy/core/__init__.py(51): (219): _call_with_frames_removed (728): exec_module (677): _load_unlocked (967): _find_and_load_unlocked (983): _find_and_load (219): _call_with_frames_removed (1043): _handle_fromlist /home/lighthouse/.local/lib/python3.7/site-packages/numpy/__init__.py(150): (219): _call_with_frames_removed (728): exec_module (677): _load_unlocked (967): _find_and_load_unlocked (983): _find_and_load Aborted (core dumped) 

# 解决

1. 在编译时，主进程模块显式的链接libpythonx.x.so，这样导出的PyExc_ImportError符号可以被后续加载的其他模块查找到。
2. 在代码中，使用dlopen("libpythonx.x.so", RTLD_LAZY | RTLD_GLOBAL)再次显式的加载libpythonx.x.so模块，其中RTLD_GLOBAL选项可以让libpythonx.x.so中定义的符号被其后打开的其他模块重定位解析使用，这样在加载_multiarray_umath.cpython-37m-x86_64-linux-gnu.so时，也就能查找到PyExc_ImportError符号了。