python-magic
python-magic copied to clipboard
Failure when importing python-magic in windows 10 python 3.9
Hi I have installed the library via pip and received no warning then when I try to import it libmagic is not found: PS G:\Pythontest> pip install python-magic Collecting python-magic Downloading python_magic-0.4.24-py2.py3-none-any.whl (12 kB) Installing collected packages: python-magic Successfully installed python-magic-0.4.24 PS G:\Pythontest> python Python 3.9.6 (tags/v3.9.6:db3ff76, Jun 28 2021, 15:26:21) [MSC v.1929 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information.
import magic Traceback (most recent call last): File "
", line 1, in File "C:\Users\xxxxxx\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\magic_init_.py", line 208, in libmagic = loader.load_lib() File "C:\Users\xxxxxx\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\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
How to install the missing library and why pip has not installed it? (or at least failed to install ?)
On Windows, you can obtain the dll by installing python-magic-bin (additionally to python-magic) with pip install python-magic-bin
. Source: https://pypi.org/project/python-magic/
Same issue in Windows with installed python-magic-bin
and python3.
When I installed python-magic
and python-magic-bin
in virtualenv, it doesn't work too.
Problem with loading libmagic library in loader.py.
It tries to load libmagic.dll from current folder (./libmagic.dll) or tries find it in %PATH%.
But binary libraries from python-magic-bin installed in magic/libmagic folder.
Workarounds:
- Copy libraries from site-packages/magic/libmagic to current folder and magic will find them.
- Add site-packages/magic/libmagic to PATH.
Steps for reproduce issue in Windows:
- Make new virtualenv
- Install in virtualenv
python-magic
,python-magic-bin
- Try import magic in python.
I can still reproduce this issue with Python 3.10.2 + python-magic 0.4.25 + python-magic-bin 0.4.14.
If you install python-magic-bin
after python-magic
then everything works. Because python-magic-bin overwrites magic/__init__.py
and load DLL from libmagic folder.
If you install python-magic-bin
before python-magic
then it doesn't work.
If you only install python-magic-bin
then it also works. However, you can't install python-magic-bin
on linux.
Before this issue is fixed, I think you have to specify your dependencies as:
python-magic-bin; sys_platform == "win32" or sys_platform == "darwin"
python-magic; sys_platform != "win32" and sys_platform != "darwin"
@eight04 this does not work as the init.py file is where the code of python-magic
is located, so you lose all updates. Notably, from_buffer
will no longer work as intended as the version exposed by python-magic-bin
does not encode the buffer so everything is application/octet-stream
. The only method at the moment to retain current functionality is to add the libraries to PATH. This information should really be exposed somewhere extremely obvious, python-magic-bin
is recommended as the installation method for windows yet it does not work.
For now I will manually installing libmagic
onto windows with https://github.com/pidydx/libmagicwin64 and removing python-magic-bin
from my requirements
Thanks @eight04! Uninstalling and installing python-magic first fixed my issue. Any objections to just adding a note to the readme on the correct installation order?
Merging into https://github.com/ahupp/python-magic/issues/293