pylibmagic
pylibmagic is a lightweight Python package designed to simplify the installation and usage of the `libmagic` C library. It provides pre-compiled `libmagic` shared libraries via Python wheels, primarily addressing the common `ImportError: failed to find libmagic` encountered when using other `libmagic` bindings like `python-magic`. The current version is 0.5.0, with releases focusing on expanding Python version and architecture support.
Common errors
-
ImportError: failed to find libmagic. Check your installation
cause The Python `magic` module (from `python-magic`) cannot locate the underlying `libmagic` C library. This is the primary problem `pylibmagic` aims to solve.fixInstall `pylibmagic` to provide the `libmagic` binaries, and ensure `python-magic` is also installed: `pip install pylibmagic python-magic`. For non-wheel scenarios or persistent issues, install system-level `libmagic` (e.g., `sudo apt-get install libmagic1` on Linux, `brew install libmagic` on macOS). -
ModuleNotFoundError: No module named 'magic'
cause The user has installed `pylibmagic` but expects it to provide the `magic` module directly. The `magic` module is actually provided by the `python-magic` package.fixInstall the `python-magic` library: `pip install python-magic`. Then, import `pylibmagic` before `magic` in your code: `import pylibmagic; import magic`. -
WindowsError: [Error 193] %1 is not a valid Win32 application
cause This error on Windows typically indicates a mismatch in the bitness between the installed Python interpreter and the `libmagic` DLLs being loaded (e.g., 64-bit Python trying to use 32-bit DLLs).fixEnsure your `libmagic` DLLs match your Python installation's bitness. A common fix is to install `python-magic-bin` (`pip install python-magic-bin`), which bundles compatible binaries.
Warnings
- gotcha pylibmagic does not provide the 'magic' module directly. Its purpose is to bundle and make the underlying `libmagic` C library accessible. To use the Python bindings for `libmagic`, you typically need to install `python-magic` separately.
- gotcha On platforms or architectures without pre-built wheels, `pylibmagic` installation might fail or require system-level `libmagic` installation.
- gotcha Using a 32-bit `libmagic` DLL with a 64-bit Python (or vice-versa) on Windows can lead to `WindowsError: [Error 193]`.
Install
-
pip install pylibmagic
Imports
- pylibmagic
import magic
import pylibmagic import magic
Quickstart
import pylibmagic
import magic
# pylibmagic ensures the underlying libmagic library is available
# You can inspect the data path it uses:
print(f"pylibmagic data path: {pylibmagic.data}")
# Now, use the python-magic library as usual
try:
m = magic.Magic()
file_type = m.from_buffer(b"hello world")
print(f"'hello world' is detected as: {file_type}")
# Example with a common file type
with open("temp_text.txt", "w") as f:
f.write("This is a test file.")
file_type_from_file = m.from_file("temp_text.txt")
print(f"'temp_text.txt' is detected as: {file_type_from_file}")
import os
os.remove("temp_text.txt")
except Exception as e:
print(f"An error occurred: {e}")
print("Ensure 'python-magic' is also installed (`pip install python-magic`).")