Skip to main content

File type identification using libmagic

Project description

⚠️ 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).

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

python_magic_standalone-0.4.28-py2.py3-none-win_amd64.whl (2.8 MB view details)

Uploaded Python 2Python 3Windows x86-64

python_magic_standalone-0.4.28-py2.py3-none-win32.whl (3.0 MB view details)

Uploaded Python 2Python 3Windows x86

python_magic_standalone-0.4.28-py2.py3-none-musllinux_1_1_x86_64.whl (821.8 kB view details)

Uploaded Python 2Python 3musllinux: musl 1.1+ x86-64

python_magic_standalone-0.4.28-py2.py3-none-musllinux_1_1_s390x.whl (836.3 kB view details)

Uploaded Python 2Python 3musllinux: musl 1.1+ s390x

python_magic_standalone-0.4.28-py2.py3-none-musllinux_1_1_ppc64le.whl (840.0 kB view details)

Uploaded Python 2Python 3musllinux: musl 1.1+ ppc64le

python_magic_standalone-0.4.28-py2.py3-none-musllinux_1_1_aarch64.whl (822.7 kB view details)

Uploaded Python 2Python 3musllinux: musl 1.1+ ARM64

python_magic_standalone-0.4.28-py2.py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (830.2 kB view details)

Uploaded Python 2Python 3manylinux: glibc 2.17+ x86-64

python_magic_standalone-0.4.28-py2.py3-none-manylinux_2_17_s390x.manylinux2014_s390x.whl (846.0 kB view details)

Uploaded Python 2Python 3manylinux: glibc 2.17+ s390x

python_magic_standalone-0.4.28-py2.py3-none-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (851.3 kB view details)

Uploaded Python 2Python 3manylinux: glibc 2.17+ ppc64le

python_magic_standalone-0.4.28-py2.py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (831.5 kB view details)

Uploaded Python 2Python 3manylinux: glibc 2.17+ ARM64

python_magic_standalone-0.4.28-py2.py3-none-macosx_11_0_arm64.whl (590.9 kB view details)

Uploaded Python 2Python 3macOS 11.0+ ARM64

python_magic_standalone-0.4.28-py2.py3-none-macosx_10_9_x86_64.whl (989.7 kB view details)

Uploaded Python 2Python 3macOS 10.9+ x86-64

File details

Details for the file python_magic_standalone-0.4.28-py2.py3-none-win_amd64.whl.

File metadata

File hashes

Hashes for python_magic_standalone-0.4.28-py2.py3-none-win_amd64.whl
Algorithm Hash digest
SHA256 d04b54adaa054c1a03385a446817c302e393c69a75ec21bc84edfad53d6aa194
MD5 21f9065b034fcae0f14f96c3fffe5ff8
BLAKE2b-256 7b5b0b2528ed4612846c5d1184a4b73236cf067b3029d380296a3e01a48ea5fe

See more details on using hashes here.

File details

Details for the file python_magic_standalone-0.4.28-py2.py3-none-win32.whl.

File metadata

File hashes

Hashes for python_magic_standalone-0.4.28-py2.py3-none-win32.whl
Algorithm Hash digest
SHA256 b7ad8f112d8ac668fa67c508d0e604cf80a32db46f4356e85ce77805204c8066
MD5 3b01947780f8d45ba6be17c064bea80e
BLAKE2b-256 a53c016fca7835b75b653a6df56ef4f85d7bf7795dcb8d8c9f7232b84c4925ff

See more details on using hashes here.

File details

Details for the file python_magic_standalone-0.4.28-py2.py3-none-musllinux_1_1_x86_64.whl.

File metadata

File hashes

Hashes for python_magic_standalone-0.4.28-py2.py3-none-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 aced80a8b2baef16df1cfdc09bf4f48f6e09ec32d72ccc763e4e4aaed8e9aaed
MD5 141603b6d280d03ab6b62bdf9e82880c
BLAKE2b-256 b8357039712e7c69d6c89b9642c8c42046bbab7005cbb842bfd992a0ee7ea87e

See more details on using hashes here.

File details

Details for the file python_magic_standalone-0.4.28-py2.py3-none-musllinux_1_1_s390x.whl.

File metadata

File hashes

Hashes for python_magic_standalone-0.4.28-py2.py3-none-musllinux_1_1_s390x.whl
Algorithm Hash digest
SHA256 b0db6b19378eb3774b1689ad88e1d80eb91cc9b1f1ce382d9170aa20add0e97a
MD5 7f4280ad18e4311ef21f1716ba21e81d
BLAKE2b-256 0dc50f65f3597d88ee75e695790c9f53443b13781bcf9b355d350d8607843b8d

See more details on using hashes here.

File details

Details for the file python_magic_standalone-0.4.28-py2.py3-none-musllinux_1_1_ppc64le.whl.

File metadata

File hashes

Hashes for python_magic_standalone-0.4.28-py2.py3-none-musllinux_1_1_ppc64le.whl
Algorithm Hash digest
SHA256 967efc703e1aeddcf0690639cea81d0313dbb933c3c6a803f2ed1c253b00a58d
MD5 5cfa9e080bbed0a39d7467e8babcb337
BLAKE2b-256 e5ecc80a6f2cdb78be33fad67aa1c6ba61e6e7083da48621e245299abf55c780

See more details on using hashes here.

File details

Details for the file python_magic_standalone-0.4.28-py2.py3-none-musllinux_1_1_aarch64.whl.

File metadata

File hashes

Hashes for python_magic_standalone-0.4.28-py2.py3-none-musllinux_1_1_aarch64.whl
Algorithm Hash digest
SHA256 80254d8b7d6fcdb4b632e8ab7d8ae88fcf451a190ecb87301800ea081429ba43
MD5 086ef0f4cfefc0ac95a8ba0a4c1bc76e
BLAKE2b-256 dedd35c75a09b5878774cb1cd3cc1d89afa10a2c2643d1666862c7e3fab9a5ae

See more details on using hashes here.

File details

Details for the file python_magic_standalone-0.4.28-py2.py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for python_magic_standalone-0.4.28-py2.py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 2b81d199d767b0c4282d143401b319759c438cb69fcbffb2c9ac611f358ca72f
MD5 723adab9c004d0564278fb29f63b3652
BLAKE2b-256 8114346cc51ac1894e26e72cc5bad6ee5b732b9221219deea415ab79a24d6738

See more details on using hashes here.

File details

Details for the file python_magic_standalone-0.4.28-py2.py3-none-manylinux_2_17_s390x.manylinux2014_s390x.whl.

File metadata

File hashes

Hashes for python_magic_standalone-0.4.28-py2.py3-none-manylinux_2_17_s390x.manylinux2014_s390x.whl
Algorithm Hash digest
SHA256 aee393366ef6ce349356fcad4178bc3acff541fb93a6d7147b578956e46f2700
MD5 3a0054da1209fcbb38825428fbd09741
BLAKE2b-256 79960a45b1a973e6eb353bf114f39856a9e0386ec3a73a0d7c71395d93ffa6fd

See more details on using hashes here.

File details

Details for the file python_magic_standalone-0.4.28-py2.py3-none-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl.

File metadata

File hashes

Hashes for python_magic_standalone-0.4.28-py2.py3-none-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl
Algorithm Hash digest
SHA256 3feb222eb090f0ffffb3243f584630aa10aba2e3466e97cdda766226f859b9f0
MD5 04c291d45b0143fc71e619592a6eba98
BLAKE2b-256 821701198853b6d54b677fd1b2c98fe9dbd983e46ab1e3e405520d6f09ae6009

See more details on using hashes here.

File details

Details for the file python_magic_standalone-0.4.28-py2.py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for python_magic_standalone-0.4.28-py2.py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 8b79bf5928c718d829d972461d567a1577a243e9a2824c54d87083a30beb1ec6
MD5 3a0d25858c84d69bbac221864680272e
BLAKE2b-256 f912f188883b5296226956667b6bcc10c434d736fa22824bcca1fd80dcb449d9

See more details on using hashes here.

File details

Details for the file python_magic_standalone-0.4.28-py2.py3-none-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for python_magic_standalone-0.4.28-py2.py3-none-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 3a31a4501c4593417641601fac1b57ed39470499ebe7e4fcbef85b41d5c71be5
MD5 9d65d83750453945a5f47ae02d6c939c
BLAKE2b-256 a38a79dace0456818bb5411de2af9d0a30abed2f860defddd9b7c4f8b5bcde8a

See more details on using hashes here.

File details

Details for the file python_magic_standalone-0.4.28-py2.py3-none-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for python_magic_standalone-0.4.28-py2.py3-none-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 4a8cf4595efe3cc64c00a506f0a780d372411dfdbf585476b887d7d77671c324
MD5 b810ddc691e11425287b3eb29d7e719c
BLAKE2b-256 3fe67e1f69e898b268289bde344a41677d84c045a6df665a2c1af78edd1f7878

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page