Termux Linux 中python文件无法导入.so文件的解决方案

文章描述了一个在Termux环境中遇到的问题,即使用Cython将Python文件编译成.so动态链接库后,尝试导入失败,错误提示为找不到库文件。作者已经尝试过修改环境变量、使用ctypes和第三方模块导入,以及检查文件路径,但问题依然存在。解决方案是检查文件位置,确保.so文件位于Termux的根目录下,例如/data/data/com.termux/files/home/,并将文件移动到该位置才能成功导入。

问题描述:

将python文件使用cython打包成.so文件之后,使用import Bb.so无法导入,一直显示OSError: dlopen failed: library "Bb.cpython-311.so" not found

已经尝试过:

  • 动态链路径放到环境变量中,【无效】
  • 使用ctypes模块导入so文件 【无效】
  • 使用其他第三方模块导入 【无效】
  • 更换名称 Bb.so 【无效】
  • 检查了100遍路径问题 【没任何问题,还是不行】

解决方案

  1. 检查路径,是否在非termux专属文件夹下。
  2. 将整个项目文件复制到termux的根目录下,即 /data/data/com.termux/files/home/中即可。

举例说明:
我的.so 文件放在 /data/data/com.termux/files/home/storage/downloads/myobj文件夹下了,我需要把 myobj cp到home下,运行下面指令就可以顺利的imoprt *.so了。

cp /data/data/com.termux/files/home/storage/downloads/myobj ~/

------------禁止转载------------

$ make -j$(nproc) && make install; Running code to generate profile data (this can take a while): # First, we need to create a clean build with profile generation # enabled. make profile-gen-stamp make[1]: Entering directory '/data/data/com.termux/files/home/Python-3.10.13' Building with support for profile generation: make build_all_generate_profile make[2]: Entering directory '/data/data/com.termux/files/home/Python-3.10.13' make build_all CFLAGS_NODIST=" -fprofile-generate" LDFLAGS_NODIST=" -fprofile-generate" LIBS="-lcrypt -ldl -lm" make[3]: Entering directory '/data/data/com.termux/files/home/Python-3.10.13' gcc -c -fno-strict-aliasing -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -fPIC -D__ANDROID_API__=24 -fPIC -D__ANDROID_API__=24 -fno-semantic-interposition -std=c99 -Werror=implicit-function-declaration -fvisibility=hidden -fprofile-generate -I./Include/internal -I. -I./Include -I/data/data/com.termux/files/usr/include -fPIC -DPy_BUILD_CORE -o Python/bootstrap_hash.o Python/bootstrap_hash.c gcc -c -fno-strict-aliasing -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -fPIC -D__ANDROID_API__=24 -fPIC -D__ANDROID_API__=24 -fno-semantic-interposition -std=c99 -Werror=implicit-function-declaration -fvisibility=hidden -fprofile-generate -I./Include/internal -I. -I./Include -I/data/data/com.termux/files/usr/include -fPIC -DPy_BUILD_CORE -o Python/symtable.o Python/symtable.c gcc -c -fno-strict-aliasing -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -fPIC -D__ANDROID_API__=24 -fPIC -D__ANDROID_API__=24 -fno-semantic-interposition -std=c99 -Werror=implicit-function-declaration -fvisibility=hidden -fprofile-generate -I./Include/internal -I. -I./Include -I/data/data/com.termux/files/usr/include -fPIC -DPy_BUILD_CORE \ -DABIFLAGS='""' \ -DMULTIARCH=\"aarch64-linux-android\" \ -o Python/sysmodule.o ./Python/sysmodule.c gcc -c -fno-strict-aliasing -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -fPIC -D__ANDROID_API__=24 -fPIC -D__ANDROID_API__=24 -fno-semantic-interposition -std=c99 -Werror=implicit-function-declaration -fvisibility=hidden -fprofile-generate -I./Include/internal -I. -I./Include -I/data/data/com.termux/files/usr/include -fPIC -DPy_BUILD_CORE -o Python/thread.o Python/thread.c gcc -c -fno-strict-aliasing -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -fPIC -D__ANDROID_API__=24 -fPIC -D__ANDROID_API__=24 -fno-semantic-interposition -std=c99 -Werror=implicit-function-declaration -fvisibility=hidden -fprofile-generate -I./Include/internal -I. -I./Include -I/data/data/com.termux/files/usr/include -fPIC -DPy_BUILD_CORE -o Python/traceback.o Python/traceback.c CANNOT LINK EXECUTABLE "gcc": library "libxml2.so.16" not found: needed by /data/data/com.termux/files/usr/lib/libLLVM.so in namespace (default) make[3]: *** [Makefile:1856: Python/bootstrap_hash.o] Error 1 make[3]: *** Waiting for unfinished jobs....CANNOT LINK EXECUTABLE "gcc": library "libxml2.so.16" not found: needed by /data/data/com.termux/files/usr/lib/libLLVM.so in namespace (default) make[3]: *** [Makefile:1856: Python/symtable.o] Error 1 CANNOT LINK EXECUTABLE "gcc": library "libxml2.so.16" not found: needed by /data/data/com.termux/files/usr/lib/libLLVM.so in namespace (default) make[3]: *** [Makefile:1856: Python/thread.o] Error 1 CANNOT LINK EXECUTABLE "gcc": library "libxml2.so.16" not found: needed by /data/data/com.termux/files/usr/lib/libLLVM.so in namespace (default) make[3]: *** [Makefile:839: Python/sysmodule.o] Error 1 CANNOT LINK EXECUTABLE "gcc": library "libxml2.so.16" not found: needed by /data/data/com.termux/files/usr/lib/libLLVM.so in namespace (default) make[3]: *** [Makefile:1856: Python/traceback.o] Error 1 make[3]: Leaving directory '/data/data/com.termux/files/home/Python-3.10.13' make[2]: *** [Makefile:531: build_all_generate_profile] Error 2 make[2]: Leaving directory '/data/data/com.termux/files/home/Python-3.10.13' make[1]: *** [Makefile:507: profile-gen-stamp] Error 2 make[1]: Leaving directory '/data/data/com.termux/files/home/Python-3.10.13' make: *** [Makefile:519: profile-run-stamp] Error 2
最新发布
12-03
Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/data/data/com.termux/files/usr/lib/python3.12/site-packages/akshare/__init__.py", line 4642, in <module> from akshare.stock_feature.stock_a_pe_and_pb import ( File "/data/data/com.termux/files/usr/lib/python3.12/site-packages/akshare/stock_feature/stock_a_pe_and_pb.py", line 320, in <module> js_functions = py_mini_racer.MiniRacer() ^^^^^^^^^^^^^^^^^^^^^^^^^ File "/data/data/com.termux/files/usr/lib/python3.12/site-packages/py_mini_racer/py_mini_racer.py", line 185, in __init__ self.__class__.ext = _build_ext_handle() ^^^^^^^^^^^^^^^^^^^ File "/data/data/com.termux/files/usr/lib/python3.12/site-packages/py_mini_racer/py_mini_racer.py", line 134, in _build_ext_handle _ext_handle = ctypes.CDLL(EXTENSION_PATH) ^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/data/data/com.termux/files/usr/lib/python3.12/ctypes/__init__.py", line 379, in __init__ self._handle = _dlopen(self._name, mode) ^^^^^^^^^^^^^^^^^^^^^^^^^ OSError: dlopen failed: library "libpthread.so.0" not found: needed by /data/data/com.termux/files/usr/lib/python3.12/site-packages/py_mini_racer/armlibmini_racer.glibc.so in namespace (default) >>> data = ak.stock_zh_a_hist(symbol="000001", period="daily") Exception ignored in: <function MiniRacer.__del__ at 0x71789ea5c0> Traceback (most recent call last): File "/data/data/com.termux/files/usr/lib/python3.12/site-packages/py_mini_racer/py_mini_racer.py", line 322, in __del__ self.ext.mr_free_context(getattr(self, "ctx", None)) ^^^^^^^^^^^^^^^^^^^^^^^^ AttributeError: 'NoneType' object has no attribute 'mr_free_context' Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'ak' is not defined >>> print(data.head())
03-16
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值