简体   繁体   中英

Pushbullet on python throws import error due to python-magic unable to find libmagic?

I'm trying to use PushBullet.py which uses python-magic which in turn uses libmagic .

I have followed the dependencies instructions and installed Cygwin and copied the three files to Windows\\system32 directory but still getting the following error:

Traceback (most recent call last):
  File "C:\New Python ActiveX Scripting Engine.py", line 1, in <module>
    from pushbullet import PushBullet
  File "C:\Python27\lib\site-packages\pushbullet\__init__.py", line 2, in <module>
    from .pushbullet import PushBullet
  File "C:\Python27\lib\site-packages\pushbullet\pushbullet.py", line 4, in <module>
    import magic
  File "C:\Python27\lib\site-packages\magic.py", line 161, in <module>
    raise ImportError('failed to find libmagic.  Check your installation')
ImportError: failed to find libmagic.  Check your installation

EDIT

If I put cygmagic-1.dll OR cygz.dll into C:\\Python27\\ and rename it to magic.dll I get the following error:

Traceback (most recent call last):
  File "C:\New Python ActiveX Scripting Engine.py", line 1, in <module>
    from pushbullet import PushBullet
  File "C:\Python27\lib\site-packages\pushbullet\__init__.py", line 2, in <module>
    from .pushbullet import PushBullet
  File "C:\Python27\lib\site-packages\pushbullet\pushbullet.py", line 4, in <module>
    import magic
  File "C:\Python27\lib\site-packages\magic.py", line 143, in <module>
    libmagic = ctypes.CDLL(dll)
  File "C:\Python27\lib\ctypes\__init__.py", line 365, in __init__
    self._handle = _dlopen(self._name, mode)
WindowsError: [Error 126] The specified module could not be found

If I put cygwin1.dll into C:\\Python27\\ and rename it to magic.dll I get the following error:

Traceback (most recent call last):
  File "C:\New Python ActiveX Scripting Engine.py", line 1, in <module>
    from pushbullet import PushBullet
  File "C:\Python27\lib\site-packages\pushbullet\__init__.py", line 2, in <module>
    from .pushbullet import PushBullet
  File "C:\Python27\lib\site-packages\pushbullet\pushbullet.py", line 4, in <module>
    import magic
  File "C:\Python27\lib\site-packages\magic.py", line 185, in <module>
    magic_open = libmagic.magic_open
  File "C:\Python27\lib\ctypes\__init__.py", line 378, in __getattr__
    func = self.__getitem__(name)
  File "C:\Python27\lib\ctypes\__init__.py", line 383, in __getitem__
    func = self._FuncPtr((name_or_ordinal, self))
AttributeError: function 'magic_open' not found

I'm doing this on Windows 7 64bit running Python 32bit 2.7.8 (fresh install today to try and resolve this problem).

Does anyone know how to resolve the problem?

EDIT: Tried on a further 5 different windows computers and all 5 have the same problem(s).

I had the same problem with python-magic and solved it by fixing the following line in the magic.py file ( C:\\Python27\\lib\\site-packages\\magic.py in my PC):

dll = ctypes.util.find_library('magic') or ctypes.util.find_library('magic1')

Since I've installed libmagic from Cygwin, the DLL was named cygmagic-1.dll . So I simply added another choice in the previous line:

dll = ctypes.util.find_library('magic') or ctypes.util.find_library('magic1') \
    or ctypes.util.find_library('cygmagic-1')

Don't forget to add cygwin\\bin to your PATH .

EDIT: It seems that this issue has been addressed on the GitHub repository .

EDIT2: These are the steps I followed to make PushBullet.py work on my machine:

  1. Install PushBullet.py via pip ( pip install PushBullet.py );
  2. Install libmagic from Cygwin ;
  3. Add C:\\cygwin\\bin\\ to the PATH environment variable;
  4. Change the magic.py file as I've explained above. (In my case the error was on line 139)

From the Windows paths in your traceback listings I guess that you are trying to execute PushBullet script from Windows version of Python. Why did you install Cygwin if you are not using it? Python for Windows really won't use Cygwin's DLLs.

You have to execute PushBullet.py from Cygwin using a Python for Cygwin, not from Windows Console using a Python for Windows. That means execute for example bash (something like C:\\cygwin64\\bin\\bash.exe --login -i ) and from bash execute PushBullet script: python PushBullet.py .

It expects, that you have Python and python-magic for Cygwin ( for Cygwin! not Windows version ) already installed.

Copying DLLs to your system directories is nonsense, don't do it. Renaming them is uber -nonsense. How did you expect it could work? Each library is specific, you can't just rename it to change how it works internally.

I'm already using PushBullet for some time, so I was interested in this particular Python script. I'm also using the Cygwin for years. So I have installed PushBullet library:

pip install pushbullet.py

Then I have created very simple script test.py :

#!/usr/bin/python
from pushbullet import PushBullet

pb = PushBullet('my_access_token')
success, push = pb.push_note("Testing title", "Hello world!")

When I executed it using ./test.py I got an error message, that I'm missing magic library, so I installed python-magic library using Cygwin's setup utility.

I executed it again and voila - I have "Hello world!" message on my phone. Done.

Just a note again: I have executed it from the Cygwin's shell (zsh, but you'll have bash I guess), not from Windows Console. I also didn't use Python for Windows but Cygwin's version. Don't mix Windows and Cygwin executables!

So dumb-proof steps would be:

  1. download and execute setup.exe from Cygwin.com
  2. install python and python-magic
  3. execute Cygwin's shell (you can execute "Cygwin Terminal" from your Start menu or just execute C:\\cygwin64\\bin\\bash.exe --login -i for example from "Run" dialog or Windows Console)
  4. install pip (see for example Pip install not functioning on windows 7 Cygwin install )
  5. install PushBullet library: pip install pushbullet.py
  6. prepare your testing script
  7. execute it: python testing_script.py or just testing_script.py if it contains the shebang line and is executable
  8. it should work, if not, post your errors, please

Thank you for a tip to useful library :)

You cannot mix Cygwin and MSVCRT binaries. Therefore, in order to use python-magic with the Windows Python, you must get a Windows-compiled libmagic and fix magic.py to find the libmagic DLL.

Otherwise, if you want to use Cygwin's python-magic as-is, you need to use Cygwin's python packages.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM