Problem
I'm writing an application in Python. It works just fine in my python environment, but when I compile it with PyInstaller and try to run the resulting executable, I get the following error: ImportError: failed to find libmagic. Check your installation
I have a feeling that this has something to do with "python-magic-bin", an install containing the binaries that were required for a module named "magic", because if I uninstall "python-magic-bin" from my Python environment and try to run the app, I get the same exact error. I think that for some reason these binaries aren't being carried to the compiled exe.
If it makes a difference, I installed the "python-magic-bin" from a .whl file. This install added a folder called "libmagic" files libmagic.dll and magic.mgc to the directory that "magic" was installed to.
Question
If I'm right about the problem, how do I get PyInstaller to carry over the binaries that "magic" needs?
Recreate the Problem
The following can be done to recreate the problem:
Copy and paste this code into your editor. Save it as a file called "test.py"
import magic m=magic.MAGIC_NONE print(m)
Download "python_magic_bin-0.4.14-py2.py3-none-win32.whl" from this link and use the following commands in the interpreter to install the required libraries to Python 3.6
>>> pip install pyinstaller >>> pip install python-magic >>> pip install python_magic_bin-0.4.14-py2.py3-none-win32.whl >>> pip install libmagic
Open a command prompt in the same directory as that "test.py" file and use the following command to compile the program using pyinstaller:
> pyinstaller test.py
After it's done, move to the newly created /dist/test directory (
cd ./dist/test
) and run the .exe using:> ./test.exe
After running it, you should see an error reading: ImportError: failed to find libmagic. Check your installation
and Failed to execute script test
Spec File
This is the spec file I'm using to compile my project.
# -*- mode: python -*-
block_cipher = None
a = Analysis(['main.py'],
pathex=['D:\\Home_Python\\pytags'],
binaries=[],
datas=[],
hiddenimports=[],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher)
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(pyz,
a.scripts,
exclude_binaries=True,
name='main',
debug=False,
strip=False,
upx=True,
console=True )
coll = COLLECT(exe,
a.binaries,
a.zipfiles,
a.datas,
strip=False,
upx=True,
name='main')
Versions
For this project, I'm using:
- Python 3.6.3
- PyInstaller 3.3.1
- Libmagic 1.0
- Python-Magic 0.4.15
- Python-Magic-Bin 0.4.14