无法从Python中的当前文件夹加载库

问题描述 投票:1回答:1

我有非常直接的代码:

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

myprog.py和mylib.so都在同一个文件夹中:

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

库是可加载的:

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

但我仍然无法加载库

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

我做错了什么?

python shared-libraries ctypes
1个回答
1
投票

根据我的研究,最合乎逻辑的错误是在其中找到了一个依赖模块

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

与活动DIR不在同一文件夹中。我能想到的唯一另一种可能性是self._name是一个字符串变量,它传递的文件名在活动DIR中找不到。

另外,确认/home/me/test/mylib.so是一个真正的目录,我只提到这个,因为.so会描述一个文件类型。所以你正在寻找的文件可能是不同类型的文件。在收到此类错误时,您要检查的所有这些实例都是准确的。

© www.soinside.com 2019 - 2024. All rights reserved.