flet icon indicating copy to clipboard operation
flet copied to clipboard

flet==0.25.0.dev3526 "Android" Apk Does not run with Numpy "Emulator"

Open jtoror opened this issue 1 year ago • 11 comments

Duplicate Check

  • [X] I have searched the opened issues and there are no duplicates

Describe the bug

"Android" Apk Does not run with Numpy "Emulator"

Code sample

Code

pyproject.toml

[project]
name = "amy_app"
version = "1.0.0"
description = "My first Flet project"
authors = [
    {name = "John Smith", email = "[email protected]"}
]

dependencies = [
    "flet==0.25.0.dev3526",
    "numpy",
    "pandas",
    "imagekitio",
    "supabase",
    "Pillow",
    "libjpeg",
    "plotly"
]

main.py

import os
import flet as ft
import numpy as np
import pandas as pd
from imagekitio import ImageKit
from supabase import create_client, Client
from PIL import Image
import plotly.express as px


def main(page: ft.Page):
    page.title = "Flet counter example"
    page.vertical_alignment = ft.MainAxisAlignment.CENTER

    txt_number = ft.TextField(
        value="0", text_align=ft.TextAlign.RIGHT, width=100)

    def minus_click(e):
        txt_number.value = str(int(txt_number.value) - 1)
        page.update()

    def plus_click(e):
        txt_number.value = str(int(txt_number.value) + 1)
        page.update()

    page.add(
        ft.Row(
            [
                ft.IconButton(ft.icons.REMOVE, on_click=minus_click),
                txt_number,
                ft.IconButton(ft.icons.ADD, on_click=plus_click),
            ],
            alignment=ft.MainAxisAlignment.CENTER,
        )
    )


ft.app(main)


To reproduce

Traceback (most recent call last): File "/data/user/0/com.flet.amy_app/files/flet/python_site_packages/numpy/_core/init.py", line 23, in from . import multiarray File "/data/user/0/com.flet.amy_app/files/flet/python_site_packages/numpy/_core/multiarray.py", line 10, in from . import overrides File "/data/user/0/com.flet.amy_app/files/flet/python_site_packages/numpy/_core/overrides.py", line 8, in from numpy._core._multiarray_umath import ( ImportError: dlopen failed: library "libc++_shared.so" not found: needed by /data/data/com.flet.amy_app/files/flet/python_site_packages/numpy/_core/_multiarray_umath.cpython-312.so in namespace classloader-namespace

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/data/user/0/com.flet.amy_app/files/flet/python_site_packages/numpy/init.py", line 114, in from numpy.config import show as show_config File "/data/user/0/com.flet.amy_app/files/flet/python_site_packages/numpy/config.py", line 4, in from numpy._core._multiarray_umath import ( File "/data/user/0/com.flet.amy_app/files/flet/python_site_packages/numpy/_core/init.py", line 49, in raise ImportError(msg) ImportError:

IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!

Importing the numpy C-extensions failed. This error can happen for many reasons, often due to issues with your setup or how NumPy was installed.

We have compiled some common reasons and troubleshooting tips at:

https://numpy.org/devdocs/user/troubleshooting-importerror.html

Please note and check the following:

  • The Python version is: Python3.12 from ""
  • The NumPy version is: "2.1.1"

and make sure that they are the versions you expect. Please carefully study the documentation linked above for further help.

Original error was: dlopen failed: library "libc++_shared.so" not found: needed by /data/data/com.flet.amy_app/files/flet/python_site_packages/numpy/_core/_multiarray_umath.cpython-312.so in namespace classloader-namespace

The above exception was the direct cause of the following exception:

Traceback (most recent call last): File "", line 43, in File "", line 229, in run_module File "", line 88, in _run_code File "/data/user/0/com.flet.amy_app/files/flet/app/main.py", line 3, in import numpy as np File "/data/user/0/com.flet.amy_app/files/flet/python_site_packages/numpy/init.py", line 119, in raise ImportError(msg) from e ImportError: Error importing numpy: you should not try to import numpy from its source directory; please exit the numpy source tree, and relaunch your python interpreter from there.

Expected behavior

No response

Screenshots / Videos

Captures

image

Operating System

Linux

Operating system details

Kubuntu 24.04

Flet version

0.25.0.dev3526

Regression

No, it isn't

Suggestions

No response

Logs

Logs
[Paste your logs here]

Additional details

No response

jtoror avatar Oct 20 '24 19:10 jtoror

libc++_shared.so has been added to Flet's Python distro and I've got both Numpy and Pandas working in Android Emulator - see recording below. Will test on a real device later.

I'm running on Resizable_Experimental_API_34.

https://github.com/user-attachments/assets/bdca8e21-0f71-43b7-bb5e-f9444423642d

App code:

import flet as ft


def main(page: ft.Page):
    page.title = "Flet libs test"

    def numpy_tests(e):
        def test_basic():
            try:
                from numpy import array

                assert (array([1, 2]) + array([3, 5])).tolist() == [4, 7]

                page.add(ft.Text("numpy: test_basic - OK"))
            except Exception as e:
                page.add(ft.Text(f"numpy: test_basic - error: {e}"))

        def test_performance():
            try:
                from time import time

                import numpy as np

                start_time = time()
                SIZE = 500
                a = np.random.rand(SIZE, SIZE)
                b = np.random.rand(SIZE, SIZE)
                np.dot(a, b)

                # With OpenBLAS, the test devices take at most 0.4 seconds. Without OpenBLAS, they take
                # at least 1.0 seconds.
                duration = time() - start_time
                page.add(
                    ft.Text(f"numpy: test_performance - OK, duration: {duration:.3f}")
                )
                print(f"{duration:.3f}")
                assert duration < 0.7
            except Exception as e:
                page.add(ft.Text(f"numpy: test_performance - error: {e}"))

        test_basic()
        test_performance()

    def pandas_tests(e):
        def test_basic():
            try:
                from pandas import DataFrame

                df = DataFrame(
                    [("alpha", 1), ("bravo", 2), ("charlie", 3)],
                    columns=["Letter", "Number"],
                )
                assert df.to_csv() == (
                    ",Letter,Number\n" "0,alpha,1\n" "1,bravo,2\n" "2,charlie,3\n"
                )

                page.add(ft.Text("pandas: test_basic - OK"))
            except Exception as e:
                page.add(ft.Text(f"pandas: test_basic - error: {e}"))

        test_basic()

    page.add(
        ft.SafeArea(
            ft.Column(
                [
                    ft.ElevatedButton("Run Numpy tests", on_click=numpy_tests),
                    ft.ElevatedButton("Run Pandas tests", on_click=pandas_tests),
                ]
            )
        )
    )


ft.app(main)

pyproject.toml:

[project]

name = "numpy_app"
version = "1.0.0"
description = ""

authors = [
{name = "Flet Developer", email = "[email protected]"}
]

dependencies = ["flet==0.25.0.dev3526", "numpy", "pandas"]

[tool.flet]
template.ref = "0.25.0-dev"
android.adaptive_icon_background = "#ffffff"

[tool.poetry]

name = "my_app"
version = "1.0.0"
description = "My first Flet project"
authors = ["Feodor Fitsner"]

[tool.poetry.dependencies]
python = "^3.8"
flet = "0.25.0.dev3526"
numpy = "^2.0.0"
pandas = "^2.2.2"

FeodorFitsner avatar Oct 21 '24 19:10 FeodorFitsner

image

jtoror avatar Oct 22 '24 01:10 jtoror

What emulator is that? What's host machine/OS, ARM or Intel?

FeodorFitsner avatar Oct 22 '24 01:10 FeodorFitsner

What emulator is that? What's host machine/OS, ARM or Intel?

On my phone a MOTOe5 plus https://www.gsmarena.com/motorola_moto_e5_plus-9095.php

host Intel x64

jtoror avatar Oct 22 '24 01:10 jtoror

I deleted my .venv and build folder, created my virtual environment from scratch and created the new build.

jtoror avatar Oct 22 '24 01:10 jtoror

Will check on a real device tomorrow.

FeodorFitsner avatar Oct 22 '24 02:10 FeodorFitsner

Will check on a real device tomorrow.

I just tried it on the emulator and it works. But it doesn't work on my physical device. Tomorrow I'll try it on a tablet to see if it works. Thanks.

jtoror avatar Oct 22 '24 03:10 jtoror

I've just tried on Galaxy Tab A8 (SM X200) and it worked. It runs on Android 13.0

Screenshot_20241021_203356

I also uploaded my APK it case you want to test it on your phone: https://github.com/FeodorFitsner/flet/releases/download/downloads/app-release.apk

FeodorFitsner avatar Oct 22 '24 03:10 FeodorFitsner

OK, I've just tested on a real phone with Android 14 and then on emulator with Android 8 and both numpy and pandas worked:

Screenshot_1729614803

Screenshot_1729614856

FeodorFitsner avatar Oct 22 '24 16:10 FeodorFitsner

OK, I've just tested on a real phone with Android 14 and then on emulator with Android 8 and both numpy and pandas worked:

Thanks Feodor, it's likely that my phone has something strange, the PIL error doesn't let me advance in my project, another thing is it possible that the first execution of the apk after installing it takes less time than it currently takes on physical devices?

jtoror avatar Oct 22 '24 18:10 jtoror

I'm working on PIL issue, thanks for your patience. Re: first time run - will be looking into that too.

FeodorFitsner avatar Oct 22 '24 19:10 FeodorFitsner

PIL has been fixed. I cannot reproduce errors from this issue.

FeodorFitsner avatar Nov 06 '24 18:11 FeodorFitsner