python-magic-bin
File type identification using libmagic binary package
What it is and what it does
python-magic-bin is a Python wrapper around libmagic, the file-type identification library that powers the Unix `file` command. It identifies files by examining their headers against a predefined database of file signatures, returning human-readable descriptions or MIME types. The package is a fork of python-magic that bundles pre-compiled libmagic binaries for Windows, macOS, and Linux, eliminating the need to install libmagic separately on those platforms.
You can use it two ways: the simple functional API (magic.from_file, magic.from_buffer) for quick file identification, or the Magic class for finer control over options like MIME mode, compression detection, and character encoding. The package has no runtime dependencies beyond the bundled libmagic binaries, making it straightforward to integrate into file-processing pipelines, content-type detection systems, or validation workflows.
Use it for:
- Detect file types in upload handlers to validate user-submitted files without relying on extensions.
- Identify MIME types for web servers or APIs serving files with correct Content-Type headers.
- Analyze compressed archives by detecting both the outer format and decompressed content type.
- Validate binary file integrity by confirming expected file signatures before processing.
- Build content-aware file routing or filtering systems that classify files by their actual type.
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
Identifies file types by examining file headers using libmagic, exposing the Unix `file` command's functionality to Python with pre-built binaries for Windows, macOS, and Linux.
Yes, if you need reliable file-type detection and want to avoid installing libmagic separately on Windows or macOS. The package is stable with no known vulnerabilities. However, verify compatibility with your Python version; the fork's last release was 2017-10-02 and the project is dormant.
Install
python-magic-bin on PyPI
pip
pip install python-magic-binuv
uv add python-magic-binpoetry
poetry add python-magic-binInstalling python-magic-bin
Before you install
Medium install friction due to platform-specific binary wheels; the package is dormant (last release 2017-10-02, last commit 2023-11-21) but carries no known vulnerabilities.
License in practice
MIT license is permissive; you may use, modify, and distribute this package freely in commercial and private projects with minimal restrictions.
Quickstart
pip install python-magic-bin
import magic
result = magic.from_file("testdata/test.pdf")
print(result) # 'PDF document, version 1.2'
mime_type = magic.from_file("testdata/test.pdf", mime=True)
print(mime_type) # 'application/pdf'
On Windows, you may need to manually provide libmagic DLLs (magic1.dll, regex2.dll, zlib1.dll) and the magic database file; 32-bit and 64-bit builds must match your Python installation.
Verify before relying
- Whether the package's dormancy (no releases since 2017-10-02) affects compatibility with modern Python versions or libmagic APIs.
- Current status of the upstream python-magic library and whether this fork remains the recommended binary-packaged alternative.
- Actual adoption and usage patterns given the long time since last release.
Package facts
| License | MIT (permissive) |
| Python support | not specified |
| Install friction | medium — platform-specific wheel |
| Runtime dependencies | none |
| Maintenance | dormant — 3,238 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 204,568/month — #9,604 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: python_magic_bin-0.4.14-py2.py3-none-macosx_10_6_intel.whl; python_magic_bin-0.4.14-py2.py3-none-win32.whl; python_magic_bin-0.4.14-py2.py3-none-win_amd64.whl
Keywords: mime, magic, file, binary
Tags
More Utilities packages
Converts domain names between Unicode and…
permissive · top 100 on PyPI
charset-normalizerDetects and normalizes text encoding from…
permissive · top 100 on PyPI
setuptoolsSetuptools is a Python build backend and…
permissive · top 100 on PyPI
pluggyPluggy provides a plugin system that lets you…
permissive · top 100 on PyPI
PygmentsPygments is a syntax highlighter that colorizes…
permissive · top 100 on PyPI
sixSix provides utility functions to write Python…
permissive · top 100 on PyPI
puremagicIdentifies file types by analyzing magic…
permissive · top 5,000 on PyPI
pylibmagicProvides pre-compiled libmagic libraries…
permissive · top 15,000 on PyPI
python-magicIdentifies file types by reading file headers…
permissive · top 1,000 on PyPI
libmagicProvides Python bindings to the libmagic…
copyleft · top 15,000 on PyPI
cigamDetects and reports file types, MIME types, and…
permissive · top 15,000 on PyPI
file-magicfile-magic provides a Python interface to…
permissive · top 15,000 on PyPI
magikaMagika identifies file types using deep…
permissive · top 5,000 on PyPI
polyfile-weaveIdentifies and maps the semantic and syntactic…
permissive · top 5,000 on PyPI
filetypeInfers file type and MIME type by examining…
permissive · top 1,000 on PyPI
typecode-libmagicProvides a prebuilt native libmagic binary and…
copyleft · top 15,000 on PyPI