Shawn Zamperini
Shawn Zamperini

Reputation: 37

Error using ctypes.CDLL on shared library in python

I have a shared library file part of a larger program. I seem to get many of these errors with all the shared libraries, so I think I've narrowed down my issue to something to do with this.

I have a file called libMdsdcl.so. Just for testing purposes I've placed it in a folder by itself and ran the following lines to repeat the error:

In [1]: import ctypes as c
In [2]: from ctypes.util import find_library
In [3]: name = 'Mdsdcl'
In [4]: libname = find_library(name)
In [5]: c.CDLL(libname)
---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
<ipython-input-6-7c40f200b197> in <module>()
----> 1 c.CDLL(libname)

/usr/lib/python3.6/ctypes/__init__.py in __init__(self, name, mode, handle, use_errno, use_last_error)
    346 
    347         if handle is None:
--> 348             self._handle = _dlopen(self._name, mode)
    349         else:
    350             self._handle = handle

OSError: libreadline.so.6: cannot open shared object file: No such file or directory

This isn't my code, but rather it is part of a code that seems to work for everyone who installs it. Based off other similar questions, I checked the architecture of my python installation and the file, and they're both 64-bit. Could something be wrong with my libreadline or something? I actually just upgraded to Ubuntu 18.04 right before this too.

For reference this is MDSplus on the tiny, tiny off chance someone seeing this is familiar with it.

Upvotes: 1

Views: 7110

Answers (1)

Arthur Dent
Arthur Dent

Reputation: 1952

You are not showing how you populate name in:

libname = c.CDLL(name)

You may need to ensure you start from the correct directory. os.path.dirname(__file__) will give you this correct directory.

But usually in this context I see something like:

libname = cdll.LoadLibrary(os.path.abspath("libreadline.so.6"))

I think the issue is your path since you don't give the actual extension of the shared object. Try:

name = "libMdsdcl.so"  # or whatever it is called

Just some ideas since I can't tell for sure based on the limited information shown.

Upvotes: 3

Related Questions