Python Magic (Binary Bundled)
python-magic-bin is a Python wrapper for `libmagic`, the file type identification library. Unlike the original `python-magic` package, `python-magic-bin` bundles the `libmagic` binary and its associated magic database, making it easier to install and use, especially on platforms like Windows where `libmagic` is not natively available. It currently stands at version 0.4.14 and has a slow release cadence, primarily for bug fixes or compatibility updates.
Common errors
-
No such file or directory: 'libmagic.dll'
cause `python-magic-bin` is failing to locate its bundled `libmagic` library or its associated magic database file, possibly due to installation issues, unusual environments, or permission problems.fixReinstall `python-magic-bin` to ensure the bundled binaries and data files are correctly placed. If using virtual environments, activate it before installing. Check for any environment variables (`MAGIC_FILE`, `PATH`) that might interfere with the library's ability to find its components. -
MagicException: b'File 0.4.14 is not compiled with utime. This causes problems with -s'
cause This error indicates a mismatch or issue with the bundled `libmagic` and how it was compiled, or how it's being used with specific flags (like `-s` for symlink following).fixVerify if the issue persists across different file types or environments. Try explicitly creating a `Magic` object without special flags, e.g., `magic.Magic(mime=True)`. If the problem persists, this might be a bug specific to the bundled `libmagic` version for your OS and may require upstream intervention or using a different `python-magic` variant. -
AttributeError: module 'magic' has no attribute 'from_file'
cause This usually happens when `import magic` is used, but the `magic` object is not instantiated, or an older/different `magic` library (not `python-magic-bin`) is imported that doesn't expose the same API.fixEnsure you are instantiating the `Magic` class: `import magic; m = magic.Magic(mime=True); m.from_file("path/to/file")`. Also, double-check that `python-magic-bin` is the *only* `magic` package installed and imported in your environment to avoid conflicts.
Warnings
- gotcha Confusion with `python-magic` (without `-bin`). `python-magic-bin` bundles the `libmagic` library, making installation easier, especially on Windows. The original `python-magic` requires `libmagic` to be installed separately via your system's package manager (e.g., `apt-get install libmagic-dev` on Debian/Ubuntu). Using both or installing the wrong one is a common source of errors.
- gotcha Limited Maintenance. The `python-magic-bin-extension` GitHub repository shows limited recent activity compared to its upstream `python-magic`. While functional, new features or rapid bug fixes are less likely, and it may not keep up with the latest `libmagic` features or bug fixes.
- gotcha Potential for Outdated Bundled `libmagic`. Since `libmagic` is bundled within `python-magic-bin`, updates to the underlying `libmagic` library are tied to `python-magic-bin` releases. This means the bundled version might lag behind the latest `libmagic` versions, potentially missing improved detections or security fixes.
Install
-
pip install python-magic-bin
Imports
- Magic
import magic
Quickstart
import magic
import os
# Create a dummy file for demonstration
with open('test_file.txt', 'w') as f:
f.write('This is a test file.')
# Instantiate Magic to get MIME types
m = magic.Magic(mime=True)
# Identify a local file
file_path = 'test_file.txt'
file_type = m.from_file(file_path)
print(f"File '{file_path}' is of type: {file_type}")
# Clean up the dummy file
os.remove(file_path)