36

When I am trying to open django admin panel this error is showing:

ImportError at /admin/
failed to find libmagic.  Check your installation
Request Method: GET
Request URL:    Django Version: 1.5.1
Exception Type: ImportError
Exception Value:    failed to find libmagic.  Check your installation
Exception Location: C:\Python27\lib\site-packages\magic.py in <module>, line 131
Python Executable:  C:\Python27\python.exe
Python Version: 2.7.2
Python Path:
['C:\\Users\\farhan\\Downloads\\dist\\Django-1.5.1\\django\\bin\\mysite',
 'C:\\Python27\\lib\\site-packages\\distribute-0.6.49-py2.7.egg',
 'C:\\Windows\\system32\\python27.zip',
 'C:\\Python27\\DLLs',
 'C:\\Python27\\lib',
 'C:\\Python27\\lib\\plat-win',
 'C:\\Python27\\lib\\lib-tk',
 'C:\\Python27',
 'C:\\Python27\\lib\\site-packages',
 'C:\\Python27\\lib\\site-packages\\setuptools-0.6c11-py2.7.egg-info']
Server time:    Thu, 22 Aug 2013 13:19:01 +0600

Here is my traceback

JayRizzo
  • 3,234
  • 3
  • 33
  • 49
Akhter Al Amin
  • 852
  • 1
  • 11
  • 25

8 Answers8

25

Go here and then:

For Windoes 32bit, download the file "python_magic_bin-0.4.14-py2.py3-none-win32.whl" and run

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

For Windows 64bit, download "python_magic_bin-0.4.14-py2.py3-none-win_amd64.whl" and run

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

now python-magic works

enter image description here

EDIT: As suggested in the comments, this also works:

pip install python-magic-bin==0.4.14
Simon
  • 5,464
  • 6
  • 49
  • 85
Francesco Mantovani
  • 10,216
  • 13
  • 73
  • 113
  • Worked for me! This actually installed an older version (0.4.14) than I had (0.4.15) using `pip install python-magic`. – Lewistrick Aug 02 '18 at 11:05
  • This tells me `Requirement already satisfied: python-magic-bin in c:\python310\lib\site-packages (0.4.14)` but the error remains. What else can I do to resolve the issue? – Matt Nov 10 '22 at 10:21
  • Hi @Matt, you have to fist remove python_magic_bin and then reinstall following my guides – Francesco Mantovani Nov 10 '22 at 10:32
  • Hi Francesco, you meant `pip uninstall python_magic_bin `, right? I tried that but it didn't help. In loader.py I made this change: `except OSError as osx: print("loader.py - load_lib: " + str(osx))` so that it will show the error. And it printed `Could not find module 'C:\Users\\PycharmProjects\\azuresite\libmagic.dll' (or one of its dependencies)` and also the other dlls: `magic1.dll, cygmagic-1.dll, libmagic-1.dll, msys-magic-1.dll` were not found. Maybe some issue with virtual environment? – Matt Nov 10 '22 at 14:36
15

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

dusual
  • 2,097
  • 3
  • 19
  • 26
  • i have downloaded the setup file do I install them? or to keep them directly under C:\Windows\System32 – Akhter Al Amin Aug 22 '13 at 16:42
  • 2
    How did you manage to make it work ? I have the same issue, and following the installation/dependencies instruction does not help :( – Gnujeremie Nov 27 '14 at 11:08
  • Thanks! FYI - For OSX I use Homebrew so I had to run `brew install libmagic` as the `pip3 install python-magic` did not work from terminal. Python-Magic installs but python3 cannot locate it. – JayRizzo Aug 02 '19 at 18:36
8

As per the documentation (https://pypi.org/project/python-magic/) you have to install libmagic.

For Windows OS, you can do it by run the bellow command:

pip install python-magic-bin

And for Ubuntu based OS, you have to it by the bellow command:

sudo apt-get install libmagic1

From, your exception it is easy to say that, you are using Windows OS, so you have install it by the first command.

Samiddha
  • 103
  • 1
  • 7
5

As per the documentation the current version of python-magic is 0.4.15

  • You can install the latest released version of python-magic through:

    pip install python-magic
    
  • This will install python-magic-0.4.15 as follows:

    C:\Users\username>pip install python-magic
    Collecting python-magic
      Using cached https://files.pythonhosted.org/packages/42/a1/76d30c79992e3750dac6790ce16f056f870d368ba142f83f75f694d93001/python_magic-0.4.15-py2.py3-none-any.whl
    Installing collected packages: python-magic
    Successfully installed python-magic-0.4.15
    
  • In the (Windows) documentation it is mentioned:

You'll need DLLs for libmagic. @julian-r has uploaded a version of this project that includes binaries to pypi: https://pypi.python.org/pypi/python-magic-bin/0.4.14

  • You can install the python-magic-bin 0.4.14 through:

    pip install python-magic-bin==0.4.14
    
  • This will install python-magic-bin 0.4.14 as follows:

    C:\Users\username>pip install python-magic-bin==0.4.14
    Collecting python-magic-bin==0.4.14
      Downloading https://files.pythonhosted.org/packages/5a/5d/10b9ac745d9fd2f7151a2ab901e6bb6983dbd70e87c71111f54859d1ca2e
    /python_magic_bin-0.4.14-py2.py3-none-win32.whl (397kB)
        100% |¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦| 399kB 473kB/s
    Installing collected packages: python-magic-bin
    Successfully installed python-magic-bin-0.4.14
    
  • Sample Code:

    import magic
    print(magic.from_file("C:/Users/username/Desktop/StackOverflow/Google_Gmail.png"))
    
  • Console Output:

    PNG image data, 1366 x 768, 8-bit/color RGB, non-interlaced
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
1

Install cygwin. Then move 'C:\cygwin64\bin\cygmagic-1.dll' to 'C:\Windows\System32\magic1.dll'.

pigsign
  • 19
  • 1
1
  1. Download cygwin. The version of cygwin must as same as python.(exp: Although your OS is Win7 x64, you still should download 32 bit cygwin because your python is 32 bit). link: https://www.cygwin.com/
  2. Install cygwin.
  3. Enter the path of cygwin and copy cygwin1.dll/cyggcc_s-1.dll/cygmagic-1.dll/cygz.dll to C:\Windows\SysWOW64.
pijing
  • 81
  • 1
  • 6
0

I was going carzy with this issue. But finally got a solution to it

Steps

  1. Uninstall all magic libraries from you system (as you may have tried installing tons of garbage by this time)
  2. Make sure you are running a python 64 bit version
  3. Install pip install python-magic-win64==0.4.13
  4. very important step, change your import statement to from winmagic import magic

Enjoy !!!

0

If your target platform supports the file command with certain extensions (available on macOS, *BSD and most Linux variants), you can use the following wrapper that does not require libmagic nor other dependencies:

import contextlib
import subprocess

def file_proc():
    args = [
        '/usr/bin/file',
        '--brief',
        '-E',
        '--no-dereference',
        '--no-buffer',
        '--preserve-date',
        '-00',
        '--mime-type',
        '--files-from', '-'
    ]

    with contextlib.ExitStack() as stack:
        proc = subprocess.Popen(args, bufsize=0, stdin=subprocess.PIPE, stdout=subprocess.PIPE, preexec_fn=None, text=True)
        stack.callback(proc.kill)

        while True:
            file_path = yield
            proc.stdin.write(file_path + '\n')
            proc.stdin.flush()

            file_type = ''
            while (c := proc.stdout.read(1)) != '\0':
                file_type += c

            yield file_type.splitlines()[0]


@contextlib.contextmanager
def file_typer():
    proc = file_proc()
    proc.send(None)

    def getter(path):
        posix_type = proc.send(path.as_posix())
        proc.send(None)
        return posix_type

    with contextlib.closing(proc):
        yield getter
        

with file_typer() as typer:
  typer('/usr/bin/python')

See the GitHub enter link description heregist for the most up-to-date version of this script.

Kentzo
  • 3,881
  • 29
  • 54