flet icon indicating copy to clipboard operation
flet copied to clipboard

Flet build error with opencv cv2.

Open hi-one-gg opened this issue 1 year ago • 23 comments

  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

hi-one-gg avatar Feb 22 '24 08:02 hi-one-gg

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 avatar Feb 22 '24 13:02 lekshmanmj

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/.

ivangermes avatar Mar 10 '24 16:03 ivangermes

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 avatar Mar 30 '24 10:03 AltugEngin

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 avatar Mar 30 '24 10:03 AltugEngin

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

AltugEngin avatar Mar 30 '24 10:03 AltugEngin

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...

hi-one-gg avatar Mar 31 '24 01:03 hi-one-gg

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.

hi-one-gg avatar Mar 31 '24 01:03 hi-one-gg

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?

avagst3 avatar Apr 24 '24 07:04 avagst3

@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/.

ivangermes avatar May 09 '24 02:05 ivangermes

@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

EmmanuelMMontesinos avatar May 13 '24 16:05 EmmanuelMMontesinos

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

avagst3 avatar May 14 '24 14:05 avagst3

@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

EmmanuelMMontesinos avatar May 14 '24 15:05 EmmanuelMMontesinos

@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

ivangermes avatar May 16 '24 03:05 ivangermes