Loading a shared C/C++ library depending on libtorch immediately before importing torch

I have a shared library named liblodepng.dylib.
depending on libtorch. It is mixed C and C++ and depends on libtorch. The following code crashes python:

import ctypes
lib_projn = ctypes.CDLL( “/build/liblodepng.dylib”)
import torch

Changing the order like this gives me an error message:

import torch
lib_projn = ctypes.CDLL(
… “/build/liblodepng.dylib”
… )
Traceback (most recent call last):
File “”, line 1, in
File “/usr/local/Cellar/python@3.9/3.9.6/Frameworks/Python.framework/Versions/3.9/lib/python3.9/ctypes/init.py”, line 374, in init
self._handle = _dlopen(self._name, mode)
OSError: dlopen(/build/liblodepng.dylib, 6): Symbol not found: __ZNK3c1010TensorImpl20is_contiguous_customENS_12MemoryFormatE
Referenced from: /usr/local/opt/libtorch/lib/libtorch_cpu.dylib
Expected in: /usr/local/lib/python3.9/site-packages/torch/lib/libc10.dylib
in /usr/local/opt/libtorch/lib/libtorch_cpu.dylib