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

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

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题描述:

将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 ~/

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

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
$ python Python 3.12.9 (main, Feb 4 2025, 22:30:28) [Clang 18.0.3 (https://android.googlesource.com/toolchain/llvm-project d8003a456 on linux Type "help", "copyright", "credits" or "license" for more information. >>> import akshare as ak 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 178, 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 125, in _build_ext_handle raise RuntimeError("Native library not available at {}".format(EXTENSION_PATH)) RuntimeError: Native library not available at /data/data/com.termux/files/usr/lib/python3.12/site-packages/py_mini_racer/libmini_racer.glibc.so >>> print(ak.__version__) Exception ignored in: <function MiniRacer.__del__ at 0x775999a520> 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 315, 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
03-16
~ $ python Python 3.12.9 (main, Feb 4 2025, 22:30:28) [Clang 18.0.3 (https://android.googlesource.com/toolchain/llvm-project d8003a456 on linux Type "help", "copyright", "credits" or "license" for more information. >>> import akshare as ak 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 178, 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 125, in _build_ext_handle raise RuntimeError("Native library not available at {}".format(EXTENSION_PATH)) RuntimeError: Native library not available at /data/data/com.termux/files/usr/lib/python3.12/site-packages/py_mini_racer/libmini_racer.glibc.so >>> >>> # 使用无JavaScript依赖的接口 >>> data = ak.stock_zh_a_spot() # 实时行情接口 Exception ignored in: <function MiniRacer.__del__ at 0x72eb46a520> 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 315, 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[data['代码'] == '000001']) # 筛选上证 指数 Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'data' is not defined >>> >>> # 历史数据替代方案 >>> data = ak.stock_zh_a_daily(symbol="sh000001", adjust="hfq")
最新发布
03-16
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值