python-magic
File type identification using libmagic
Decision gist · record as of 2026-08-14
Yes. python-magic is a mature, widely-used library (top 1000 PyPI packages) with no security vulnerabilities, permissive licensing, and low install friction. The only gotcha is the system-level libmagic dependency, which is straightforward to satisfy on all major platforms. Install it when you need reliable file type detection beyond filename extensions.AI-flagged interpretation of the facts on this page — verify before relying
Before you install
- libmagic C library must be installed separately (apt-get install libmagic1 on Debian/Ubuntu, brew install libmagic on macOS, or python-magic-bin on Windows).
- Low install friction; pure Python wheel with no runtime dependencies.
- Maintenance is active with recent commits, though the latest PyPI release is from 2022.
License · maintenance · safety
MIT (permissive) — MIT license is permissive; you can use this in commercial and proprietary projects with minimal restrictions.
last release 2022-06-07 (1529 days) · last repo commit 2026-07-20 · 2,914 stars
0 known vulnerabilities (OSV.dev, 2026-08-14) · 30,749,529 downloads/mo, #793 on PyPI
Alternatives
Verify before relying
pip install python-magic
import magic
magic.from_file("testdata/test.pdf")
# Returns: 'PDF document, version 1.2'
magic.from_file("testdata/test.pdf", mime=True)
# Returns: 'application/pdf'- Whether the 1529 days since last release (2022-06-07) reflects active maintenance or stalled development despite recent commits.
- Performance characteristics when processing large files or high-volume batch operations.
- Thread-safety guarantees beyond the documented warning that Magic class instances are not thread-safe.
What it is and what it does
python-magic is a thin Python wrapper around libmagic, the C library that powers the Unix `file` command. It identifies file types by examining file headers against a predefined database of magic numbers and patterns, returning human-readable type descriptions or MIME types. The package provides two main interfaces: simple module-level functions (from_file, from_buffer) for straightforward identification, and a Magic class for advanced use cases like custom magic databases or character encoding detection.
The module has no runtime dependencies and installs as a pure Python wheel, but requires the libmagic C library to be present on the system. It supports Python 2.7 and 3.5 through 3.9, and is actively maintained on GitHub. Common use cases include validating uploaded files, detecting document formats, and filtering by MIME type in web applications or file processing pipelines.
Use it for
- Validate file uploads in web applications by checking actual file type rather than trusting user-supplied extensions.
- Automatically detect and categorize files in batch processing pipelines based on their actual format.
- Extract MIME types for files to serve with correct Content-Type headers in HTTP responses.
- Identify compressed or nested file formats (e.g., gzip-compressed text) using the uncompress flag.
Worth the install?
AI-flagged interpretation of the facts on this page. Verify before relying on it.
Yes.
python-magic is a mature, widely-used library (top 1000 PyPI packages) with no security vulnerabilities, permissive licensing, and low install friction. The only gotcha is the system-level libmagic dependency, which is straightforward to satisfy on all major platforms. Install it when you need reliable file type detection beyond filename extensions.
Install
python-magic on PyPI
Before you install
Low install friction; pure Python wheel with no runtime dependencies. Maintenance is active with recent commits, though the latest PyPI release is from 2022. On Windows, you may need to install python-magic-bin separately for libmagic DLLs.
libmagic C library must be installed separately (apt-get install libmagic1 on Debian/Ubuntu, brew install libmagic on macOS, or python-magic-bin on Windows).
License in practice
MIT license is permissive; you can use this in commercial and proprietary projects with minimal restrictions.
Quickstart
pip install python-magic
import magic
magic.from_file("testdata/test.pdf")
# Returns: 'PDF document, version 1.2'
magic.from_file("testdata/test.pdf", mime=True)
# Returns: 'application/pdf'
Verify before relying
- Whether the 1529 days since last release (2022-06-07) reflects active maintenance or stalled development despite recent commits.
- Performance characteristics when processing large files or high-volume batch operations.
- Thread-safety guarantees beyond the documented warning that Magic class instances are not thread-safe.
Package facts
| License | MIT permissive |
| Python support | Supports the current Python release >=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.* |
| Install friction | Low. Pure-Python wheel |
| Runtime dependencies | None |
| Maintenance | Actively maintained 1,529 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 30,749,529 / month, #793 on PyPI 30-day window, as of 2026-08-14 |
| Known vulnerabilities | None known OSV.dev, checked 2026-08-14 |
| Classifiers | Intended Audience :: DevelopersLicense :: OSI Approved :: MIT LicenseProgramming Language :: PythonProgramming Language :: Python :: 2.7Programming Language :: Python :: 3Programming Language :: Python :: 3.5Programming Language :: Python :: 3.6Programming Language :: Python :: 3.7Programming Language :: Python :: 3.8Programming Language :: Python :: 3.9Programming Language :: Python :: Implementation :: CPython |
Evidence: python_magic-0.4.27-py2.py3-none-any.whl
Tags
Let your AI agent find packages like this
Example. Real query, live index.
An agent finds packages by wishing: SkillFed indexes 14,416 PyPI packages by what they can do, searchable in plain language. Give your agent the search over MCP.
Similar packages
Detects and reports file types, MIME types, and extensions by analyzing file magic bytes.
file-magic provides a Python interface to libmagic, enabling detection of file types, MIME types, and encodings from file paths or content.
The main gotcha is the libmagic system dependency, which is typically already present but must be verified in your deployment environment.
Infers file type and MIME type by examining magic number signatures in files or byte buffers, with no external dependencies.
However, the last release was 2022-11-02 with no recent commits, so it may not support the newest file formats or receive bug fixes promptly.
Identifies and maps the semantic and syntactic structure of files, including polyglots and embedded files, with a pure-Python libmagic implementation that can replace the `file` command and recursively extract embedded content.
However, the aging maintenance status (204 days since last release, minimal repository activity) and 14 runtime dependencies warrant caution for production use—verify…
Provides Python bindings to the libmagic library for file type detection and MIME type identification using ctypes.
Provides pre-compiled libmagic libraries bundled with Python, solving the missing system dependency problem that typically breaks python-magic imports.
See also python-magic-bin · puremagic · typecode · magika