简体   繁体   English

由于python-magic无法找到libmagic,python上的pushbullet会引发导入错误?

[英]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 . 我正在尝试使用PushBullet.py ,它使用python-magic ,后者又使用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: 我已遵循依赖项说明并安装了Cygwin并将这三个文件复制到Windows\\system32目录但仍然收到以下错误:

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: 如果我将cygmagic-1.dll OR cygz.dll放入C:\\Python27\\并将其重命名为magic.dll我会收到以下错误:

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: 如果我将cygwin1.dll放入C:\\Python27\\并将其重命名为magic.dll我会收到以下错误:

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). 我在运行Python 32bit 2.7.8的Windows 7 64bit上执行此操作(今天全新安装以尝试解决此问题)。

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). 编辑:尝试了另外5台不同的Windows计算机,所有5台都有相同的问题。

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): 我遇到了与python-magic相同的问题,并通过修复magic.py文件中的以下行来解决它(我的PC中的C:\\Python27\\lib\\site-packages\\magic.py ):

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 . 由于我从Cygwin安装了libmagic ,因此DLL被命名为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 . 不要忘记将cygwin\\bin添加到PATH

EDIT: It seems that this issue has been addressed on the GitHub repository . 编辑:似乎这个问题已在GitHub存储库中解决

EDIT2: These are the steps I followed to make PushBullet.py work on my machine: EDIT2:这是我在我的机器上运行PushBullet.py遵循的步骤:

  1. Install PushBullet.py via pip ( pip install PushBullet.py ); 安装PushBullet.py通过pippip install PushBullet.py );
  2. Install libmagic from Cygwin ; Cygwin安装libmagic ;
  3. Add C:\\cygwin\\bin\\ to the PATH environment variable; C:\\cygwin\\bin\\PATH环境变量中;
  4. Change the magic.py file as I've explained above. 像我上面解释的那样更改magic.py文件。 (In my case the error was on line 139) (就我而言,错误发生在第139行)

From the Windows paths in your traceback listings I guess that you are trying to execute PushBullet script from Windows version of Python. 从traceback列表中的Windows路径,我猜你正在尝试从Windows版本的Python执行PushBullet脚本。 Why did you install Cygwin if you are not using it? 如果不使用Cygwin,为什么要安装? Python for Windows really won't use Cygwin's DLLs. Python for Windows真的不会使用Cygwin的DLL。

You have to execute PushBullet.py from Cygwin using a Python for Cygwin, not from Windows Console using a Python for Windows. 您必须使用Python for Cygwin从Cygwin执行PushBullet.py ,而不是使用Python for Windows从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 . 这意味着执行例如bash (类似于C:\\cygwin64\\bin\\bash.exe --login -i )和bash执行PushBullet脚本: python PushBullet.py

It expects, that you have Python and python-magic for Cygwin ( for Cygwin! not Windows version ) already installed. 它预计,你已经安装了Cygwin( 适用于Cygwin!而不是Windows版本 )的Pythonpython-magic

Copying DLLs to your system directories is nonsense, don't do it. 将DLL复制到系统目录是无稽之谈,不要这样做。 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. 我已经使用了PushBullet一段时间了,所以我对这个特殊的Python脚本很感兴趣。 I'm also using the Cygwin for years. 我也在使用Cygwin多年。 So I have installed PushBullet library: 所以我安装了PushBullet库:

pip install pushbullet.py

Then I have created very simple script test.py : 然后我创建了非常简单的脚本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. 当我使用./test.py执行它时,我收到一条错误消息,我错过了magic库,因此我使用Cygwin的安装实用程序安装了python-magic库。

I executed it again and voila - I have "Hello world!" 我再次执行它瞧 - 我有“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. 再说一遍:我已经从Cygwin的shell中执行了它(zsh,但我猜你会有bash ),而不是来自Windows控制台。 I also didn't use Python for Windows but Cygwin's version. 我也没有使用Python for Windows而是使用Cygwin的版本。 Don't mix Windows and Cygwin executables! 不要混用Windows和Cygwin可执行文件!

So dumb-proof steps would be: 所以愚蠢的步骤将是:

  1. download and execute setup.exe from Cygwin.com Cygwin.com下载并执行setup.exe
  2. install python and python-magic 安装pythonpython-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) 执行Cygwin的shell(您可以从“开始”菜单执行“Cygwin终端”或只执行C:\\cygwin64\\bin\\bash.exe --login -i ,例如从“运行”对话框或Windows控制台执行)
  4. install pip (see for example Pip install not functioning on windows 7 Cygwin install ) install pip (参见例如Pip install在Windows 7 Cygwin安装上不起作用
  5. install PushBullet library: pip install pushbullet.py 安装PushBullet库: 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 执行它: python testing_script.py或者只是testing_script.py如果它包含shebang行并且是可执行的
  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. 你不能混合使用Cygwin和MSVCRT二进制文件。 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. 因此,为了在Windows Python中使用python-magic,你必须得到一个Windows编译的libmagic并修复magic.py来找到libmagic DLL。

Otherwise, if you want to use Cygwin's python-magic as-is, you need to use Cygwin's python packages. 否则,如果你想按原样使用Cygwin的python-magic,你需要使用Cygwin的python包。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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