{"id":8554,"library":"python-magic-bin","title":"Python Magic (Binary Bundled)","description":"python-magic-bin is a Python wrapper for `libmagic`, the file type identification library. Unlike the original `python-magic` package, `python-magic-bin` bundles the `libmagic` binary and its associated magic database, making it easier to install and use, especially on platforms like Windows where `libmagic` is not natively available. It currently stands at version 0.4.14 and has a slow release cadence, primarily for bug fixes or compatibility updates.","status":"active","version":"0.4.14","language":"en","source_language":"en","source_url":"https://github.com/julian-r/python-magic-bin-extension","tags":["file-type","magic","libmagic","mime-type","binary","windows-compatible"],"install":[{"cmd":"pip install python-magic-bin","lang":"bash","label":"Install `python-magic-bin`"}],"dependencies":[],"imports":[{"symbol":"Magic","correct":"import magic"}],"quickstart":{"code":"import magic\nimport os\n\n# Create a dummy file for demonstration\nwith open('test_file.txt', 'w') as f:\n    f.write('This is a test file.')\n\n# Instantiate Magic to get MIME types\nm = magic.Magic(mime=True)\n\n# Identify a local file\nfile_path = 'test_file.txt'\nfile_type = m.from_file(file_path)\nprint(f\"File '{file_path}' is of type: {file_type}\")\n\n# Clean up the dummy file\nos.remove(file_path)","lang":"python","description":"Demonstrates how to import and use `python-magic-bin` to identify the MIME type of a local file. The `magic.Magic()` constructor is used, typically with `mime=True` to get standard MIME types."},"warnings":[{"fix":"Ensure you explicitly install either `python-magic` or `python-magic-bin`, but not both if you don't understand their differences. For easier setup, especially on Windows, `python-magic-bin` is usually preferred.","message":"Confusion with `python-magic` (without `-bin`). `python-magic-bin` bundles the `libmagic` library, making installation easier, especially on Windows. The original `python-magic` requires `libmagic` to be installed separately via your system's package manager (e.g., `apt-get install libmagic-dev` on Debian/Ubuntu). Using both or installing the wrong one is a common source of errors.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Be aware that new issues or detection problems might not be resolved quickly. Consider contributing to the project or evaluating the more actively maintained `python-magic` if system-level `libmagic` installation is not an issue for your environment.","message":"Limited Maintenance. The `python-magic-bin-extension` GitHub repository shows limited recent activity compared to its upstream `python-magic`. While functional, new features or rapid bug fixes are less likely, and it may not keep up with the latest `libmagic` features or bug fixes.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Check the `python-magic-bin` release notes for information on the bundled `libmagic` version. If you require the absolute latest `libmagic` features or security patches, consider using `python-magic` and installing the latest `libmagic` via your system package manager.","message":"Potential for Outdated Bundled `libmagic`. Since `libmagic` is bundled within `python-magic-bin`, updates to the underlying `libmagic` library are tied to `python-magic-bin` releases. This means the bundled version might lag behind the latest `libmagic` versions, potentially missing improved detections or security fixes.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Reinstall `python-magic-bin` to ensure the bundled binaries and data files are correctly placed. If using virtual environments, activate it before installing. Check for any environment variables (`MAGIC_FILE`, `PATH`) that might interfere with the library's ability to find its components.","cause":"`python-magic-bin` is failing to locate its bundled `libmagic` library or its associated magic database file, possibly due to installation issues, unusual environments, or permission problems.","error":"No such file or directory: 'libmagic.dll'"},{"fix":"Verify if the issue persists across different file types or environments. Try explicitly creating a `Magic` object without special flags, e.g., `magic.Magic(mime=True)`. If the problem persists, this might be a bug specific to the bundled `libmagic` version for your OS and may require upstream intervention or using a different `python-magic` variant.","cause":"This error indicates a mismatch or issue with the bundled `libmagic` and how it was compiled, or how it's being used with specific flags (like `-s` for symlink following).","error":"MagicException: b'File 0.4.14 is not compiled with utime. This causes problems with -s'"},{"fix":"Ensure you are instantiating the `Magic` class: `import magic; m = magic.Magic(mime=True); m.from_file(\"path/to/file\")`. Also, double-check that `python-magic-bin` is the *only* `magic` package installed and imported in your environment to avoid conflicts.","cause":"This usually happens when `import magic` is used, but the `magic` object is not instantiated, or an older/different `magic` library (not `python-magic-bin`) is imported that doesn't expose the same API.","error":"AttributeError: module 'magic' has no attribute 'from_file'"}]}