Sauci / pya2l

BSD 3-Clause "New" or "Revised" License
33 stars 9 forks source link

Imports in autogenerated files wrong? #23

Open stephan192 opened 8 months ago

stephan192 commented 8 months ago

I have a working python script which uses your pya2l module. As long as i run my script from VisualStudioCode or CMD everything is working. But when i'm trying to generate an exe file out of my python script via PyInstaller or Nuitka, in both cases the generated exe file is not running and in the console i get the following error message. The following message is reported by the exe generated with PyInstaller. The error message from the one generated with Nuitka is, besides the filepaths, identical.

Traceback (most recent call last):
  File "my_script.py", line 17, in <module>
  File "PyInstaller\loader\pyimod02_importers.py", line 419, in exec_module
  File "my_lib.py", line 5, in <module>
  File "PyInstaller\loader\pyimod02_importers.py", line 419, in exec_module
  File "my_a2l_parser.py", line 5, in <module>
  File "PyInstaller\loader\pyimod02_importers.py", line 419, in exec_module
  File "pya2l\__init__.py", line 12, in <module>
  File "PyInstaller\loader\pyimod02_importers.py", line 419, in exec_module
  File "pya2l\cli.py", line 18, in <module>
  File "PyInstaller\loader\pyimod02_importers.py", line 419, in exec_module
  File "pya2l\parser.py", line 15, in <module>
  File "PyInstaller\loader\pyimod02_importers.py", line 419, in exec_module
  File "pya2l\protobuf\API_pb2.py", line 15, in <module>
ModuleNotFoundError: No module named 'protobuf'
[10064] Failed to execute script 'my_script' due to unhandled exception!

Used versions (All packages installed via pip): Python: 3.11.1 pya2l 0.1.2 Nuitka 2.0.1 pyinstaller 6.3.0

Looking into one of the autogenerated files, e.g. _protobuf\APIpb2.py, Pylance installed with VisualStudioCode reports reportMissingImports for line 15. This fits to the error message shown above.

If i change all similar includes in all autogenerated files from e.g. from protobuf import A2L_pb2 as protobuf_dot_A2L__pb2 to from . import A2L_pb2 as protobuf_dot_A2L__pb2 i can generate working exe files with both PyInstaller and Nuitka and the Pylance warning vanishes.

For me it seems as if the script which generates the content of the protobuf folder makes an error.

Additional note: When using PyInstaller and Nuitka you have to explicitly add _a2l_grpc_windowsamd64.dll to get a working exe file. But this is not related to this issue.

bertbroer commented 7 months ago

I run into the same issue. Have you found a workaround?

Sauci commented 7 months ago

It seems to be a known bug/limitation, which is apparently not close from beeing fixed. In the meanwhile, this tool has been developed to overcome this problem. Could you give it a try and give a feedback if it actually fixes the integration problem with PyInstaller? Thanks

stephan192 commented 7 months ago

I adjusted every relevant import from from protobuf to from . to get it working.

But finally i decided to write my one, very reduced, A2L parser instead of using py2al because i don't want to use libraries which have to be adjusted after installation.