1

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

David Ferenczy Rogožan
  • 23,966
  • 9
  • 79
  • 68
Ryflex
  • 5,559
  • 25
  • 79
  • 148
  • https://github.com/ahupp/python-magic#dependencies – John Zwinck Aug 15 '14 at 14:23
  • @JohnZwinck I've already covered that in my second line of my post, it's the same error before and after... – Ryflex Aug 15 '14 at 14:27
  • Maybe you can use a debugger or so to isolate where exactly it is looking for the files it needs. – John Zwinck Aug 15 '14 at 14:28
  • @JohnZwinck could you possibly explain how best to do that; What debugger? – Ryflex Aug 15 '14 at 14:30
  • On Linux I'd normally use `strace` and grep for `libmagic` to see what files it's trying to open. Maybe your Cygwin has that; if not I'm sure there are other tools on Windows, like perhaps the SysInternals File Monitor thingy which shows you which process is opening (or failing to open) which files. – John Zwinck Aug 15 '14 at 14:40
  • Cygwin has `strace`, but this kind of debugging is good for skilled users, not for ones who renames DLLs in a hope of fixing problem :) There is really no need to do that, I was surprised how it's easy to actually make it work. Just don't mix Windows and Cygwin executables, don't rename anything and it has to work. – David Ferenczy Rogožan Aug 26 '14 at 20:32

3 Answers3

2

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)
David Ferenczy Rogožan
  • 23,966
  • 9
  • 79
  • 68
fvizeus
  • 88
  • 6
  • Hi fvizeus, could you explain at all the exact steps you did? – Ryflex Sep 21 '14 at 15:32
  • Hello, @Ryflex! I have edited the answer to give a step by step explanation of what I did to get it working on my PC. – fvizeus Sep 22 '14 at 17:18
  • Hey @fvizeus I've followed the way that worked for you but I think I'm doing something wrong during the install of libmagic from cygwin :S – Ryflex Sep 23 '14 at 07:29
  • For me the installation worked just fine. I've downloaded the 32-bit installer from [cygwin.com](https://www.cygwin.com/). Then I proceeded with the recommended settings and chose a mirror. After that I was asked to select the packages. In this part I think I had to manually choose libmagic for installation. Just search for "libmagic" in the search box and select the package located on "Devel". By now it should download the packages and proceed to the installation. – fvizeus Sep 30 '14 at 13:48
1

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

Community
  • 1
  • 1
David Ferenczy Rogožan
  • 23,966
  • 9
  • 79
  • 68
  • I a lot of scripts in the Windows Python, how can I get PushBullet working on Windows versions... or how can I make my scripts work with the Cygwin version? I am using loads of librarys like skype4py, beautifulsoup, mechanize, pandas... etc – Ryflex Aug 27 '14 at 18:11
  • Sorry, I don't use Widnows versions, so I don't know, how to make it work with them. But I have described how to make it work with Cygwin very in depth, step by step so I don't understand what are you asking. – David Ferenczy Rogožan Aug 27 '14 at 21:00
  • Just follow my 8 "dumb-proof" steps and it has to work. It was really very easy to make it work. – David Ferenczy Rogožan Aug 27 '14 at 21:02
  • BTW you can uninstall Windows version of Python completely and use Cygwin's one only. All your existing Python scripts will work with it too. It's easier to install all dependencies this way. – David Ferenczy Rogožan Aug 27 '14 at 21:04
  • @Ryflex So did you solve it successfully? I'm just asking if my answer had helped you, because you have offered a bounty for solving your problem. – David Ferenczy Rogožan Sep 01 '14 at 15:30
0

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.

Yaakov
  • 1,705
  • 10
  • 10