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
11.44k stars 445 forks source link

flet build windows doesn't work #2422

Closed Yanzzzi closed 9 months ago

Yanzzzi commented 10 months ago

Description

My script has a popup that creates pdf and excel reports. The code for creating reports is placed in a separate file, it uses the fpdf2 and openpyxl libraries. An unknown error occurs when using the "flet build windows" command (logs below).

At the same time, "flet build windows" works properly on a minimal example.

Code example to reproduce the issue: main.py

import flet as ft
import reports

def main(page: ft.Page):
    ...
    pb = ft.PopupMenuButton(
        items=[
            ft.PopupMenuItem(content=ft.TextField(label='01 01 2024 00:00:00-31 01 2024 23:59:59',
                                                  text_size=11,
                                                  label_style=ft.TextStyle(size=11)
                                                  )),
            ft.PopupMenuItem(icon=ft.icons.PICTURE_AS_PDF_OUTLINED, text=reports.PDF_SHORT_NO_DATE,
                             on_click=click_pdf_button),
            ft.PopupMenuItem(icon=ft.icons.PICTURE_AS_PDF_OUTLINED, text=reports.PDF_SHORT_WITH_DATE,
                             on_click=click_pdf_button),
            ft.PopupMenuItem(icon=ft.icons.PICTURE_AS_PDF_OUTLINED, text=reports.PDF_FULL,
                             on_click=click_pdf_button),
            ft.PopupMenuItem(icon=ft.icons.PICTURE_AS_PDF_OUTLINED, text=reports.PDF_SUMMARY,
                             on_click=click_pdf_button),
            ft.PopupMenuItem(),
            ft.PopupMenuItem(icon=ft.icons.TEXT_SNIPPET, text=reports.EXCEL_SHORT_NO_DATE,
                             on_click=click_pdf_button),
            ft.PopupMenuItem(icon=ft.icons.TEXT_SNIPPET, text=reports.EXCEL_SHORT_WITH_DATE,
                             on_click=click_pdf_button),
            ft.PopupMenuItem(icon=ft.icons.TEXT_SNIPPET, text=reports.EXCEL_FULL,
                             on_click=click_pdf_button),
            ft.PopupMenuItem(icon=ft.icons.TEXT_SNIPPET, text=reports.EXCEL_SUMMARY,
                             on_click=click_pdf_button),
        ],
    )
    upper_buttons = ft.Row([pb], expand=True)
    page.add(upper_buttons,)

ft.app(main)

reports.py

import pyodbc
from fpdf import FPDF, YPos, XPos, Align
from fpdf.fonts import FontFace
import re

from openpyxl import Workbook
from openpyxl.drawing.image import Image
from openpyxl.styles import PatternFill, Border, Side, Alignment, Protection, Font

class Report:
    ...
    abstract class

class PDF(Report, FPDF):
    ...
    creating a report

class EXCEL(Report):
    ...
    creating a report

Describe the results you received:

flet build windows
Creating Flutter bootstrap project...OK
Customizing app icons and splash images...OK
Generating app icons...OK
Packaging Python app...Running package command                                                                                                                                                            
Creating asset directory: C:\Users\user\AppData\Local\Temp\flet_flutter_build_OIYlIhhnLX\app                                                                                                              
Copying Python app from D:\python_test_32_python\fdff to C:\Users\user\AppData\Local\Temp\serious_python_temp1ca76c41                                                                                     
Installing dependencies  with pip command to C:\Users\user\AppData\Local\Temp\serious_python_temp1ca76c41\__pypackages__                                                                                  
Extracting Python distributive from C:\Users\user\AppData\Local\Temp\cpython-3.11.6+20231002-x86_64-pc-windows-msvc-shared-install_only.tar.gz to C:\Users\user\AppData\Local\Temp\hostpython3.11_8c14fc79
Compiling Python sources at C:\Users\user\AppData\Local\Temp\serious_python_temp1ca76c41

Error building Flet app - see the log of failed command above.

Additional information you deem important (e.g. issue happens only occasionally): Before that, in principle, the "flet build windows" command did not work for me, it gave the following error:

flet build windows
Creating Flutter bootstrap project...OK
Customizing app icons and splash images...OK
Generating app icons...Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "D:\python projects\gui\.venv\Scripts\flet.exe\__main__.py", line 7, in <module>
  File "D:\python projects\gui\.venv\Lib\site-packages\flet\cli\cli.py", line 83, in main
    args.handler(args)
  File "D:\python projects\gui\.venv\Lib\site-packages\flet\cli\commands\build.py", line 518, in handle
    icons_result = self.run(
                   ^^^^^^^^^
  File "D:\python projects\gui\.venv\Lib\site-packages\flet\cli\commands\build.py", line 731, in run
    r = subprocess.run(
        ^^^^^^^^^^^^^^^
  File "C:\Users\user\AppData\Local\Programs\Python\Python312\Lib\subprocess.py", line 548, in run
    with Popen(*popenargs, **kwargs) as process:
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\user\AppData\Local\Programs\Python\Python312\Lib\subprocess.py", line 1026, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "C:\Users\user\AppData\Local\Programs\Python\Python312\Lib\subprocess.py", line 1538, in _execute_child
    hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
OSError: [WinError 193] %1 is not a valid Win32 application

I managed to fix this by replacing the python interpreter of the x64 version with x32.

Flet version (pip show flet):

Name: flet
Version: 0.19.0
Summary: Flet for Python - easily build interactive multi-platform apps in Python
Home-page:
Author: Appveyor Systems Inc.
Author-email: hello@flet.dev
License: Apache-2.0
Location: D:\python projects\gui\.venv\Lib\site-packages
Requires: cookiecutter, flet-runtime, packaging, qrcode, watchdog, websocket-client, websockets
Required-by:

Operating system:

Windows 10 Pro

FeodorFitsner commented 10 months ago

Please provide full logs with flet build windows -vv command.

Yanzzzi commented 10 months ago

Please provide full logs with flet build windows -vv command.

Thanks for the hint @FeodorFitsner, for some reason I didn't think to use the -vv flag. With the help of full logs, an error was found: it was not possible to compile an f-string that used the same quotes (everything worked when running the script normally).

f'{current_date_obj.strftime('%m/%Y')}'

I have a couple of questions left:

  1. As I wrote above, with the x64 version of the interpreter, the build command does not work. Does the command work only with the x32 version, or is it possible to run it on x64 somehow?
  2. At the first launch the .exe file after build the program freezes for 2-3 seconds and displays only a white screen. On subsequent launches the delay becomes less (about 1 second). Why is this happening? When packing the program with the pack command, the .exe file runs immediately, nothing freezes.
FeodorFitsner commented 9 months ago

It looked like it was Python 3.12.0 specific error. Updating to 3.12.1 should fix that: https://github.com/flet-dev/flet/issues/2441#issuecomment-1902568848