无法从Python中的当前文件夹加载库
我有非常直接的代码:无法从Python中的当前文件夹加载库,python,shared-libraries,ctypes,Python,Shared Libraries,Ctypes,我有非常直接的代码: 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:
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
me@host:~/test$python myprog.py
回溯(最近一次呼叫最后一次):
文件“myprog.py”,第4行,在
打印ctypes.cdll.LoadLibrary(os.path.abspath(dll\u名称))
LoadLibrary中第440行的文件“/usr/lib/python2.7/ctypes/_init__uuu.py”
返回自我类型(名称)
文件“/usr/lib/python2.7/ctypes/_init__.py”,第362行,在_init中__
self.\u handle=\u dlopen(self.\u名称,模式)
OSError:/home/me/test/mylib.so:无法打开共享对象文件:没有这样的文件或目录
我做错了什么?根据我的研究,最符合逻辑的错误是在
__init__ self.handle = _dlopen(self._name, mode)
与活动目录不在同一文件夹中。
我唯一能想到的另一种可能性是self.\u name
是一个字符串变量,它传递了一个在活动目录中找不到的文件名
另外,请确认
/home/me/test/mylib.so
是一个真正的目录,我之所以提到它,是因为.so
将描述一种文件类型。因此,您正在查找的文件可能是另一种类型的文件。所有这些实例都是您希望在收到此类错误时检查是否准确的实例 我已经验证了依赖关系。全部就绪(除了一个,由于某种原因,我无法使用find_library找到它,所以我创建了指向它的链接,现在一切正常)。我已经验证的另一件事是目标CPU名称和arch。这里看起来也不错。那么你的程序现在运行正常了?还是我误解了你。不,不是。我所说的是依赖项和CPU目标是确定的。您是否检查了我在那里输入的关于的最后一点。因此
是一种文件类型,并确保在目录中找到相同的文件类型这很奇怪,请允许我做一些进一步的研究,我将再次发布。这很有趣!同时,请检查您的所有依赖项。。。这也包括导入模块内的所有依赖项。因此,您需要进入您的库,找到“LoadLibrary”模块并检查其中的依赖项。。如果在python目录中移动文件,可能会无意中从其正确目录中删除依赖项