Search code examples
pythoncimportpyinstallerexe

How do I convert a python code importing c to an exe file?


I am currently trying to get my project to work as an executable so I can share it more easily, but the code involved imports some c code to improve speed using the ctypes library. I am using pyinstaller to produce my .exes and it's working fine except with the CDLL ctype function, as in the code below:

from ctypes import CDLL
import time

foo_lib_path = 'theories/foo.so'
foo = CDLL(foo_lib_path)
print('Mission accomplished')
time.sleep(10)

When I run this code in my normal environment it works fine but when I compile to exe using pyinstaller --onefile 'bar.py' or pyinstaller --hidden-import 'theories/foo.so' --onefile 'bar.py' it immediately cuts out. How can I account for importing c libraries in my code?


Solution

  • The solution I eventually found for any future users with the same problem was to add the command --add-binary 'theories/foo.so;.' to --onefile instead of --hidden-import 'theories/foo.so'.