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. This is not recommended for general use. In particular, it's not safe for sharing across multiple threads and will fail throw if this is attempted.

>>> 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 by MSYS2 (mingw32 and mingw64).

For ARM64 Windows, you'll need to compile libmagic from source or install from MSYS2.

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

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.28
File
python_magic_standalone-0.4.28-py2.py3-none-win_amd64.whl Python 2, Python 3 none Windows x86-64 Details
python_magic_standalone-0.4.28-py2.py3-none-win32.whl Python 3, Python 2 none Windows x86-32 Details
python_magic_standalone-0.4.28-py2.py3-none-musllinux_1_1_x86_64.whl Python 3, Python 2 none Linux musl 1.1+ x86-64 Details
python_magic_standalone-0.4.28-py2.py3-none-musllinux_1_1_s390x.whl Python 3, Python 2 none Linux musl 1.1+ IBM System/390x Details
python_magic_standalone-0.4.28-py2.py3-none-musllinux_1_1_ppc64le.whl Python 3, Python 2 none Linux musl 1.1+ PowerPC 64-le Details
python_magic_standalone-0.4.28-py2.py3-none-musllinux_1_1_aarch64.whl Python 3, Python 2 none Linux musl 1.1+ ARM64 Details
python_magic_standalone-0.4.28-py2.py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl Python 2, Python 3 none Linux glibc 2.17+ x86-64 Details
python_magic_standalone-0.4.28-py2.py3-none-manylinux_2_17_s390x.manylinux2014_s390x.whl Python 3, Python 2 none Linux glibc 2.17+ IBM System/390x Details
python_magic_standalone-0.4.28-py2.py3-none-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl Python 2, Python 3 none Linux glibc 2.17+ PowerPC 64-le Details
python_magic_standalone-0.4.28-py2.py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl Python 3, Python 2 none Linux glibc 2.17+ ARM64 Details
python_magic_standalone-0.4.28-py2.py3-none-macosx_11_0_arm64.whl Python 2, Python 3 none macOS 11.0+ ARM64 Details
python_magic_standalone-0.4.28-py2.py3-none-macosx_10_9_x86_64.whl Python 3, Python 2 none macOS 10.9+ x86-64 Details

Total release size: 14.0 MB

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

Download URL python_magic_standalone-0.4.28-py2.py3-none-win_amd64.whl
Size 2.8 MB
Tags Python 2 Python 3 Windows x86-64
SHA-256 checksum
How to use checksums
d04b54adaa054c1a03385a446817c302e393c69a75ec21bc84edfad53d6aa194
BLAKE2b-256 checksum
How to use checksums
7b5b0b2528ed4612846c5d1184a4b73236cf067b3029d380296a3e01a48ea5fe
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.0.0 CPython/3.12.11

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

Download URL python_magic_standalone-0.4.28-py2.py3-none-win32.whl
Size 3.0 MB
Tags Python 2 Python 3 Windows x86-32
SHA-256 checksum
How to use checksums
b7ad8f112d8ac668fa67c508d0e604cf80a32db46f4356e85ce77805204c8066
BLAKE2b-256 checksum
How to use checksums
a53c016fca7835b75b653a6df56ef4f85d7bf7795dcb8d8c9f7232b84c4925ff
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.0.0 CPython/3.12.11

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

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

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

Download URL python_magic_standalone-0.4.28-py2.py3-none-musllinux_1_1_s390x.whl
Size 836.3 kB
Tags Linux musl 1.1+ IBM System/390x Python 2 Python 3
SHA-256 checksum
How to use checksums
b0db6b19378eb3774b1689ad88e1d80eb91cc9b1f1ce382d9170aa20add0e97a
BLAKE2b-256 checksum
How to use checksums
0dc50f65f3597d88ee75e695790c9f53443b13781bcf9b355d350d8607843b8d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.0.0 CPython/3.12.11

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

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

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

Download URL python_magic_standalone-0.4.28-py2.py3-none-musllinux_1_1_aarch64.whl
Size 822.7 kB
Tags Linux musl 1.1+ ARM64 Python 2 Python 3
SHA-256 checksum
How to use checksums
80254d8b7d6fcdb4b632e8ab7d8ae88fcf451a190ecb87301800ea081429ba43
BLAKE2b-256 checksum
How to use checksums
dedd35c75a09b5878774cb1cd3cc1d89afa10a2c2643d1666862c7e3fab9a5ae
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.0.0 CPython/3.12.11

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

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

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

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

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

Download URL python_magic_standalone-0.4.28-py2.py3-none-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl
Size 851.3 kB
Tags Linux glibc 2.17+ PowerPC 64-le Python 2 Python 3
SHA-256 checksum
How to use checksums
3feb222eb090f0ffffb3243f584630aa10aba2e3466e97cdda766226f859b9f0
BLAKE2b-256 checksum
How to use checksums
821701198853b6d54b677fd1b2c98fe9dbd983e46ab1e3e405520d6f09ae6009
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.0.0 CPython/3.12.11

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

Download URL python_magic_standalone-0.4.28-py2.py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Size 831.5 kB
Tags Linux glibc 2.17+ ARM64 Python 2 Python 3
SHA-256 checksum
How to use checksums
8b79bf5928c718d829d972461d567a1577a243e9a2824c54d87083a30beb1ec6
BLAKE2b-256 checksum
How to use checksums
f912f188883b5296226956667b6bcc10c434d736fa22824bcca1fd80dcb449d9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.0.0 CPython/3.12.11

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

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

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

Download URL python_magic_standalone-0.4.28-py2.py3-none-macosx_10_9_x86_64.whl
Size 989.7 kB
Tags Python 2 Python 3 macOS 10.9+ x86-64
SHA-256 checksum
How to use checksums
4a8cf4595efe3cc64c00a506f0a780d372411dfdbf585476b887d7d77671c324
BLAKE2b-256 checksum
How to use checksums
3fe67e1f69e898b268289bde344a41677d84c045a6df665a2c1af78edd1f7878
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.0.0 CPython/3.12.11

Release history Release notifications | RSS feed

This release

0.4.28 This release

12 release files

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