Skip to main content

⚠️ Disclaimer

This package python-magic-standalone is an unofficial distribution of python-magic. It is built directly from @ddelange’s abi3-wheels branch, which is the subject of open PR #294. It provides prebuilt wheels with libmagic bundled for Windows, macOS, and Linux.

The sole purpose of this fork is to make the wheels available on PyPI, since the PR has been open for quite a while. Once the open PR is merged, please refer back to python-magic.

It can be installed with any of the following commands, depending on the tool you use;

# pip
pip install python-magic-standalone

# uv
uv add python-magic-standalone

# poetry
poetry add python-magic-standalone

python-magic

PyPI version ci Join the chat at https://gitter.im/ahupp/python-magic

python-magic is a Python interface to the libmagic file type identification library. libmagic identifies file types by checking their headers according to a predefined list of file types. This functionality is exposed to the command line by the Unix command file.

Usage

>>> import magic
>>> magic.from_file("testdata/test.pdf")
'PDF document, version 1.2'
# recommend using at least the first 2048 bytes, as less can produce incorrect identification
>>> magic.from_buffer(open("testdata/test.pdf", "rb").read(2048))
'PDF document, version 1.2'
>>> magic.from_file("testdata/test.pdf", mime=True)
'application/pdf'

There is also a Magic class that provides more direct control, including overriding the magic database file and turning on character encoding detection. The identification methods from_file, from_buffer, and from_descriptor serialize access with a per-instance lock, so those methods can be called from multiple threads on the same instance. This does not cover concurrent parameter changes through setparam or direct access to the libmagic cookie.

>>> f = magic.Magic(uncompress=True)
>>> f.from_file('testdata/test.gz')
'ASCII text (gzip compressed data, was "test", last modified: Sat Jun 28 21:32:52 2008, from Unix)'

You can also combine the flag options:

>>> f = magic.Magic(mime=True, uncompress=True)
>>> f.from_file('testdata/test.gz')
'text/plain'

Installation

This module is a CDLL wrapper around the libmagic C library. The current stable version of python-magic is available on PyPI and can be installed by running:

pip install python-magic

Compiled libmagic and the magic database come bundled in the wheels on PyPI. You can use your own magic.mgc database by setting the MAGIC environment variable, or by using magic.Magic(magic_file='path/to/magic.mgc'). If you want to compile your own libmagic, circumvent the wheels by explicitly installing from source:

pip install python-magic --no-binary python-magic

For systems not supported by the wheels, pip installs from source, which requires libmagic to be installed separately:

Linux

The Linux wheels should run on most systems out of the box.

Depending on your system and CPU architecture, there might be no compatible wheel uploaded. However, precompiled libmagic might still be available for your system:

# Debian/Ubuntu
apt-get update && apt-get install -y libmagic1
# Alpine
apk add --update libmagic
# RHEL
dnf install file-libs

Windows

The DLLs that are bundled in the Windows wheels are built from source with the MSYS2 toolchain (mingw32, ucrt64 and clangarm64).

OSX

The Mac wheels are compiled with maximum backward compatibility. For older Macs, you'll need to install libmagic from source:

# homebrew
brew install libmagic
# macports
port install file

If python-magic fails to load the library it may be in a non-standard location, in which case you can set the environment variable DYLD_LIBRARY_PATH to point to it.

SmartOS:

  • Install libmagic for source: https://github.com/file/file
  • Depending on your ./configure --prefix settings set your LD_LIBRARY_PATH to /lib

Troubleshooting

  • 'MagicException: could not find any magic files!': some installations of libmagic do not correctly point to their magic database file. Try specifying the path to the file explicitly in the constructor: magic.Magic(magic_file='path/to/magic.mgc').

  • 'WindowsError: [Error 193] %1 is not a valid Win32 application': Attempting to run the 32-bit libmagic DLL in a 64-bit build of python will fail with this error. Here are 64-bit builds of libmagic for windows: https://github.com/pidydx/libmagicwin64. Newer version can be found here: https://github.com/nscaife/file-windows.

  • 'WindowsError: exception: access violation writing 0x00000000 ' This may indicate you are mixing Windows Python and Cygwin Python. Make sure your libmagic and python builds are consistent.

Bug Reports

python-magic is a thin layer over the libmagic C library. Historically, most bugs that have been reported against python-magic are actually bugs in libmagic; libmagic bugs can be reported on their tracker here: https://bugs.astron.com/my_view_page.php. If you're not sure where the bug lies feel free to file an issue on GitHub and I can triage it.

Running the tests

We use the tox test runner which can be installed with python -m pip install tox.

To run tests locally across all available python versions:

python -m tox

Or to run just against a single version:

python -m tox py

To run the tests across a variety of linux distributions (depends on Docker):

./test/run_all_docker_test.sh

libmagic python API compatibility

The python bindings shipped with libmagic use a module name that conflicts with this package. To work around this, python-magic includes a compatibility layer for the libmagic API. See COMPAT.md for a guide to libmagic / python-magic compatibility.

Versioning

Minor version bumps should be backwards compatible. Major bumps are not.

Author

Written by Adam Hupp in 2001 for a project that never got off the ground. It originally used SWIG for the C library bindings, but switched to ctypes once that was part of the python standard library.

You can contact me via my website or GitHub.

License

python-magic is distributed under the MIT license. See the included LICENSE file for details.

I am providing code in the repository to you under an open source license. Because this is my personal repository, the license you receive to my code is from me and not my employer (Facebook).

Metadata

Release files for python-magic-standalone 0.4.29

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distributions (wheels)

Table of built distributions (wheels) for python-magic-standalone 0.4.29
File Actions
python_magic_standalone-0.4.29-py2.py3-none-win_arm64.whl Python 2, Python 3 none Windows ARM64 Details for python_magic_standalone-0.4.29-py2.py3-none-win_arm64.whl
python_magic_standalone-0.4.29-py2.py3-none-win_amd64.whl Python 2, Python 3 none Windows x86-64 Details for python_magic_standalone-0.4.29-py2.py3-none-win_amd64.whl
python_magic_standalone-0.4.29-py2.py3-none-win32.whl Python 2, Python 3 none Windows x86-32 Details for python_magic_standalone-0.4.29-py2.py3-none-win32.whl
python_magic_standalone-0.4.29-py2.py3-none-musllinux_1_1_x86_64.whl Python 3, Python 2 none Linux musl 1.1+ x86-64 Details for python_magic_standalone-0.4.29-py2.py3-none-musllinux_1_1_x86_64.whl
python_magic_standalone-0.4.29-py2.py3-none-musllinux_1_1_s390x.whl Python 2, Python 3 none Linux musl 1.1+ IBM System/390x Details for python_magic_standalone-0.4.29-py2.py3-none-musllinux_1_1_s390x.whl
python_magic_standalone-0.4.29-py2.py3-none-musllinux_1_1_ppc64le.whl Python 3, Python 2 none Linux musl 1.1+ PowerPC 64-le Details for python_magic_standalone-0.4.29-py2.py3-none-musllinux_1_1_ppc64le.whl
python_magic_standalone-0.4.29-py2.py3-none-musllinux_1_1_i686.whl Python 2, Python 3 none Linux musl 1.1+ x86-32 Details for python_magic_standalone-0.4.29-py2.py3-none-musllinux_1_1_i686.whl
python_magic_standalone-0.4.29-py2.py3-none-musllinux_1_1_aarch64.whl Python 3, Python 2 none Linux musl 1.1+ ARM64 Details for python_magic_standalone-0.4.29-py2.py3-none-musllinux_1_1_aarch64.whl
python_magic_standalone-0.4.29-py2.py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl Python 2, Python 3 none Linux glibc 2.17+ x86-64 Details for python_magic_standalone-0.4.29-py2.py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
python_magic_standalone-0.4.29-py2.py3-none-manylinux_2_17_s390x.manylinux2014_s390x.whl Python 3, Python 2 none Linux glibc 2.17+ IBM System/390x Details for python_magic_standalone-0.4.29-py2.py3-none-manylinux_2_17_s390x.manylinux2014_s390x.whl
python_magic_standalone-0.4.29-py2.py3-none-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl Python 2, Python 3 none Linux glibc 2.17+ PowerPC 64-le Details for python_magic_standalone-0.4.29-py2.py3-none-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl
python_magic_standalone-0.4.29-py2.py3-none-manylinux_2_17_i686.manylinux2014_i686.whl Python 2, Python 3 none Linux glibc 2.17+ x86-32 Details for python_magic_standalone-0.4.29-py2.py3-none-manylinux_2_17_i686.manylinux2014_i686.whl
python_magic_standalone-0.4.29-py2.py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl Python 2, Python 3 none Linux glibc 2.17+ ARM64 Details for python_magic_standalone-0.4.29-py2.py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
python_magic_standalone-0.4.29-py2.py3-none-macosx_11_0_arm64.whl Python 3, Python 2 none macOS 11.0+ ARM64 Details for python_magic_standalone-0.4.29-py2.py3-none-macosx_11_0_arm64.whl
python_magic_standalone-0.4.29-py2.py3-none-macosx_10_9_x86_64.whl Python 2, Python 3 none macOS 10.9+ x86-64 Details for python_magic_standalone-0.4.29-py2.py3-none-macosx_10_9_x86_64.whl

Total release size: 15.1 MB

Release files / python_magic_standalone-0.4.29-py2.py3-none-win_arm64.whl

Download URL python_magic_standalone-0.4.29-py2.py3-none-win_arm64.whl
Size 1.5 MB
Tags Python 2 Python 3 Windows ARM64
SHA-256 checksum
How to use checksums
fba6b07889c28acc94caec9506df60c529a494320aa443b5b97570a54ba101a5
BLAKE2b-256 checksum
How to use checksums
0b3c62632d9768685784f0b3a9ab72365a1f4daa100b9bc92738536859d83d78
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.0.0 CPython/3.12.16

Release files / python_magic_standalone-0.4.29-py2.py3-none-win_amd64.whl

Download URL python_magic_standalone-0.4.29-py2.py3-none-win_amd64.whl
Size 1.6 MB
Tags Python 2 Python 3 Windows x86-64
SHA-256 checksum
How to use checksums
ee9767392b66d0d63bc2ec6c79930308585f5b5bcb7f749eb7b68fa1e5bbdfc2
BLAKE2b-256 checksum
How to use checksums
421bb2242d6100f4248ad99b722794734690ed0af7da947cb9d332ad6af24de7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.0.0 CPython/3.12.16

Release files / python_magic_standalone-0.4.29-py2.py3-none-win32.whl

Download URL python_magic_standalone-0.4.29-py2.py3-none-win32.whl
Size 1.7 MB
Tags Python 2 Python 3 Windows x86-32
SHA-256 checksum
How to use checksums
264a93acecb94a4c79b7474054c37a709e27e63001a5207290fcddb05a9abe17
BLAKE2b-256 checksum
How to use checksums
5df7dfeb397a6c27eaeef94ee541a25b57ea4be08f3fbbd622eb9d92b38d6b7f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.0.0 CPython/3.12.16

Release files / python_magic_standalone-0.4.29-py2.py3-none-musllinux_1_1_x86_64.whl

Download URL python_magic_standalone-0.4.29-py2.py3-none-musllinux_1_1_x86_64.whl
Size 850.7 kB
Tags Linux musl 1.1+ x86-64 Python 2 Python 3
SHA-256 checksum
How to use checksums
b504e4dcb7ca25769e8b3bb1d04db23a6acb6ea52acf5ced2b87375818674f4f
BLAKE2b-256 checksum
How to use checksums
3a4f25ef40e280d5fed8cabe966bffa8f5e46d94151226bd230e7c131edaf877
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.0.0 CPython/3.12.16

Release files / python_magic_standalone-0.4.29-py2.py3-none-musllinux_1_1_s390x.whl

Download URL python_magic_standalone-0.4.29-py2.py3-none-musllinux_1_1_s390x.whl
Size 865.3 kB
Tags Linux musl 1.1+ IBM System/390x Python 2 Python 3
SHA-256 checksum
How to use checksums
690aac176b1824366010f4bf1618b009c34b19f45ea713039cd391566c58ace5
BLAKE2b-256 checksum
How to use checksums
822d9887a055d20330681bf79dfb2950a2e6fed7cd20c011f9d2a1803d203c25
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.0.0 CPython/3.12.16

Release files / python_magic_standalone-0.4.29-py2.py3-none-musllinux_1_1_ppc64le.whl

Download URL python_magic_standalone-0.4.29-py2.py3-none-musllinux_1_1_ppc64le.whl
Size 868.2 kB
Tags Linux musl 1.1+ PowerPC 64-le Python 2 Python 3
SHA-256 checksum
How to use checksums
155a8236da45125de10020d3175cba861a72a5de1ae939bd93d41152c96e86bc
BLAKE2b-256 checksum
How to use checksums
e895a9319ce23c6bdb524db726d6ae3ea62f92fca4dd587bb802248027e5fef4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.0.0 CPython/3.12.16

Release files / python_magic_standalone-0.4.29-py2.py3-none-musllinux_1_1_i686.whl

Download URL python_magic_standalone-0.4.29-py2.py3-none-musllinux_1_1_i686.whl
Size 841.7 kB
Tags Linux musl 1.1+ x86-32 Python 2 Python 3
SHA-256 checksum
How to use checksums
fd9491444121da492774bb4def36047d1baac3463707925e73afa653b795c44d
BLAKE2b-256 checksum
How to use checksums
922c22a2efc16b153ed45706de10196843762f77fc2f83dc71003eff0403108e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.0.0 CPython/3.12.16

Release files / python_magic_standalone-0.4.29-py2.py3-none-musllinux_1_1_aarch64.whl

Download URL python_magic_standalone-0.4.29-py2.py3-none-musllinux_1_1_aarch64.whl
Size 851.8 kB
Tags Linux musl 1.1+ ARM64 Python 2 Python 3
SHA-256 checksum
How to use checksums
8ebc179aeb86d5ca01d024fdafc7f9166c7f8d6c77207e251c42b3f116402cf3
BLAKE2b-256 checksum
How to use checksums
ad828dc9d6f71a7d9769fe007279a56ff5dd4a343db3ef01dcb755e4bd019e3d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.0.0 CPython/3.12.16

Release files / python_magic_standalone-0.4.29-py2.py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL python_magic_standalone-0.4.29-py2.py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 860.0 kB
Tags Linux glibc 2.17+ x86-64 Python 2 Python 3
SHA-256 checksum
How to use checksums
d6225dfd64a5e1bb0443a802a2cc9b25fe2b56850de4522ce792ee3851409232
BLAKE2b-256 checksum
How to use checksums
d41bad2fd5aae832bc53bf1f209cfad428a5733b9f88502e836c5335e27f5015
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.0.0 CPython/3.12.16

Release files / python_magic_standalone-0.4.29-py2.py3-none-manylinux_2_17_s390x.manylinux2014_s390x.whl

Download URL python_magic_standalone-0.4.29-py2.py3-none-manylinux_2_17_s390x.manylinux2014_s390x.whl
Size 876.9 kB
Tags Linux glibc 2.17+ IBM System/390x Python 2 Python 3
SHA-256 checksum
How to use checksums
c7cae8ffdfc6e05c1c915981acb2518f4ba6874f5b6be80dccbf368db2749be9
BLAKE2b-256 checksum
How to use checksums
400082b5e96b3609ff63906cd896f5dfa27114f00f53ed3389b0d4115fdc4944
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.0.0 CPython/3.12.16

Release files / python_magic_standalone-0.4.29-py2.py3-none-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl

Download URL python_magic_standalone-0.4.29-py2.py3-none-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl
Size 879.8 kB
Tags Linux glibc 2.17+ PowerPC 64-le Python 2 Python 3
SHA-256 checksum
How to use checksums
4ab2c2429432156ed54f26d7adb3e79d9efebbd7e64c9b1ffadae42610c322cd
BLAKE2b-256 checksum
How to use checksums
95540e26724490ab88bb21273e3cffa7dd4e5291023c1d6ab87ef2303aa2cd57
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.0.0 CPython/3.12.16

Release files / python_magic_standalone-0.4.29-py2.py3-none-manylinux_2_17_i686.manylinux2014_i686.whl

Download URL python_magic_standalone-0.4.29-py2.py3-none-manylinux_2_17_i686.manylinux2014_i686.whl
Size 847.2 kB
Tags Linux glibc 2.17+ x86-32 Python 2 Python 3
SHA-256 checksum
How to use checksums
4cd307450db24686b6265b5806b3951f21a53031b84777b05e60daf08e15f205
BLAKE2b-256 checksum
How to use checksums
4dd36bcbbf8f8a0b7e39a06df75ce5c9bfe920e833cbde9303d9ab1284e6d1cf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.0.0 CPython/3.12.16

Release files / python_magic_standalone-0.4.29-py2.py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl

Download URL python_magic_standalone-0.4.29-py2.py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Size 860.8 kB
Tags Linux glibc 2.17+ ARM64 Python 2 Python 3
SHA-256 checksum
How to use checksums
0c69468599e22f0264c82a379b9b5ac30e8c149365ef7a1460ddce50f5c787f6
BLAKE2b-256 checksum
How to use checksums
4930399559fbab1d35a4e6515fba9ce8e857c8379080cd679ec438fc4c623d4b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.0.0 CPython/3.12.16

Release files / python_magic_standalone-0.4.29-py2.py3-none-macosx_11_0_arm64.whl

Download URL python_magic_standalone-0.4.29-py2.py3-none-macosx_11_0_arm64.whl
Size 614.6 kB
Tags Python 2 Python 3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
403d77db542f1bc91f1b311ec76952ae70ecf8fc11ff3c1a144df03cb5d9a542
BLAKE2b-256 checksum
How to use checksums
c5ce293f837f4350c66feee851a87498d9965990995983451999cc3c02ad999e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.0.0 CPython/3.12.16

Release files / python_magic_standalone-0.4.29-py2.py3-none-macosx_10_9_x86_64.whl

Download URL python_magic_standalone-0.4.29-py2.py3-none-macosx_10_9_x86_64.whl
Size 1.0 MB
Tags Python 2 Python 3 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
2fa0b1d4d64f40b98f413c9d681bcdae1cc50026ac97158ea73efdaff548a436
BLAKE2b-256 checksum
How to use checksums
c776804f83aa339ed550042f660f35b50b9cd46f5dc3bf63d5d373ed67fa4aee
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.0.0 CPython/3.12.16

Security for python-magic-standalone 0.4.29

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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A 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.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.4.29 This release

15 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page