Registry / serialization / libmagic

libmagic

JSON →
library1.0pypypiunverified

libmagic provides Python bindings to the `libmagic` C library, which identifies file types by checking their 'magic numbers' and other patterns. It's a thin wrapper around the venerable `file` command-line utility. The current version is 1.0, and it generally follows a stable, low-cadence release cycle focusing on maintenance and compatibility with the underlying C library.

pip install libmagic
INSTALL
IMPORT
SIG · LIBMAGIC
L
libmagic
serializationpythonv1.0
Install
2.5s avg
Import
Disk
17MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.910 runs
installs and imports cleanly · install 0.0s · import 0.000s · 19.2MB
glibc
py 3.103.910 runs
installs and imports cleanly · install 2.5s · import 0.000s · 20MB
17MB installed
● package 17MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Magic
import pymagic
from pymagic import Magic

This quickstart demonstrates how to use `libmagic` to identify file types from both actual files and byte buffers. It also shows how to retrieve the MIME type. A crucial first step not shown here is installing the underlying C `libmagic` library.

import os from magic import Magic # Create a dummy file for demonstration dummy_file_path = "test_file_for_libmagic.txt" with open(dummy_file_path, "w") as f: f.write("This is a plain text file content.") try: # Initialize Magic - often detects libmagic automatically # If not, you might need to specify the path to your magic.mgc file: # m = Magic(magic_file='/usr/share/misc/magic.mgc') m = Magic() # Get file type description from a file file_description = m.from_file(dummy_file_path) print(f"File '{dummy_file_path}' description: {file_description}") # Get file type from a byte buffer buffer_data = b"\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR..." buffer_description = m.from_buffer(buffer_data) print(f"Buffer data description: {buffer_description}") # Get MIME type explicitly m_mime = Magic(mime=True) mime_type = m_mime.from_file(dummy_file_path) print(f"File '{dummy_file_path}' MIME type: {mime_type}") finally: # Clean up the dummy file if os.path.exists(dummy_file_path): os.remove(dummy_file_path)
file --version
Debug
Known issues
gotchaThe `libmagic` Python package is a binding to the `libmagic` C library, which must be installed separately on your operating system.
fix
Before `pip install libmagic`, ensure the C library is present. For Debian/Ubuntu: `sudo apt-get install libmagic1`. For macOS (Homebrew): `brew install libmagic`. For Fedora/RHEL: `sudo dnf install file-libs`.
affects: 1.x
gotchaUsing `Magic().from_file()` on a directory path will result in an `IsADirectoryError`.
fix
Always verify that the path provided to `from_file()` points to an actual file, for example, by using `os.path.isfile(path)`.
affects: 1.x
gotchaIf the `libmagic` C library is installed in a non-standard location (e.g., custom Homebrew prefix or system without standard paths), the Python bindings might fail to find it.
fix
You may need to set the `MAGIC_FILE` environment variable to the full path of `magic.mgc` (the database file) or `LIBMAGIC_PATH` to the shared library (`libmagic.so`, `libmagic.dylib`, `libmagic.dll`) before running your Python script.
affects: 1.x
gotchaWhen dealing with filenames or file content that might contain non-ASCII characters, ensure your system's locale settings and Python's string handling are consistent to avoid encoding issues.
fix
While `libmagic` itself handles various encodings, the Python bindings typically return Unicode strings. If parsing output, be aware of character sets. For MIME types, specify `mime=True` in the `Magic` constructor.
affects: 1.x
Upgrade
Version history
1.0latest on PyPI · released Mar 2, 2012
Audit
Dependencies

No dependency data recorded yet.

Agent activity
4 hits · last 30 days
node
4
Resources
libmagic — pip install libmagic · libregistry