flet-dev / flet

Flet enables developers to easily build realtime web, mobile and desktop apps in Python. No frontend experience required.
https://flet.dev
Apache License 2.0
9.59k stars 372 forks source link

Flet build error with opencv cv2. #2714

Open blackCmd opened 3 months ago

blackCmd commented 3 months ago
  1. I made an app using flet create myapp.
  2. Added opencv-python to requirements.txt in myapp directory.
  3. I made a desktop app using flet build windows ./myapp.
  4. When I run the created app, I get an error as shown in the picture below.

image image

ENV.

Windows10 flet 0.21 and 0.22

lekshmanmj commented 3 months ago

please try just by giving opencv instead of opencv-python in requirements.txt, and let me know if it helps you solve the issue.

ivangermes commented 2 months ago

IMHO This is because packaging removes the "py" files, leaving only the "pyc" files.

As a quick fix, you can add config.py and config3.py to data/flutter_assets/app/app.zip/__pypackages__/cv2/ manually. ( you can take these files from site-packages/cv2/ ).

Update: You need to add config.py and config3.py to resulted app.zip. This app.zip is created after the build in build/YOUR_PLATFORM/data/flutter_assets/app/. The path in app.zip to put the files is /__pypackages__/cv2/.

AltugEngin commented 2 months ago

please try just by giving opencv instead of opencv-python in requirements.txt, and let me know if it helps you solve the issue.

exactly same issue for me too... Unfortunately it didn't work for me

AltugEngin commented 2 months ago

Traceback (most recent call last): File "", line 40, in File "", line 229, in run_module File "", line 88, in _run_code File "/tmp/serious_python_tempBWLPXY/main.py", line 2, in File "/tmp/serious_python_tempBWLPXY/pypackages/cv2/init.py", line 181, in File "/tmp/serious_python_tempBWLPXY/pypackages/cv2/init.py", line 111, in bootstrap File "/tmp/serious_python_tempBWLPXY/pypackages/cv2/init.py", line 109, in load_first_config ImportError: OpenCV loader: missing configuration file: ['config.py']. Check OpenCV installation.

AltugEngin commented 2 months ago

IMHO This is because packaging removes the "py" files, leaving only the "pyc" files.

As a quick fix, you can add config.py and config3.py to data/flutter_assets/app/app.zip/__pypackages__/cv2/ manually. ( you can take these files from site-packages/cv2/ ).

it didn't work neither

blackCmd commented 2 months ago

IMHO This is because packaging removes the "py" files, leaving only the "pyc" files.

As a quick fix, you can add config.py and config3.py to data/flutter_assets/app/app.zip/__pypackages__/cv2/ manually. ( you can take these files from site-packages/cv2/ ).

@ivangermes It didnt work...

blackCmd commented 2 months ago

please try just by giving opencv instead of opencv-python in requirements.txt, and let me know if it helps you solve the issue.

@lekshmanmj Yes I tried but it didn't work.

avagst3 commented 1 month ago

Hi, I just had the same error. I use flet 0.22 and opencv 4.9.0.80. manually add the file into the source folder don't work, I also tried to add the folder with .py transform in .pyi but it still doesn't work. Anything new about this problem?

ivangermes commented 3 weeks ago

@AltugEngin

it didn't work neither

@blackCmd

It didnt work...

@avagst3

You need to add config.py and config3.py to resulted app.zip. This app.zip is created after the build in build/YOUR_PLATFORM/data/flutter_assets/app/. The path in app.zip to put the files is /__pypackages__/cv2/.

EmmanuelMMontesinos commented 2 weeks ago

@ivangermes Thank you very much! I had the same error and it was driving me crazy (I'm a newbie). Then I got this error:

RuntimeError: cannot cache function '_make_tree': no locator available for file 'C:\\Users\\My_user\\AppData\\Local\\Temp\\serious_python_temp32194b5c\\__pypackages__\\pymatting\\util\\kdtree.py'

But applying your solution with those dependencies (copy them where they belong in app.zip everything goes perfect.) 💯

Edit: As the process can be tedious and confusing, I created this tool (with flet XD) It's ugly as hell but it works! The only doubt I have is if it could be a bad practice to include the dependencies like this...

I look forward to hearing your answers Flet Package Loader

avagst3 commented 2 weeks ago

@ivangermes I add the file as you say but .exe still use the same Temp folder (so my change is not applyed). Any solution ?

EmmanuelMMontesinos commented 2 weeks ago

@avagst3 apply it to the generated Flet build It is in your_project_flet: \build\windows\data\flutter_assets\app\app.zip But if you have several dependencies that need this solution I recommend the tool I have created with the solution of @ivangermes Flet Package Loader

ivangermes commented 2 weeks ago

@EmmanuelMMontesinos I use a similar automation. I made a simple bash/python script to add a file to zip. And use it in my CI/CD system.

function add_file_2_zip {
python3 -c "
import zipfile as zf, sys
with zf.ZipFile(sys.argv[1], 'a') as z:
    z.write(sys.argv[2], sys.argv[3])
" $1 $2 $3
}

zip_file="/home/user/proj/OpenStitching/build/linux/data/flutter_assets/app/app.zip"
src="/home/user/proj/OpenStitching/venv/lib/python3.11/site-packages/cv2/config.py"
dst="/__pypackages__/cv2/config.py"

add_file_2_zip $zip_file $src $dst
Vaibhavsun commented 2 weeks ago

Can anyone help me with this error. I am trying to flet build apk and having opencv-python in my project and getting this installing dependency with error vaib

baseplate-admin commented 2 weeks ago

@Vaibhavsun. its not possible to add python modules with c extensions in apk build.

Vaibhavsun commented 2 weeks ago

is there any other similar python library which can be used i just needs to capture using camera and it is easily build with flet apk

baseplate-admin commented 2 weeks ago

is there any other similar python library which can be used i just needs to capture using camera and it is easily build with flet apk

Possibly not. Everything is opencv based these days.

if you want to use flet with opencv on android you have to wait 2-3 months. Entire python ecosystem is moving to android/ios.

FeodorFitsner commented 1 week ago

You don't need to wait for 2-3 months. We are going to provide pre-built opencv packages with other popular packages in the first order: https://flet.dev/roadmap#packaging

TaisukeMorimoto commented 3 days ago

You don't need to wait for 2-3 months. We are going to provide pre-built opencv packages with other popular packages in the first order: https://flet.dev/roadmap#packaging

I am really looking forward to it!

Vaibhavsun commented 3 days ago

Actually when i was trying with pygrabber which is i think a pure python package then also it is not building and stuck with extracting ? image

image

baseplate-admin commented 3 days ago

@Vaibhavsun that's because pygrabber uses opencv

blackCmd commented 2 days ago

It has been several months since I posted. It still hasn't been resolved.