flet icon indicating copy to clipboard operation
flet copied to clipboard

ModuleNotFoundError: No module named 'pywintypes' error when using pywin32 code

Open EasyDevv opened this issue 1 year ago • 2 comments

Description Issue does not occur in python code state, but occurs after windows build

Code example to reproduce the issue:

import flet as ft
import win32com.client

info_dict = {}


def get_info():
    global info_dict

    wmi = win32com.client.GetObject("winmgmts:")
    boards = wmi.ExecQuery("SELECT * FROM Win32_BaseBoard")
    info_dict = {}
    for board in boards:
        info_dict["Manufacturer"] = board.Manufacturer
        info_dict["Product"] = board.Product
        info_dict["Serial Number"] = board.SerialNumber
        info_dict["Version"] = board.Version
        info_dict["Status"] = board.Status
    return info_dict


def main(page: ft.Page):
    page.add(ft.Text("Motherboard Information:"))

    for key, value in info_dict.items():
        page.add(ft.Text(f"{key}: {value}"))


info_dict = get_info()
ft.app(main)

Describe the results you received:

Run the built exe file

Traceback (most recent call last):
  File "<string>", line 40, in <module>
  File "<frozen runpy>", line 229, in run_module
  File "<frozen runpy>", line 88, in _run_code
  File "C:\Users\dev\AppData\Local\Temp\serious_python_tempf288247f\main.py", line 2, in <module>
  File "C:\Users\dev\AppData\Local\Temp\serious_python_tempf288247f\__pypackages__\win32com\__init__.py", line 8, in <module>
  File "C:\Users\dev\AppData\Local\Temp\serious_python_tempf288247f\__pypackages__\pythoncom.py", line 2, in <module>
ModuleNotFoundError: No module named 'pywintypes'

Describe the results you expected:

Additional information you deem important (e.g. issue happens only occasionally):

Flet version (pip show flet):

0.22

Give your requirements.txt file (don't pip freeze, instead give direct packages):

flet==0.22
pywin32==306

Operating system:

Windows 11

Additional environment details:

EasyDevv avatar Apr 15 '24 13:04 EasyDevv

i've this error too.. Could'u solve it? I try to import win32print and win32api but if i compile the script send me this error: Traceback (most recent call last): File "", line 43, in File "", line 229, in run_module File "", line 88, in _run_code File "[path]/main.py", line 11, in ModuleNotFoundError: No module named 'win32print'. EDIT [ 2024-06-18 21:34hs arg]: Finally i replaced the functions of win32print with subprocess comands for get the printer info and send to print a pdf file. If you need help with this, talk to me

Laztef avatar Jun 18 '24 05:06 Laztef

I realized that to use win32gui, win32api in pyinstaller builds,

I need to use the --hidden-import option as shown below.

pyinstaller --onefile main.py --hidden-import pywintypes

I'm guessing this is a similar issue.

There is no such option in flet build.

EasyDevv avatar Jul 01 '24 06:07 EasyDevv

change win32api with another module with similar functions. Luck with the search! If you need help, talk to me

Laztef avatar Aug 04 '24 03:08 Laztef

There is no such option in flet build.

https://flet.dev/docs/reference/cli/pack#:~:text=the%20executable%0A%20%20%2D%2D-,hidden,-%2Dimport%20%5BHIDDEN_IMPORT%20...%5D

ndonkoHenri avatar Aug 04 '24 09:08 ndonkoHenri

Converting to python native code via GPT or Claude worked for the most part. When using the flet build command, it seems to be a clean way to avoid using the pywin32 package as much as possible.

Thanks for the reply

import flet as ft
import subprocess


def get_info():
    info_dict = {}

    # Windows Motherboard Information
    output = subprocess.check_output(
        "wmic baseboard get product,Manufacturer,SerialNumber,version /format:csv",
        shell=True,
    ).decode()
    lines = output.strip().split("\n")
    if len(lines) > 1:
        headers = lines[0].strip().split(",")
        values = lines[1].strip().split(",")
        info_dict = dict(zip(headers, values))

    info_dict["Status"] = "OK" if info_dict else "Unknown"
    return info_dict


def main(page: ft.Page):
    page.title = "Motherboard Information"
    page.window.width = 400
    page.window.height = 300

    info_text = ft.Text("", size=16)

    def on_button_click(e):
        info_dict = get_info()
        info_text.value = "\n".join([f"{k}: {v}" for k, v in info_dict.items()])
        page.update()

    fetch_info_button = ft.FilledButton(
        text="Get Motherboard Info", on_click=on_button_click
    )

    column = ft.Column(controls=[fetch_info_button, info_text], disabled=False)

    page.add(column)


ft.app(target=main)

EasyDevv avatar Aug 07 '24 02:08 EasyDevv

i don't need this now, but the @ndonkoHenri link's suggest to try this option/argument: --hidden-import [HIDDEN_IMPORT ...] add an import not visible in the code of the script(s). Try this if you want to use win32api

Laztef avatar Aug 10 '24 19:08 Laztef