Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: can't import cv2 #23031

Closed
Ar57m opened this issue Jan 25, 2025 · 21 comments · Fixed by #23067
Closed

[Bug]: can't import cv2 #23031

Ar57m opened this issue Jan 25, 2025 · 21 comments · Fixed by #23067
Labels
bug report Something is not working properly system-level bug Bug is hidden somewhere in OS and can't be fixed

Comments

@Ar57m
Copy link

Ar57m commented Jan 25, 2025

Problem description

Got this this error, I reinstalled termux and it persisted. Do I need to install something or is it a bug?

import cv2
  File "/data/data/com.termux/files/usr/lib/python3.12/site-packages/cv2/__init__.py", line 181, in <module>
    bootstrap()                                                 File "/data/data/com.termux/files/usr/lib/python3.12/site-packages/cv2/__init__.py", line 153, in bootstrap                   native_module = importlib.import_module("cv2")
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/data/com.termux/files/usr/lib/python3.12/importlib/__init__.py", line 90, in import_module                        return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                                             ImportError: dlopen failed: cannot locate symbol "eglDestroySyncKHR" referenced by "/system/lib64/libgui.so"...

What steps will reproduce the bug?

import cv2 on a python script

What is the expected behavior?

import and use opencv

System information

termux-info
Termux Variables:
TERMUX_APK_RELEASE=F_DROID
TERMUX_APP_PACKAGE_MANAGER=apt
TERMUX_APP_PID=30038                                          TERMUX_IS_DEBUGGABLE_BUILD=0
TERMUX_MAIN_PACKAGE_FORMAT=debian
TERMUX_VERSION=0.118.1                                        TERMUX__USER_ID=0                                             Packages CPU architecture:
aarch64
Subscribed repositories:
# sources.list
deb https://packages-cf.termux.dev/apt/termux-main/ stable main
# x11-repo (sources.list.d/x11.list)
deb https://packages-cf.termux.dev/apt/termux-x11/ x11 main
Updatable packages:
All packages up to date
termux-tools version:
1.44.6
Android version:
13
Kernel build information:
Linux localhost 5.4.233-qgki-27101688-abM526BXXS5CXE2 #1 SMP PREEMPT Fri May 17 15:23:51 KST 2024 aarch64 Android
Device manufacturer:                                          samsung
Device model:
SM-M526B
LD Variables:
LD_LIBRARY_PATH=
LD_PRELOAD=/data/data/com.termux/files/usr/lib/libtermux-exec.so
@Ar57m Ar57m added bug report Something is not working properly untriaged labels Jan 25, 2025
@Biswa96
Copy link
Member

Biswa96 commented Jan 25, 2025

ImportError: dlopen failed: cannot locate symbol "eglDestroySyncKHR" referenced by "/system/lib64/libgui.so"

It seems to be the Android OS issue specific to the device. Please try to export the variable first LD_PRELOAD=/system/lib64/libEGL.so

@Ar57m
Copy link
Author

Ar57m commented Jan 25, 2025

ImportError: dlopen failed: cannot locate symbol "eglDestroySyncKHR" referenced by "/system/lib64/libgui.so"

It seems to be the Android OS issue specific to the device. Please try to export the variable first LD_PRELOAD=/system/lib64/libEGL.so

tried that and printed this when importing cv2:

CANNOT LINK EXECUTABLE "/data/data/com.termux/files/usr/bin/python": cannot locate symbol "Xzs_Construct" referenced by "/system/lib64/libunwindstack.so"...

no idea why that happens, idk how to locate this one, I'm probably never going to pkg upgrade again lol

@Ar57m
Copy link
Author

Ar57m commented Jan 25, 2025

here more info

~/changes $ termux-info
Termux Variables:
TERMUX_APK_RELEASE=F_DROID
TERMUX_APP_PACKAGE_MANAGER=apt
TERMUX_APP_PID=31945
TERMUX_IS_DEBUGGABLE_BUILD=0
TERMUX_MAIN_PACKAGE_FORMAT=debian
TERMUX_VERSION=0.118.1
TERMUX__USER_ID=0
Packages CPU architecture:
aarch64
Subscribed repositories:
# sources.list
deb https://mirrors.ravidwivedi.in/termux/termux-main stable main
# x11-repo (sources.list.d/x11.list)
deb https://mirrors.ravidwivedi.in/termux/termux-x11 x11 main
Updatable packages:
All packages up to date
termux-tools version:
1.44.6
Android version:
13
Kernel build information:
Linux localhost 5.4.233-qgki-27101688-abM526BXXS5CXE2 #1 SMP PREEMPT Fri May 17 15:23:51 KST 2024 aarch64 Android
Device manufacturer:
samsung
Device model:
SM-M526B
LD Variables:
LD_LIBRARY_PATH=
LD_PRELOAD=/system/lib64/libEGL.so
~/changes $ printenv
SHELL=/data/data/com.termux/files/usr/bin/bash
COLORTERM=truecolor
HISTCONTROL=ignoreboth
PREFIX=/data/data/com.termux/files/usr
TERMUX_IS_DEBUGGABLE_BUILD=0
TERMUX_MAIN_PACKAGE_FORMAT=debian
PWD=/data/data/com.termux/files/home/changes
TERMUX_VERSION=0.118.1
EXTERNAL_STORAGE=/sdcard
LD_PRELOAD=/system/lib64/libEGL.so
HOME=/data/data/com.termux/files/home
LANG=en_US.UTF-8
TERMUX_APK_RELEASE=F_DROID
DEX2OATBOOTCLASSPATH=/apex/com.android.art/javalib/core-oj.jar:/apex/com.android.art/javalib/core-libart.jar:/apex/com.android.art/javalib/okhttp.jar:/apex/com.android.art/javalib/bouncycastle.jar:/apex/com.android.art/javalib/apache-xml.jar:/system/framework/framework.jar:/system/framework/framework-graphics.jar:/system/framework/ext.jar:/system/framework/telephony-common.jar:/system/framework/voip-common.jar:/system/framework/ims-common.jar:/system/framework/knoxsdk.jar:/system/framework/SmpsManager.jar:/system/framework/uibc_java.jar:/system/framework/esecomm.jar:/system/framework/tcmiface.jar:/system/framework/telephony-ext.jar:/system/framework/QPerformance.jar:/system/framework/UxPerformance.jar:/apex/com.android.i18n/javalib/core-icu4j.jar
TMPDIR=/data/data/com.termux/files/usr/tmp
ANDROID_DATA=/data
TERM=xterm-256color
ANDROID_I18N_ROOT=/apex/com.android.i18n
SHLVL=1
ANDROID_ROOT=/system
BOOTCLASSPATH=/apex/com.android.art/javalib/core-oj.jar:/apex/com.android.art/javalib/core-libart.jar:/apex/com.android.art/javalib/okhttp.jar:/apex/com.android.art/javalib/bouncycastle.jar:/apex/com.android.art/javalib/apache-xml.jar:/system/framework/framework.jar:/system/framework/framework-graphics.jar:/system/framework/ext.jar:/system/framework/telephony-common.jar:/system/framework/voip-common.jar:/system/framework/ims-common.jar:/system/framework/knoxsdk.jar:/system/framework/SmpsManager.jar:/system/framework/uibc_java.jar:/system/framework/esecomm.jar:/system/framework/tcmiface.jar:/system/framework/telephony-ext.jar:/system/framework/QPerformance.jar:/system/framework/UxPerformance.jar:/apex/com.android.i18n/javalib/core-icu4j.jar:/apex/com.android.adservices/javalib/framework-adservices.jar:/apex/com.android.adservices/javalib/framework-sdksandbox.jar:/apex/com.android.appsearch/javalib/framework-appsearch.jar:/apex/com.android.btservices/javalib/framework-bluetooth.jar:/apex/com.android.conscrypt/javalib/conscrypt.jar:/apex/com.android.ipsec/javalib/android.net.ipsec.ike.jar:/apex/com.android.media/javalib/updatable-media.jar:/apex/com.android.mediaprovider/javalib/framework-mediaprovider.jar:/apex/com.android.ondevicepersonalization/javalib/framework-ondevicepersonalization.jar:/apex/com.android.os.statsd/javalib/framework-statsd.jar:/apex/com.android.permission/javalib/framework-permission.jar:/apex/com.android.permission/javalib/framework-permission-s.jar:/apex/com.android.scheduling/javalib/framework-scheduling.jar:/apex/com.android.sdkext/javalib/framework-sdkextensions.jar:/apex/com.android.tethering/javalib/framework-connectivity.jar:/apex/com.android.tethering/javalib/framework-connectivity-t.jar:/apex/com.android.tethering/javalib/framework-tethering.jar:/apex/com.android.uwb/javalib/framework-uwb.jar:/apex/com.android.wifi/javalib/framework-wifi.jar:/apex/com.samsung.android.ipm/javalib/framework-samsung-ipm.jar:/apex/com.samsung.android.shell/javalib/framework-samsung-privilege.jar
ANDROID_TZDATA_ROOT=/apex/com.android.tzdata
TERMUX_APP_PID=31945
PATH=/data/data/com.termux/files/usr/bin
ANDROID_ART_ROOT=/apex/com.android.art
OLDPWD=/data/data/com.termux/files/home
_=/data/data/com.termux/files/usr/bin/printenv

weird that through the command line calling python and importing cv2 works fine

@Ar57m
Copy link
Author

Ar57m commented Jan 26, 2025

forgot to say im running another python script through a subprocess, how can I fix that? was working fine before

@licy183
Copy link
Member

licy183 commented Jan 26, 2025

I can't reproduce it on my device. It is well-known that on some devices the android linker doesn't resolve symbols correctly. apt install libandroid-stub and try again.

@licy183 licy183 added system-level bug Bug is hidden somewhere in OS and can't be fixed and removed untriaged labels Jan 26, 2025
@Ar57m
Copy link
Author

Ar57m commented Jan 26, 2025

I can't reproduce it on my device. It is well-known that on some devices the android linker doesn't resolve symbols correctly. apt install libandroid-stub and try again.

that didn't help unfortunately, here my code which has this problem https://github.com/Ar57m/another_fractal_generator/tree/server
server.py creates a subprocess which runs runner.py which imports tools.py which imports cv2 and uses it on its functions. I got this error after updating everything using pkg, was working fine before 🤔

@Biswa96
Copy link
Member

Biswa96 commented Jan 26, 2025

You could try appending lzma in LD_PRELOAD. liblzma.so have the Xzs_Construct symbol.

LD_PRELOAD=/system/lib64/libEGL.so:/system/lib64/liblzma.so

@Ar57m
Copy link
Author

Ar57m commented Jan 26, 2025

You could try appending lzma in LD_PRELOAD. liblzma.so have the Xzs_Construct symbol.

LD_PRELOAD=/system/lib64/libEGL.so:/system/lib64/liblzma.so

error changed to

ImportError: dlopen failed: cannot locate symbol "glEGLImageTargetTexture2DOES" referenced by "/system/lib64/libgui.so"... 

@licy183
Copy link
Member

licy183 commented Jan 26, 2025

that didn't help unfortunately,

Try not setting LD_PRELOAD (or set it to /data/data/com.termux/files/usr/lib/libtermux-exec.so).

@Ar57m
Copy link
Author

Ar57m commented Jan 26, 2025

I think I may have found the problem, my main script server.py was importing my module tools.py, the same that I'm importing on runner.py(which runs on a subprocess on server.py), after removing it, it magically works fine again, somehow it's conflicting 🤔. Thanks everyone for the help, I think you guys can close the issue.

@Biswa96
Copy link
Member

Biswa96 commented Jan 26, 2025

I think, importing python script should not cause that kind of issue. If there is any error in python script it should be a python error.

@Ar57m
Copy link
Author

Ar57m commented Jan 26, 2025

yeah it's odd let me give you a example code where I hit it:

alpha.py

import subprocess
import gamma #comment it if it crashes

if __name__ == "__main__":
    print("Running beta.py as a subprocess")
    subprocess.run(["python", "beta.py"])
    gamma.debug_message() #comment it if it crashes

beta.py

import gamma

if __name__ == "__main__":
    print("Creating a white image using gamma module")
    gamma.save_image("white_image.jpg", color=(255, 255, 255))

gamma.py

import cv2
import numpy as np

def save_image(filename, color):
    print(f"Saving image: {filename}")
    image = np.full((100, 100, 3), color, dtype=np.uint8)
    cv2.imwrite(filename, image)
    print(f"Image {filename} saved successfully")

def debug_message():
    print("Debug: gamma.py is imported successfully")

@truboxl
Copy link
Contributor

truboxl commented Jan 27, 2025

Reproduce your issue with environment variable LD_DEBUG=1 then post logcat output

@Ar57m
Copy link
Author

Ar57m commented Jan 27, 2025

Reproduce your issue with environment variable LD_DEBUG=1 then post logcat output

here

log_output.txt

@truboxl
Copy link
Contributor

truboxl commented Jan 27, 2025

I can reproduce the issue. Suspect leakage in ffmpeg libavcodec.so.61.19.100 that links libmediandk.so which pulls libs in /system/lib64.

Please run these commands to build a stub libmediandk.so and see if it can resolve your issue:

curl -L https://android.googlesource.com/platform/frameworks/av/+/master/media/ndk/libmediandk.map.txt?format=TEXT | base64 -d > libmediandk.map.txt
for i in $(grep ';' libmediandk.map.txt | grep -vE '}|\*' | cut -d';' -f1 | awk '{ print $1 }' | sort -u); do echo "void $i() {}"; done | tee mediandk.c
cc mediandk.c -o $PREFIX/lib/libmediandk.so -shared

@Ar57m
Copy link
Author

Ar57m commented Jan 27, 2025

I can reproduce the issue. Suspect leakage in ffmpeg libavcodec.so.61.19.100 that links libmediandk.so which pulls libs in /system/lib64.

Please run these commands to build a stub libmediandk.so and see if it can resolve your issue:

curl -L https://android.googlesource.com/platform/frameworks/av/+/master/media/ndk/libmediandk.map.txt?format=TEXT | base64 -d > libmediandk.map.txt
for i in $(grep ';' libmediandk.map.txt | grep -vE '}|*' | cut -d';' -f1 | awk '{ print $1 }' | sort -u); do echo "void $i() {}"; done | tee mediandk.c
cc mediandk.c -o $PREFIX/lib/libmediandk.so -shared

nice that worked really well 🎉
no more errors

python alpha.py
Running beta.py as a subprocess
Creating a white image using gamma module
Saving image: white_image.jpg
Image white_image.jpg saved successfully
Debug: gamma.py is imported successfully

@licy183
Copy link
Member

licy183 commented Jan 27, 2025

Maybe libmediandk.so should be shipped as part of libandroid-stub...

@truboxl
Copy link
Contributor

truboxl commented Jan 27, 2025

Maybe libmediandk.so should be shipped as part of libandroid-stub...

Sounds good to me. I dont have better solution.

@gphg
Copy link

gphg commented Mar 3, 2025

I'm having the same problem while using a Python module.

Traceback (most recent call last):
  File "/data/data/com.termux/files/usr/bin/scenedetect", line 5, in <module>
    from scenedetect.__main__ import main
  File "/data/data/com.termux/files/usr/lib/python3.12/site-packages/scenedetect/__init__.py", line 25, in <module>
    import cv2 as _
  File "/data/data/com.termux/files/usr/lib/python3.12/site-packages/cv2/__init__.py", line 181, in <module>
    bootstrap()
  File "/data/data/com.termux/files/usr/lib/python3.12/site-packages/cv2/__init__.py", line 153, in bootstrap
    native_module = importlib.import_module("cv2")
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/data/data/com.termux/files/usr/lib/python3.12/importlib/__init__.py", line 90, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ImportError: dlopen failed: cannot locate symbol "_ZN4absl12lts_2024011612log_internal9kCharNullE" referenced by "/data/data/com.termux/files/usr/lib/libopencv_dnn.so"...

It was working two days ago. I have no idea why.

Update:
I suspect it has to do with Numpy, as I get this warning on reinstalling opencv-python.

pkg reinstall opencv-python
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
0 upgraded, 0 newly installed, 1 reinstalled, 0 to remove and 0 not upgraded.
Need to get 0 B/1607 kB of archives.
After this operation, 0 B of additional disk space will be used.
(Reading database ... 74484 files and directories currently installed.)
Preparing to unpack .../opencv-python_4.11.0_aarch64.deb ...
Unpacking opencv-python (4.11.0) over (4.11.0) ...
Setting up opencv-python (4.11.0) ...
WARNING: opencv-python is compiled with numpy 2.2.1, but numpy 2.2.3 is installed. Please report it to https://github.com/termux/termux-packages if any bug happens.

@licy183
Copy link
Member

licy183 commented Mar 4, 2025

Revbumping opencv should resolve it. Has been done in e5f01d4.

@gphg
Copy link

gphg commented Mar 4, 2025

Revbumping opencv should resolve it. Has been done in e5f01d4.

Thanks. It is working now. Worth of reporting. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug report Something is not working properly system-level bug Bug is hidden somewhere in OS and can't be fixed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants