{"id":8496,"library":"pylibmagic","title":"pylibmagic","description":"pylibmagic is a lightweight Python package designed to simplify the installation and usage of the `libmagic` C library. It provides pre-compiled `libmagic` shared libraries via Python wheels, primarily addressing the common `ImportError: failed to find libmagic` encountered when using other `libmagic` bindings like `python-magic`. The current version is 0.5.0, with releases focusing on expanding Python version and architecture support.","status":"active","version":"0.5.0","language":"en","source_language":"en","source_url":"https://github.com/kratsg/pylibmagic","tags":["file-type-detection","libmagic","bindings","binary-dependencies","wheels"],"install":[{"cmd":"pip install pylibmagic","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"note":"pylibmagic makes the libmagic C library available; the 'magic' module itself is typically provided by the 'python-magic' package. You should import pylibmagic first to ensure libmagic is found, then import magic.","wrong":"import magic","symbol":"pylibmagic","correct":"import pylibmagic\nimport magic"}],"quickstart":{"code":"import pylibmagic\nimport magic\n\n# pylibmagic ensures the underlying libmagic library is available\n# You can inspect the data path it uses:\nprint(f\"pylibmagic data path: {pylibmagic.data}\")\n\n# Now, use the python-magic library as usual\ntry:\n    m = magic.Magic()\n    file_type = m.from_buffer(b\"hello world\")\n    print(f\"'hello world' is detected as: {file_type}\")\n    \n    # Example with a common file type\n    with open(\"temp_text.txt\", \"w\") as f:\n        f.write(\"This is a test file.\")\n    file_type_from_file = m.from_file(\"temp_text.txt\")\n    print(f\"'temp_text.txt' is detected as: {file_type_from_file}\")\n    import os\n    os.remove(\"temp_text.txt\")\nexcept Exception as e:\n    print(f\"An error occurred: {e}\")\n    print(\"Ensure 'python-magic' is also installed (`pip install python-magic`).\")","lang":"python","description":"This quickstart demonstrates how to use `pylibmagic` in conjunction with the `python-magic` library. `pylibmagic` ensures that the `libmagic` C library is available for `python-magic` to use, solving common installation issues. After importing `pylibmagic`, you can then proceed to use the `magic` module from `python-magic` as intended."},"warnings":[{"fix":"Ensure both `pylibmagic` and `python-magic` are installed: `pip install pylibmagic python-magic`.","message":"pylibmagic does not provide the 'magic' module directly. Its purpose is to bundle and make the underlying `libmagic` C library accessible. To use the Python bindings for `libmagic`, you typically need to install `python-magic` separately.","severity":"gotcha","affected_versions":"All versions"},{"fix":"On Debian/Ubuntu, install `libmagic1` or `libmagic-dev` (`sudo apt-get install libmagic1`). On macOS, install via Homebrew (`brew install libmagic`). For Windows, `python-magic-bin` might be a more robust solution, or ensure `libmagic` DLLs are correctly placed and match your Python's bitness.","message":"On platforms or architectures without pre-built wheels, `pylibmagic` installation might fail or require system-level `libmagic` installation.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Ensure that the `libmagic` DLLs and your Python installation are both either 32-bit or 64-bit. Consider using `pip install python-magic-bin` which often includes the correct binaries for Windows.","message":"Using a 32-bit `libmagic` DLL with a 64-bit Python (or vice-versa) on Windows can lead to `WindowsError: [Error 193]`.","severity":"gotcha","affected_versions":"All versions on Windows"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Install `pylibmagic` to provide the `libmagic` binaries, and ensure `python-magic` is also installed: `pip install pylibmagic python-magic`. For non-wheel scenarios or persistent issues, install system-level `libmagic` (e.g., `sudo apt-get install libmagic1` on Linux, `brew install libmagic` on macOS).","cause":"The Python `magic` module (from `python-magic`) cannot locate the underlying `libmagic` C library. This is the primary problem `pylibmagic` aims to solve.","error":"ImportError: failed to find libmagic. Check your installation"},{"fix":"Install the `python-magic` library: `pip install python-magic`. Then, import `pylibmagic` before `magic` in your code: `import pylibmagic; import magic`.","cause":"The user has installed `pylibmagic` but expects it to provide the `magic` module directly. The `magic` module is actually provided by the `python-magic` package.","error":"ModuleNotFoundError: No module named 'magic'"},{"fix":"Ensure your `libmagic` DLLs match your Python installation's bitness. A common fix is to install `python-magic-bin` (`pip install python-magic-bin`), which bundles compatible binaries.","cause":"This error on Windows typically indicates a mismatch in the bitness between the installed Python interpreter and the `libmagic` DLLs being loaded (e.g., 64-bit Python trying to use 32-bit DLLs).","error":"WindowsError: [Error 193] %1 is not a valid Win32 application"}]}