static_magic
Yet another set of Python bindings for libmagic, the library behind the file command.
There's like 8 of these on PyPI, and even an official one shipped as part of file, so why another one?
Well basically none of them statically link and version libmagic. And I want to do that. static_magic compiles libmagic into the extension and ships the matching magic database inside the package, so you get the same answers everywhere, whatever libmagic your system has (or doesn't have).
Also I wanted to learn how C extensions work (not CFFI, not Cython, honest-to-god C extensions).
Install
pip install static_magic
We use the libmagic version as the first two parts of our version number. So if you install static_magic==5.45.* you are guaranteed to get libmagic 5.45. The third part is for changes to the bindings themselves.
Wheels are built for CPython 3.12+ (including free-threaded 3.14t) on:
- Linux x86_64 and aarch64 (glibc / manylinux)
- macOS arm64 (Apple Silicon), macOS 11+
Anywhere else, pip builds from the source distribution, which needs a C compiler and make.
Usage
import static_magic
static_magic.get_description_file("report.pdf") # 'PDF document, version 1.4, 2 page(s)'
static_magic.get_mime_type_file("report.pdf") # 'application/pdf'
with open("report.pdf", "rb") as f:
static_magic.get_mime_type_bytes(f.read(2048)) # 'application/pdf'
The module-level functions share a database that's loaded the first time you use one. If you'd rather manage that yourself, or want a custom database, make a Magic:
m = static_magic.Magic() # or Magic(database="/path/to/magic.mgc")
m.get_description_file("/bin/ls") # 'ELF 64-bit LSB pie executable, x86-64, ...'
m.get_description_bytes(b"%PDF-1.4\n") # 'PDF document, version 1.4'
A Magic is safe to share between threads; calls on the same instance take turns.
Files that can't be read raise the usual OSError subclasses rather than returning libmagic's error text:
try:
static_magic.get_mime_type_file("missing.txt")
except FileNotFoundError as e:
print(e.filename) # 'missing.txt'
static_magic.libmagic_version() returns the bundled libmagic version as an int, e.g. 545.
Development
libmagic comes from the file repository, pinned as a git submodule:
git clone --recurse-submodules https://github.com/cstarkers/static_magic.git
cd static_magic
pip install -e .
python -m pytest tests
Building from git needs autotools (autoconf, automake, libtool) to generate libmagic's configure. Source distributions ship it pre-generated, so installing from PyPI doesn't. After changing the C code, run pip install -e . again.
Upgrading libmagic
The package version comes from the submodule, so moving to a new libmagic is:
- Check out the new release tag in the submodule, e.g.
git -C third_party/file checkout FILE5_46. - Reset
BINDING_REVISIONinsetup.pyto0. rm -rf build && pip install -e . && python -m pytest tests.test_libmagic_version_matches_package_versionchecks the compiled libmagic matches the new version.- Commit the submodule bump and tag
v5.46.0.
For a bindings-only release on the same libmagic, bump BINDING_REVISION instead.
License
BSD-2-Clause, see LICENSE. libmagic's own licence is in LICENSE-libmagic and is included in every wheel.
Metadata
Release files for static-magic 5.45.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| static_magic-5.45.0.tar.gz | 1.7 MB | With provenance | Details |
Built distributions (wheels)
Total release size: 9.9 MB
Release files / static_magic-5.45.0.tar.gz
| Download URL | static_magic-5.45.0.tar.gz |
|---|---|
| Size | 1.7 MB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
da31cf75ff2eb905ac63c5edad5865031fe0408cc98783092590528df2a04582
|
|
BLAKE2b-256 checksum How to use checksums |
747f8a8d355d72854c91b837dd647b3599d7c7f3bb00c51854690b407e5da8f2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 29, 2026.
Transparency logRelease files / static_magic-5.45.0-cp314-cp314t-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
| Download URL | static_magic-5.45.0-cp314-cp314t-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl |
|---|---|
| Size | 771.2 kB |
| Tags | CPython 3.14 CPython 3.14 free-threading Linux glibc 2.17+ x86-64 Linux glibc 2.28+ x86-64 |
|
SHA-256 checksum How to use checksums |
dc198aae85cea13cf0825abe89941fa4bed7a1c12c65e42cf0d907508fb86426
|
|
BLAKE2b-256 checksum How to use checksums |
8d4ccac8215666e5038876fc99109fdc8be8da2e6d22a8ce3f151ddf3e936402
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 29, 2026.
Transparency logRelease files / static_magic-5.45.0-cp314-cp314t-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
| Download URL | static_magic-5.45.0-cp314-cp314t-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl |
|---|---|
| Size | 766.7 kB |
| Tags | CPython 3.14 CPython 3.14 free-threading Linux glibc 2.17+ ARM64 Linux glibc 2.28+ ARM64 |
|
SHA-256 checksum How to use checksums |
239e9ebd4144b58d26ac7ae2815e5394f8d7ef75498c39e604b1d5299dba18f0
|
|
BLAKE2b-256 checksum How to use checksums |
ed360b0eeccde9e369b59cd18a401bf9a68556ee3430feff39e9d3eeb52ff0bc
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 29, 2026.
Transparency logRelease files / static_magic-5.45.0-cp314-cp314t-macosx_11_0_arm64.whl
| Download URL | static_magic-5.45.0-cp314-cp314t-macosx_11_0_arm64.whl |
|---|---|
| Size | 524.1 kB |
| Tags | CPython 3.14 CPython 3.14 free-threading macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
f4740ec3dcb500f158b9fed53f8506c9253c20d3488924c98885ab07a97d56c3
|
|
BLAKE2b-256 checksum How to use checksums |
0a40a122e8a7108011dcb02c9a9528a98a1f0ef1cfc9d6ff58bdc647c451bf8c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 29, 2026.
Transparency logRelease files / static_magic-5.45.0-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
| Download URL | static_magic-5.45.0-cp314-cp314-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl |
|---|---|
| Size | 770.7 kB |
| Tags | CPython 3.14 Linux glibc 2.17+ x86-64 Linux glibc 2.28+ x86-64 |
|
SHA-256 checksum How to use checksums |
79ee21b4791a049ef32588cca4d07ab4a0f9ebf1591ee7b2f24174ace53a0565
|
|
BLAKE2b-256 checksum How to use checksums |
485299e649ac3daf314b81f7429c11d8f6c2dacb3084c1c1888e3deff1a053f7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 29, 2026.
Transparency logRelease files / static_magic-5.45.0-cp314-cp314-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
| Download URL | static_magic-5.45.0-cp314-cp314-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl |
|---|---|
| Size | 766.0 kB |
| Tags | CPython 3.14 Linux glibc 2.17+ ARM64 Linux glibc 2.28+ ARM64 |
|
SHA-256 checksum How to use checksums |
ee3f030ed15af0073617a84b3d382c26d80c8df99a2006ad18719696513e30af
|
|
BLAKE2b-256 checksum How to use checksums |
751515f3a77fd25c0bf044c6471d013d07928e870935cdcdc0e38880e4dbd8fc
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 29, 2026.
Transparency logRelease files / static_magic-5.45.0-cp314-cp314-macosx_11_0_arm64.whl
| Download URL | static_magic-5.45.0-cp314-cp314-macosx_11_0_arm64.whl |
|---|---|
| Size | 523.9 kB |
| Tags | CPython 3.14 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
0c3896ffd0d612d4412eba6e73d91023766f9c909d4706a6c06c50c8ee074f2b
|
|
BLAKE2b-256 checksum How to use checksums |
bc5339cac0cfb565c37d400619bdd5276133c527ed61dd9cdb57aab7fd283a3d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 29, 2026.
Transparency logRelease files / static_magic-5.45.0-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
| Download URL | static_magic-5.45.0-cp313-cp313-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl |
|---|---|
| Size | 770.5 kB |
| Tags | CPython 3.13 Linux glibc 2.17+ x86-64 Linux glibc 2.28+ x86-64 |
|
SHA-256 checksum How to use checksums |
6f80975ab787675c8bec6345d437acfe92b3dde9bb2f11f6af22e077b39af603
|
|
BLAKE2b-256 checksum How to use checksums |
8a54719a9cdfe4969b298262eed1be5a0826d99efbf4859a483ac9319d90a42d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 29, 2026.
Transparency logRelease files / static_magic-5.45.0-cp313-cp313-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
| Download URL | static_magic-5.45.0-cp313-cp313-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl |
|---|---|
| Size | 765.8 kB |
| Tags | CPython 3.13 Linux glibc 2.17+ ARM64 Linux glibc 2.28+ ARM64 |
|
SHA-256 checksum How to use checksums |
348ef7c65d813130237b7bf2a2b953d98d02794867a3601b7ec5beada353d72f
|
|
BLAKE2b-256 checksum How to use checksums |
75e5806f8675620b732d9c55ef25e728f00bc8509b597d15366a7555f5f57443
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 29, 2026.
Transparency logRelease files / static_magic-5.45.0-cp313-cp313-macosx_11_0_arm64.whl
| Download URL | static_magic-5.45.0-cp313-cp313-macosx_11_0_arm64.whl |
|---|---|
| Size | 523.9 kB |
| Tags | CPython 3.13 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
90b9f09bfda71c52775d2a9e62e5129f55233efc0c31e02a2375c4f7b0a571f8
|
|
BLAKE2b-256 checksum How to use checksums |
50d6d69f19127573c378b4dc2a49c995ca5be171be2a18ab1fe4cec76e78ad71
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 29, 2026.
Transparency logRelease files / static_magic-5.45.0-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl
| Download URL | static_magic-5.45.0-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.manylinux_2_28_x86_64.whl |
|---|---|
| Size | 770.5 kB |
| Tags | CPython 3.12 Linux glibc 2.17+ x86-64 Linux glibc 2.28+ x86-64 |
|
SHA-256 checksum How to use checksums |
1a63eb2cb5aa5b027324349f92316bec7b2c3d342808d1e978a4f29512f8659f
|
|
BLAKE2b-256 checksum How to use checksums |
2dae717571223bd4d047d208f0143e2a569dcbc24dabf7667b4aab42e104d4f6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 29, 2026.
Transparency logRelease files / static_magic-5.45.0-cp312-cp312-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl
| Download URL | static_magic-5.45.0-cp312-cp312-manylinux2014_aarch64.manylinux_2_17_aarch64.manylinux_2_28_aarch64.whl |
|---|---|
| Size | 765.7 kB |
| Tags | CPython 3.12 Linux glibc 2.17+ ARM64 Linux glibc 2.28+ ARM64 |
|
SHA-256 checksum How to use checksums |
5ec7f43405d1b8f15c127bb55809a841fe9c1322b2ece2dd57c29359f9c7100b
|
|
BLAKE2b-256 checksum How to use checksums |
630f61c2c08f4782119f895db58b9d013143ae368563009c24254db48c307857
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 29, 2026.
Transparency logRelease files / static_magic-5.45.0-cp312-cp312-macosx_11_0_arm64.whl
| Download URL | static_magic-5.45.0-cp312-cp312-macosx_11_0_arm64.whl |
|---|---|
| Size | 523.9 kB |
| Tags | CPython 3.12 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
9e6397be5454cc899491c55a51f5a3708bb2deba26d008812b8966bcca2d045e
|
|
BLAKE2b-256 checksum How to use checksums |
94c4c44455d211c6269bbc7efa15740a7fe51c651f3eaeed8501aabec27ad662
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 29, 2026.
Transparency logSecurity for static-magic 5.45.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
All 13 files in this release have been uploaded with provenance metadata.
Attestation for static_magic-5.45.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 29, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.