python-magic icon indicating copy to clipboard operation
python-magic copied to clipboard

Failure when importing python-magic in windows 10 python 3.9

Open GiuseppeLeviBo opened this issue 3 years ago • 4 comments

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 ?)

GiuseppeLeviBo avatar Jul 14 '21 23:07 GiuseppeLeviBo

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/

fxpaquette avatar Aug 03 '21 19:08 fxpaquette

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:

  1. Copy libraries from site-packages/magic/libmagic to current folder and magic will find them.
  2. Add site-packages/magic/libmagic to PATH.

Steps for reproduce issue in Windows:

  1. Make new virtualenv
  2. Install in virtualenv python-magic, python-magic-bin
  3. Try import magic in python.

av-gantimurov avatar Aug 18 '21 11:08 av-gantimurov

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 avatar Feb 06 '22 08:02 eight04

@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

prchristie avatar Jul 27 '22 05:07 prchristie

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?

k128 avatar Jul 26 '23 14:07 k128

Merging into https://github.com/ahupp/python-magic/issues/293

ahupp avatar Aug 25 '23 18:08 ahupp