Search code examples
pythongoogle-colaboratorypython-magic

Error in trying to install/use Python-Magic in Google Colab


I am trying to install python-magic in Colab. Here is the Github of the library https://github.com/ahupp/python-magic#dependencies

When I try :

!pip install python-magic
import magic

This Results in

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-44-1461204615fc> in <module>()
      1 get_ipython().system('pip install python-magic')
----> 2 import magic

/usr/local/lib/python3.6/dist-packages/magic.py in <module>()
    179 if not libmagic or not libmagic._name:
    180     # It is better to raise an ImportError since we are importing magic module
--> 181     raise ImportError('failed to find libmagic.  Check your installation')
    182 
    183 magic_t = ctypes.c_void_p

ImportError: failed to find libmagic.  Check your installation

---------------------------------------------------------------------------
NOTE: If your import is failing due to a missing package, you can
manually install dependencies using either !pip or !apt.

To view examples of installing some common dependencies, click the
"Open Examples" button below.
---------------------------------------------------------------------------

I have tried every solution possible

This SO post says Exception Value:failed to find libmagic. Check your installation in windows 7

Your multiuploader form uses python-magic library possibly for filetype identification. However you probably do not have all the missing dependencies installed. Please install the missing dependencies. https://github.com/ahupp/python-magic#dependencies

But dependencies are not listed anymore.

I've also tried

pip install python_magic_bin-0.4.14-py2.py3-none-win_amd64.whl

and

pip install python_magic_bin-0.4.14-py2.py3-none-win32.whl

from this SO post

pip installing eyeD3 module. Failed to find libmagic

I tried

pip install python-magic-bin==0.4.14

I also tried

!brew install libmagic

But Colab doesn't seem to recognize brew


Solution

  • The way you install Libmagic on Colab is as follows:

    !apt-get install libmagic-dev
    !pip install python-magic
    

    Then, you can import it without a problem:

    import magic
    

    An exemplary Colab link