pywinrt / pywinrt

Code generation tool to create Python bindings from WinRT metadata.
https://pywinrt.readthedocs.io
MIT License
87 stars 9 forks source link

Support for PyInstaller #41

Closed Georglider closed 4 months ago

Georglider commented 10 months ago

I have encountered with ImportError while I was converting my python program to windows executable file:

import 'typing' # <pyimod03_importers.FrozenImporter object at 0x000001B8CE46BD90>
# winrt._winrt not found in PYZ
Traceback (most recent call last):
  File "main.py", line 1, in <module>
    import winrt.windows.networking.networkoperators as no
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "PyInstaller\loader\pyimod03_importers.py", line 476, in exec_module
  File "winrt\windows\networking\networkoperators\__init__.py", line 5, in <module>
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "PyInstaller\loader\pyimod03_importers.py", line 476, in exec_module
  File "winrt\system\__init__.py", line 4, in <module>
ImportError: cannot import name '_winrt' from 'winrt' (unknown location)
[28012] Failed to execute script 'main' due to unhandled exception!

I have tried including winrt._winrt as hiddenimports param, but it didn't change anything. Is there anything that I'm missing? Thanks in advance!

dlech commented 10 months ago

Can you please provide all of the step required to reproduce the issue?

Georglider commented 10 months ago

Here's the code for reproduction. It turns on your mobile hotspot and prints whether it was successful or not

import asyncio
import winrt.windows.networking.networkoperators as no
import winrt.windows.networking.connectivity as co

async def enable():
  try:
    info = co.NetworkInformation.get_internet_connection_profile()
    manager = no.NetworkOperatorTetheringManager.create_from_connection_profile(info)
    state = manager.tethering_operational_state
    if state == no.TetheringOperationalState.ON:
      return "WAS ALREADY ENABLED"
    elif state == no.TetheringOperationalState.OFF:
      ioAsync = await manager.start_tethering_async()
      if ioAsync.status == 0:
        return "SUCCESSFULLY ENABLED"
      return "COULDN'T ENABLE:( " + ioAsync.additional_error_message
    elif state == no.TetheringOperationalState.IN_TRANSITION:
      return "PROCESS..."
    else:
      return "SOMETHING WENT WRONG..."
  except:
    return "SOMETHING WENT WRONG... (EXCEPTION)"

async def main():
  info = await enable()
  print(info)

asyncio.run(main())

To make it an exe file I use: pyinstaller reproduction.py --onefile --paths \.venv\Lib\site-packages or pyinstaller reproduction.py --onefile --paths \.venv\Lib\site-packages --hidden-import winrt._winrt --debug=imports for debug logging And libraries list for winrt:

dlech commented 7 months ago

I tried this today and was not able to reproduce the problem. The .exe created successfully.

pyinstaller --version
6.5.0