Install `python-magic-bin` on Windows automatically
Is your feature request related to a problem? Please describe.
AFAIK, on Windows file type detection is broken because only python-magic is installed, but python-magic-bin is needed for libmagic to be load.
Issue #234 also talked about this problem, but it seems like the PR that closed it only skipped the failed test on Windows, and for Windows users, python-magic-bin still needs to be manually installed after running pip install unstructured.
@WHALEEYE what behavior do you observe before you install python-magic-bin? Like what specifically do you mean by "broken"?
For example, is an exception raised? If so what exception, including full traceback?
Or are certain filetypes just not recognized or perhaps misidentified?
@scanny Thanks for your reply! I installed unstructured IO on my Windows system with poetry, with such an entry in my pyproject.toml.
unstructured = { extras = ["all-docs"], version = "^0.15.0", optional = true }
When I'm running the example of using dropbox as the data source (I installed unstructure[dropbox] for this), it downloaded all the files successfully, but after this (I guess it was during partitioning), it raised an error like this
unstructured.ingest.error.PartitionError: Error in partitioning content: failed to find libmagic. Check your installation
I checked with pip show python-magic-bin and it showed nothing. Then I installed python-magic-bin manually and it worked well, so I guess this is not installed automatically on Windows?
@WHALEEYE I need the full stack trace, not just the error.
2024-07-25 16:43:44,109 SpawnPoolWorker-5 ERROR failed to partition doc: {"processor_config": {"reprocess": false, "verbose": true, "work_dir": "C:\\Users\\Isaac\\.cache\\unstructured\\ingest\\pipeline", "output_dir": "dropbox-output", "num_processes": 2, "raise_on_error": false}, "read_config": {"download_dir": "C:\\Users\\Isaac\\.cache\\unstructured\\ingest\\dropbox\\7e8b2a9ad6", "re_download": false, "preserve_downloads": false, "download_only": false, "max_docs": null}, "connector_config": {"remote_url": "dropbox://unstructured/", "uncompress": false, "recursive": true, "file_glob": null, "access_config": {"token": "*******"}, "protocol": "dropbox", "path_without_protocol": "unstructured/", "dir_path": "unstructured", "file_path": ""}, "_source_metadata": {"date_created": null, "date_modified": null, "version": "43212403970773838955741063541098233225", "source_url": "dropbox:///unstructured/options.ini", "exists": true, "permissions_data": null}, "_date_processed": null, "remote_file_path": "/unstructured/options.ini", "registry_name": "dropbox", "base_filename": "\\options.ini", "filename": "C:\\Users\\Isaac\\.cache\\unstructured\\ingest\\dropbox\\7e8b2a9ad6\\options.ini", "_output_filename": "dropbox-output\\options.ini.json", "record_locator": {"protocol": "dropbox", "remote_file_path": "/unstructured/options.ini"}, "unique_id": "C:\\Users\\Isaac\\.cache\\unstructured\\ingest\\dropbox\\7e8b2a9ad6\\options.ini", "date_created": null, "date_modified": null, "date_processed": null, "exists": true, "permissions_data": null, "version": "43212403970773838955741063541098233225", "source_url": "dropbox:///unstructured/options.ini"}, Error in partitioning content: failed to find libmagic. Check your installation
Traceback (most recent call last):
File "D:\Cache\pypoetry\virtualenvs\camel-ai-C9nw6eu8-py3.10\lib\site-packages\unstructured\ingest\error.py", line 19, in wrapper
return f(*args, **kwargs)
File "D:\Cache\pypoetry\virtualenvs\camel-ai-C9nw6eu8-py3.10\lib\site-packages\unstructured\ingest\interfaces.py", line 559, in partition_file
elements = partition(
File "D:\Cache\pypoetry\virtualenvs\camel-ai-C9nw6eu8-py3.10\lib\site-packages\unstructured\partition\auto.py", line 288, in partition
filetype = detect_filetype(
File "D:\Cache\pypoetry\virtualenvs\camel-ai-C9nw6eu8-py3.10\lib\site-packages\unstructured\file_utils\filetype.py", line 57, in detect_filetype
import magic
File "D:\Cache\pypoetry\virtualenvs\camel-ai-C9nw6eu8-py3.10\lib\site-packages\magic\__init__.py", line 209, in <module>
libmagic = loader.load_lib()
File "D:\Cache\pypoetry\virtualenvs\camel-ai-C9nw6eu8-py3.10\lib\site-packages\magic\loader.py", line 49, in load_lib
raise ImportError('failed to find libmagic. Check your installation')
ImportError: failed to find libmagic. Check your installation
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "D:\Cache\pypoetry\virtualenvs\camel-ai-C9nw6eu8-py3.10\lib\site-packages\unstructured\ingest\pipeline\partition.py", line 48, in run
elements = doc.process_file(
File "D:\Cache\pypoetry\virtualenvs\camel-ai-C9nw6eu8-py3.10\lib\site-packages\unstructured\ingest\interfaces.py", line 597, in process_file
elements = self.partition_file(partition_config=partition_config, **partition_kwargs)
File "D:\Cache\pypoetry\virtualenvs\camel-ai-C9nw6eu8-py3.10\lib\site-packages\unstructured\ingest\error.py", line 22, in wrapper
raise cls(cls.error_string.format(str(error))) from error
unstructured.ingest.error.PartitionError: Error in partitioning content: failed to find libmagic. Check your installation
2024-07-25 16:43:44,153 MainProcess INFO No files to process after partitioning
Ahh, okay, interesting. What version of unstructured are you running on?
Line 57 of unstructured\file_utils\filetype.py hasn't been import magic (as it is in the stack-trace) for quite a long time :)
Try upgrading to the latest (0.15.0) and let us know what happens with that version.
Interesting. I saw on the main branch this line does not exist anymore and I guess this problem has been fixed. However, this seems like a recent change and in the latest release (0.15.0) this line still exists. Therefore I think this will work in the next release and I'll update by then :)
@WHALEEYE I'll close this then, in the expectation things will work after you upgrade. But please re-open it it's still giving you trouble after that and we'll have another look :)
@scanny I just did a double check on this and noticed that unstructured io will try to take fallback strategies when libmagic is not installed. However, seems like this fallback mechanism is not working in 0.15.0 (also the latest version on main), and my error is actually caused by this.
This line was changed in a recent commit, and it actually only detects the installation of python-magic package instead of the libmagic dependency.
https://github.com/Unstructured-IO/unstructured/blob/4e61acc1c6b924a4ff643a1cc145a3f59980d18c/unstructured/file_utils/filetype.py#L56
Therefore, as long as python-magic is installed, LIBMAGIC_AVAILABLE will always be True, and if libmagic is not installed, import magic will cause the error I met in this issue.
Ah, interesting, this is a very helpful observation, thanks @WHALEEYE, I'll look into this :)
@scanny
- https://github.com/Unstructured-IO/unstructured/pull/3828
- https://github.com/Unstructured-IO/unstructured/blob/b5ff79d8dbc4c86b9fec9f7632a6a6e2c20b5f17/unstructured/file_utils/filetype.py#L55
After committing with the issue below, an infinite bottleneck occurs at importlib.import_module("magic") when loading the library on Windows.
It is resolved by installing python-magic-bin, but I am leaving a mention because I judge that the commit you wrote did not solve the problem.