Tamir
Tamir

Reputation: 2533

Unable to load library from the current folder in Python

I have very straight forward code:

import ctypes
import os
dll_name = "./mylib.so"
print ctypes.cdll.LoadLibrary(os.path.abspath(dll_name))

Both myprog.py and mylib.so are in the same folder:

me@host:~/test $ pwd
/home/me/test
me@host:~/test $ ls -a
.   myprog.py       mylib.so

Library is loadble:

me@host:~/test $ readelf -h mylib.so
ELF Header:
  Magic:   7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00
  Class:                             ELF32
  Data:                              2's complement, little endian
  Version:                           1 (current)
  OS/ABI:                            UNIX - System V
  ABI Version:                       0
  Type:                              DYN (Shared object file)
  Machine:                           ARM
  Version:                           0x1
  Entry point address:               0x0
  Start of program headers:          52 (bytes into file)
  Start of section headers:          385780 (bytes into file)
  Flags:                             0x5000000, Version5 EABI
  Size of this header:               52 (bytes)
  Size of program headers:           32 (bytes)
  Number of program headers:         7
  Size of section headers:           40 (bytes)
  Number of section headers:         23
  Section header string table index: 22

But I am still not able to load the library

me@host:~/test $ python myprog.py
Traceback (most recent call last):
  File "myprog.py", line 4, in <module>
    print ctypes.cdll.LoadLibrary(os.path.abspath(dll_name))
  File "/usr/lib/python2.7/ctypes/__init__.py", line 440, in LoadLibrary
    return self._dlltype(name)
  File "/usr/lib/python2.7/ctypes/__init__.py", line 362, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: /home/me/test/mylib.so: cannot open shared object file: No such file or directory

What I am doing wrong?

Upvotes: 2

Views: 2126

Answers (1)

Nicholas Shaffer
Nicholas Shaffer

Reputation: 80

From my research the most logical error is there is a dependent module found within

 __init__ self.handle = _dlopen(self._name, mode) 

that is not within the same folder as your active DIR. The only other possibility I can think of is self._name is a string variable that is passed a name of a file that is not found in the active DIR.

Also, confirm that /home/me/test/mylib.so is a true directory, I only mention this because .so would describe a file type. so it may be that the file you are looking for is a different type of file. All of these instances are something you want to check are accurate when receiving this type of error.

Upvotes: 1

Related Questions