puremagic
Pure python implementation of magic file detection
What it is and what it does
puremagic is a pure Python library that identifies file types by examining magic numbers and file content. It serves as a lightweight, dependency-free alternative to python-magic/libmagic, making it suitable for cross-platform use without requiring system libraries. The package implements functions like from_file() and from_string() for basic identification, plus more detailed magic_file() and magic_stream() methods that return all possible matches ranked by confidence.
Since version 2.0, puremagic includes a deep scan system that performs content-aware analysis beyond simple magic number matching, improving accuracy for formats like Office documents, text files, CSV, MP3, Python source, JSON, HDF5, email, and scientific formats. Deep scan is enabled by default but can be disabled via the PUREMAGIC_DEEPSCAN environment variable. The package also includes a command-line interface for batch file identification.
Use it for:
- Identify uploaded file types in web applications without installing system dependencies like libmagic.
- Batch scan directories to classify files by format and MIME type across different operating systems.
- Validate file authenticity by confirming magic numbers match expected formats before processing.
- Detect and distinguish between similar formats (e.g., Office xlsx vs. OpenDocument odt) via deep scan.
- Analyze scientific data files (HDF5, VCF, SAM, GFF) with format-specific validation built in.
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
Identifies file types by analyzing magic numbers and file content, with zero runtime dependencies and cross-platform support.
Yes. Zero runtime dependencies, active maintenance, permissive MIT license, and no known vulnerabilities make this a low-risk choice. Install friction is minimal. Use it if you need file type detection without system library dependencies or if you want content-aware analysis beyond simple magic number matching. The Python 3.12+ requirement is a constraint but not a blocker for modern projects.
Install
puremagic on PyPI
pip
pip install puremagicuv
uv add puremagicpoetry
poetry add puremagicInstalling puremagic
Before you install
Low install friction with no runtime dependencies. Active maintenance with recent releases; last commit 2026-04-09 and 128 days since latest release.
License in practice
MIT license permits commercial and private use with minimal restrictions; attribution required.
Quickstart
import puremagic
# Identify file from path
ext = puremagic.from_file('test.gif')
# Returns: '.gif'
# Get all matches with confidence
results = puremagic.magic_file('test.gif')
# Returns: [['.gif', 'image/gif', 'Graphics interchange format file (GIF87a)', 0.7], ...]
Requires Python 3.12 or later; Python 3.7–3.11 users must use the 1.x release chain.
Verify before relying
- Exact count of supported file formats and how it compares to libmagic coverage
- Performance benchmarks against libmagic or other alternatives
- Accuracy metrics for deep scan across the supported format categories
Package facts
| License | MIT (permissive) |
| Python support | supports the current Python release (>=3.12) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | none |
| Maintenance | actively maintained — 128 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 7,537,817/month — #1,725 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: puremagic-2.2.0-py3-none-any.whl
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
filetypeInfers file type and MIME type by examining…
permissive · top 1,000 on PyPI
python-magic-binIdentifies file types by examining file headers…
permissive · top 15,000 on PyPI
python-magicIdentifies file types by reading file headers…
permissive · top 1,000 on PyPI
cigamDetects and reports file types, MIME types, and…
permissive · top 15,000 on PyPI
magikaMagika identifies file types using deep…
permissive · top 5,000 on PyPI
file-magicfile-magic provides a Python interface to…
permissive · top 15,000 on PyPI
libmagicProvides Python bindings to the libmagic…
copyleft · top 15,000 on PyPI
identifyIdentifies file types by examining file paths,…
permissive · top 1,000 on PyPI
polyfile-weaveIdentifies and maps the semantic and syntactic…
permissive · top 5,000 on PyPI
pylibmagicProvides pre-compiled libmagic libraries…
permissive · top 15,000 on PyPI