skillfed

python-magic-bin

File type identification using libmagic binary package

python-magic-bin v0.4.14 204.6K downloads/30d#9,604 on PyPI33
Permissive license MIT DORMANT released

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-bin

uv

uv add python-magic-bin

poetry

poetry add python-magic-bin

Installing 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

Intended Audience :: DevelopersLicense :: OSI Approved :: MIT LicenseProgramming Language :: PythonProgramming Language :: Python :: 2Programming Language :: Python :: 3

Tags

file type detection pythonmime type identificationlibmagic python wrapperfile magic number detectionbinary file type recognitionfile header analysismagic database file identification
file-identificationmime-type-detectionbinary-analysis

More Utilities packages