# Problem loading C++ torch script oeprator from python

**URL:** <https://discuss.pytorch.org/t/problem-loading-c-torch-script-oeprator-from-python/45604>\
**Category:** C++\
**Created:** [May 18, 2019, 9:44pm UTC](https://discuss.pytorch.org/t/problem-loading-c-torch-script-oeprator-from-python/45604 "2019-05-18T21:44:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jviereck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/jviereck/32/12246_2.png) [@jviereck](https://discuss.pytorch.org/u/jviereck)\
**Post date:** [May 18, 2019, 9:44pm UTC](https://discuss.pytorch.org/t/problem-loading-c-torch-script-oeprator-from-python/45604/1 "2019-05-18T21:44:27Z")

</div>

Hi folks,

following the tutorial at [1](https://pytorch.org/tutorials/advanced/torch_script_custom_ops.html), I am trying to implement a simple C++ torch script operator. I’ve installed libtorch [2](https://pytorch.org/cppdocs/installing.html) and managed to compile my module. However, when I am trying to load the operator via

```python
import torch
torch.ops.load_library("/home/jviereck/dev/tools/torch_pinocchio/build/libtorch_pinocchio.so")

```

this is the error I am getting:

```auto
$ python2 ../test/basic.py 
Traceback (most recent call last):
  File "../test/basic.py", line 2, in <module>
    torch.ops.load_library("/home/jviereck/dev/tools/torch_pinocchio/build/libtorch_pinocchio.so")
  File "/home/jviereck/.local/lib/python2.7/site-packages/torch/_ops.py", line 102, in load_library
    ctypes.CDLL(path)
  File "/usr/lib/python2.7/ctypes/ __init__.py", line 362, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: /home/jviereck/dev/tools/torch_pinocchio/build/libtorch_pinocchio.so: undefined symbol: _ZN6caffe26detail37_typeMetaDataInstance_preallocated_29E

```

Note that I’ve installed the torch via `pip install torch`.

My best guess is that the python torch is not aware about the libraries in the libtorch folder. Does someone has an idea how to resolve this?

Best,  
Julian

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [May 18, 2019, 11:13pm UTC](https://discuss.pytorch.org/t/problem-loading-c-torch-script-oeprator-from-python/45604/2 "2019-05-18T23:13:13Z")

</div>

Not sure if that’s related to your problem, but similar issues originated in multiple PyTorch installatons (or some old, unused installs).  
Could you check, if your current environment has some old PyTorch installs (in conda, pip, and build from source)?

---

<div class="post-metadata">

**Author:** ![leowalkling](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/leowalkling/32/11519_2.png) [@leowalkling](https://discuss.pytorch.org/u/leowalkling)\
**Post date:** [May 19, 2019, 5:32pm UTC](https://discuss.pytorch.org/t/problem-loading-c-torch-script-oeprator-from-python/45604/3 "2019-05-19T17:32:38Z")

</div>

My guess is that either

- Your compiler version is not the same as the one that compiled the caffe2 library, in particular differing in its name mangling (mapping C++ namespace+symbol+signature to a unique name in an address table)
- The function you were using somewhere in your extension (caffe2::typeMetaDataInstance\_preallocated) is not exported by the libraries you linked against.

What you can do is check whether you are getting the same error when compiling with the same compiler as was used to compile Caffe2.
