'Creating universal build for mac os with pyinstaller and python3

I have two mac machines. One is intel based and running big sur and second is M1 and running big sur. I am trying to build a sample hello world package using pyinstaller and with universal2 options . the package works fine on intel machine but when I copy that package on the M1 machine it throws below error

marwa@admins-Mac-mini test_app % ls
build           dist            test.py         test.spec
marwa@admins-Mac-mini test_app % cd dist
marwa@admins-Mac-mini dist % ls
test            test.app
marwa@admins-Mac-mini dist % cd test
marwa@admins-Mac-mini test % ls
Python                  libbz2.1.0.dylib        libexpat.1.dylib        libintl.8.dylib         libssl.3.dylib
base_library.zip        libcrypto.3.dylib       libffi.8.dylib          liblzma.5.dylib         libz.1.dylib
lib-dynload             libedit.0.dylib         libiconv.2.dylib        libncurses.6.dylib      test
marwa@admins-Mac-mini test % ./test
[29040] Module object for struct is NULL!
Traceback (most recent call last):
  File "struct.py", line 13, in <module>
ImportError: dynamic module does not define module export function (PyInit__struct)
[29040] Module object for pyimod02_archive is NULL!
Traceback (most recent call last):
  File "PyInstaller/loader/pyimod02_archive.py", line 27, in <module>
ModuleNotFoundError: No module named 'struct'
[29040] Module object for pyimod03_importers is NULL!
Traceback (most recent call last):
  File "PyInstaller/loader/pyimod03_importers.py", line 23, in <module>
ModuleNotFoundError: No module named 'pyimod02_archive'
Traceback (most recent call last):
  File "PyInstaller/loader/pyiboot01_bootstrap.py", line 17, in <module>
ModuleNotFoundError: No module named 'pyimod03_importers'
[29040] Failed to execute script 'pyiboot01_bootstrap' due to unhandled exception: No module named 'pyimod03_importers'
[29040] Traceback:

Compiling my code using below command

cloudqa@MACBOOKAIR01 test_app % /opt/local/bin/python3.9 /opt/local/Library/Frameworks/Python.framework/Versions/3.9/bin/pyinstaller   --target-architecture universal2 test.py

also


cloudqa@MACBOOKAIR01 test_app % lipo -archs /opt/local/bin/python3.9
x86_64 arm64
cloudqa@MACBOOKAIR01 test_app %

If I go and check the architecture of the binary in the dist folder I can see below

cloudqa@MACBOOKAIR01 test % ls
Python                  libbz2.1.0.dylib        libexpat.1.dylib        libintl.8.dylib         libssl.3.dylib
base_library.zip        libcrypto.3.dylib       libffi.8.dylib          liblzma.5.dylib         libz.1.dylib
lib-dynload             libedit.0.dylib         libiconv.2.dylib        libncurses.6.dylib      test
cloudqa@MACBOOKAIR01 test % lipo -archs test
x86_64 arm64
cloudqa@MACBOOKAIR01 test %


Solution 1:[1]

It seems like there is a possible import/path problem early on from where the following errors cascade from. It’s difficult to tell without seeing the code from struct.py. Just going by ImportError something is either missing or not properly set.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 former_Epsilon