Python Windows 上的 Pyusb - 没有可用的后端

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13773132/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 09:34:17  来源:igfitidea点击:

Pyusb on windows - no backend available

pythonwindows-7libusbpyusb

提问by user634340

I'm trying to have my python application interface with an NFC device via USB. The best option seems to be pyusb, but I can't get it to connect to the libusb backend. I keep getting

我正在尝试通过 USB 将我的 python 应用程序接口与 NFC 设备连接起来。最好的选择似乎是 pyusb,但我无法让它连接到 libusb 后端。我不断得到

ValueError: No backend available

ValueError:没有可用的后端

I've looked at the stack trace, and found that usb/backend/libusb10.py(which is part of pyusb) is trying to load libusb-1.0.dllto use as the backend, but it can't find it. It's not that its not in my path, its not on my computer at all!

我查看了堆栈跟踪,发现usb/backend/libusb10.py(它是 pyusb 的一部分)正在尝试加载libusb-1.0.dll以用作后端,但找不到它。并不是它不在我的路径中,它根本不在我的电脑上!

I have installed libusb-win32 (http://libusb.org/wiki/libusb-win32), but the resulting directory only seems to include libusb0.dll. Where is libusb-1.0.dll???!

我已经安装了 libusb-win32 ( http://libusb.org/wiki/libusb-win32),但结果目录似乎只包含 libusb0.dll。libusb-1.0.dll 在哪里???

I would love to know either where to get that dll, or even a different suggestion to get pyusb to work on Windows 7.

我很想知道从哪里获得那个 dll,或者甚至是让 pyusb 在 Windows 7 上工作的不同建议。

回答by Dave L.

I had a similar issue recently trying to talk to a USB device I am developing. I scoured the web looking for libusb-1.0.dll's and had no luck. I found source code, but nothing built and ready to install. I ended up installing the libusb-win32 binaries, which is the libusb0.dll.

我最近在尝试与我正在开发的 USB 设备交谈时遇到了类似的问题。我在网上搜索 libusb-1.0.dll 并没有走运。我找到了源代码,但没有构建和准备安装。我最终安装了 libusb-win32 二进制文件,即 libusb0.dll。

PyUSB will search for libusb-1.0, libusb0, and openUSB backends.

PyUSB 将搜索 libusb-1.0、libusb0 和 openUSB 后端。

libusb0.dll was already on my system, but something was still not set up right, do PyUSB was not working.

libusb0.dll 已经在我的系统上,但仍然没有正确设置,PyUSB 是否不起作用。

I followed the directions hereto download and install the driver using the GUI tools provided to install the filter driver, and the INF wizard. Note, it didn't work until I ran the INF wizard.

我按照此处的说明使用提供的 GUI 工具下载和安装驱动程序,以安装过滤器驱动程序和 INF 向导。请注意,直到我运行 INF 向导它才起作用。

I'm pretty new to programming and I've found the lack of clear documentation/examples to string this all together rather frustrating.

我对编程还很陌生,我发现缺乏清晰的文档/示例来将这一切串联在一起,这令人沮丧。

回答by George G

I am using Python 2.6.5, libusb-win32-device.bin-0.1.12.1and pyusb-1.0.0-a0on a windows XP system and kept receiving ValueError: No backend available.

我正在使用 Python 2.6.5,libusb-win32-device.bin-0.1.12.1pyusb-1.0.0-a0在 Windows XP 系统上不断接收ValueError: No backend available.

Since there wasn't any real help on the web for this problem I spent a lot of time finding that ctypes util.pyuses the Pathvariable to find the library file. My pathdid not include windows\system32and PYUSBdidn't find the library. I updated the pathvariable and now the USB is working.

由于网络上没有针对此问题的任何真正帮助,因此我花了很多时间寻找ctypes util.py使用该Path变量来查找库文件的方法。我path不包括windows\system32PYUSB没找到库。我更新了path变量,现在 USB 正在工作。

回答by tbenisa

To connect to your NFC device via USB using PYUSB, you will need to install the backend for that device. I do not think there is any backend for any device other than a libusb device.

要使用 PYUSB 通过 USB 连接到您的 NFC 设备,您需要为该设备安装后端。我认为除了 libusb 设备之外,任何设备都没有任何后端。

To build a backend. You will need to know the driver (.sys file) for your device, so you could write a wrapper DLL to expose functionalities in the device. Your DLL would have to have a method to find device based on PID & VID, another method to open device and another method to send data and so on...

建立一个后端。您将需要知道您的设备的驱动程序(.sys 文件),以便您可以编写一个包装 DLL 来公开设备中的功能。您的 DLL 必须有一种基于 PID 和 VID 查找设备的方法、另一种打开设备的方法和另一种发送数据的方法等等......

回答by Liviu

"There are two versions of the libusb API: the current libusb-1.0API, and its legacy predecessor libusb-0.1." (http://www.libusb.org/) "libusb-win32is a port of the USB library ?libusb-0.1to the Microsoft Windows operating systems". "Download the latest release tarball" from the same page(1.0.9is the current version) to have libusb-1.0equivalent, you'll find a folder Win32, where you'll find your libusb-1.0.dllto play with! You can even build it: http://www.libusb.org/wiki/windows_backend.

“libusb API 有两个版本:当前的libusb-1.0API 和它的旧版 API libusb-0.1。” ( http://www.libusb.org/) “libusb-win32是 USB 库的端口?libusb-0.1到 Microsoft Windows 操作系统”。从同一页面1.0.9是当前版本)“下载最新版本的 tarball”以获取libusb-1.0等效文件,您会找到一个文件夹Win32,您可以在其中找到libusb-1.0.dll要玩的!您甚至可以构建它:http: //www.libusb.org/wiki/windows_backend

EDITYou have to build it (download from/ http://sourceforge.net/projects/libusb/files/libusb-1.0/) since the tarball is from 2012, while the latest sources are from 2014-06-15.

编辑您必须构建它(从/ http://sourceforge.net/projects/libusb/files/libusb-1.0/下载),因为 tarball 来自 2012,而最新的来源来自 2014-06-15。

回答by fixxxer

Just in case:

以防万一:

I haven't tried this on Windows but I had to set DYLD_LIBRARY_PATH path to circumvent this error on the Macintosh.

我没有在 Windows 上尝试过这个,但我必须设置 DYLD_LIBRARY_PATH 路径来规避 Macintosh 上的这个错误。

export DYLD_LIBRARY_PATH=/opt/local/lib

导出 DYLD_LIBRARY_PATH=/opt/local/lib

Discussion on whether or not to set this variable is here.

关于是否设置这个变量的讨论在这里

回答by beebek

Download and install libusb-win32-devel-filter-1.2.6.0.exe. It should work.

下载并安装libusb-win32-devel-filter-1.2.6.0.exe。它应该工作。

回答by user9393931

I had the same problem with Windows 10, both Python 2.7.16 and Python 3.7.2. I installed libusb (through python -m pip install libusb) but the error message remained. Also, the advice above about installing libusb-win32 did not work for me; neither of the 2 links (original post and @beebek's answer) existed.

我在 Windows 10 上遇到了同样的问题,包括 Python 2.7.16 和 Python 3.7.2。我安装了 libusb(通过python -m pip install libusb),但错误消息仍然存在。另外,上面关于安装 libusb-win32 的建议对我不起作用;两个链接(原始帖子和@beebek 的答案)都不存在。

What didwork, however, is the comment by @user1495323 : I copied libusb-1.0.dllfrom C:\Users\username\AppData\Roaming\Python\Python27\site-packages\libusb\_platform\_windows\x64\to C:\Windows\System32\

什么工作,但是,是@ user1495323评论:我复制libusb-1.0.dllC:\Users\username\AppData\Roaming\Python\Python27\site-packages\libusb\_platform\_windows\x64\C:\Windows\System32\

回答by Sergey Lourie

There's a simpler solution.

有一个更简单的解决方案。

Download and unpack to C:\PATH the libusb-1.0.20 from download link

下载链接下载libusb-1.0.20 并解压到 C:\PATH

Then try this line:

然后试试这一行:

backend = usb.backend.libusb1.get_backend(find_library=lambda x: "C:\PATH\libusb-1.0.20\MS32\dll\libusb-1.0.dll")

dev = usb.core.find(backend=backend, find_all=True)

backend = usb.backend.libusb1.get_backend(find_library=lambda x: "C:\PATH\libusb-1.0.20\MS32\dll\libusb-1.0.dll")

dev = usb.core.find(后端=后端,find_all=True)

Depending on your system, try either MS64 or MS32 version of the .dll

根据您的系统,尝试使用 MS64 或 MS32 版本的 .dll

Update of 17/01/2020, after a request to share more code:

2020 年 1 月 17 日更新,请求共享更多代码后:

import usb.core
import usb.util

from infi.devicemanager import DeviceManager
dm = DeviceManager()
devices = dm.all_devices
for i in devices:
    try:
        print ('{} : address: {}, bus: {}, location: {}'.format(i.friendly_name, i.address, i.bus_number, i.location))
    except Exception:
        pass


import usb.backend.libusb1

backend = usb.backend.libusb1.get_backend(find_library=lambda x: "C:\libusb-1.0.20\MS32\dll\libusb-1.0.dll")
dev = usb.core.find(backend=backend, find_all=True)

def EnumerateUSB():    #I use a simple function that scans all known USB connections and saves their info in the file
    with open("EnumerateUSBLog.txt", "w") as wf:
        counter = 0
        for d in dev:
            try:
                wf.write("USB Device number " + str(counter) + ":" + "\n")
                wf.write(d._get_full_descriptor_str() + "\n")
                wf.write(d.get_active_configuration() + "\n")
                wf.write("\n")
                counter += 1
            except NotImplementedError:
                wf.write("Device number " + str(counter) + "is busy." + "\n")
                wf.write("\n")
                counter += 1
            except usb.core.USBError:
                wf.write("Device number " + str(counter) + " is either disconnected or not found." + "\n")
                wf.write("\n")
                counter += 1
        wf.close()