Search code examples
pythonpyinstallerpyc

How to remove Bad magic number error in Pyinstaller


I have a python project which contains app.py and source.py files. Project also has other files for configs and logs. I have converted app.py to app.exe using pyinstaller

pyinstaller app.py

This has created a dist directory. I have copy pasted source.py, configs and the log files in the dist directory.

I also want to create a setup for this which will be installed on another machine. I cannot share the source.py file as it is thus instead of .py I have used source.pyc. I have now used inno compiler and have created a setup file.

As per my understanding, pyinstaller automatically binds the python interpreter so we do not need to install python on any other machine. I simply installed the exe and it started working fine.

In my project, I have a functionality which calls the source.pyc file like below:

exec(os.system("source.pyc install"))

This was working fine on my development machine but in other machine, it is giving me bad magic error.

As per online forums, this normally happens when we try to run the pyc file with different python version interpreter but in my case I am using the same pyinstaller interpreter. Then how come this error is coming.

Is there any other of compiling the additional files apart from app.py using pyinstaller. How can I remove this error. Thanks


Solution

  • PyInstaller does not bundle or bind a Python interpreter with the EXE file. The EXE file is a compiled binary file which, as it is compiled, does not need an interpreter. A PYC file still requires an interpreter.

    You can compile the other files (i.e. source.py) using PyInstaller as well, and then move the compiled EXE file to the same directory as app.exe. In that case you would be able to run your command of import os; os.system("source.exe") from the main program. If your other compiled scripts require modules/libraries not used/included in the app.exe compilation, then when moving the compiled source.exe, you will need to move the bundled libraries to the app.exe directory as well.