0

I have troubleshooted for days with no luck, I hope someone here can help please?

There's lots of discussions about this online all with roughly the same answer so I won't go to much into depth. Here are my system specs for all that I know needs installing.

#478

  • Windows 10
  • Python Version: 3.10.11
  • Pip 23.1.2
  • pyusb-1.2.1
  • libusb1-3.0.0

I have downloaded and added the libusb-1.0.dll file to C:\Windows\System32\libusb-1.0.dll but I still receive the error that No Backend is available. I have checked and the file is definitely there, I copied it over myself of course.

Here is the simplified code to test for the backend but I will add the full code of my app lower down.

import usb.core
import usb.backend.libusb1

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

The result in the terminal is:

Traceback (most recent call last): File "f:\Software Development\Controller Detection Programme\pythonLocator.py", line 6, in dev = usb.core.find(backend=backend, find_all=True) File "C:\Users\dmitr\AppData\Local\Programs\Python\Python310\lib\site-packages\usb\core.py", line 1309, in find raise NoBackendError('No backend available') usb.core.NoBackendError: No backend available

I am pulling my hair out trying to understand why this is still not working, I have directly specified the PATH where the file is and yet it still doesn't find it or am I missing something? I have also added the 32 bit library in SysWOW64 just in case but from what I understood, I'm pretty sure the interpreter isn't searching there.

I am still new to this so apologies if I don't understand the obvious but does anyone know what could be causing this? Any help is much appreciated, cheers.

Here is the full code as it stands but it's practically the same:

import sys
import usb.core
import usb.backend.libusb1

import tkinter as tk
import tkinter.messagebox as tkmb

import time

\#be = libusb1.get_backend()
\#devbe = usb.core.find(backend=be)

window = tk.Tk()

window.title("Controller Detection")

window.geometry("700x500")

isConnected = False
starttime = time.time()

label = tk.Label(window, text="Click the Button to start the controller check",
font=('Calibri 15 bold'))
label.pack(pady=20)

def checkForController():
backend = usb.backend.libusb1.get_backend(find_library=lambda x:"C:\\Windows\\System32\\libusb-1.0.dll")

\#find all USB devices
dev = usb.core.find(find_all=True, backend=backend)

while True:
time.sleep(4 - ((time.time() - starttime) % 2))

    # loop through devices, checking for idProduct 3302, i.e. the controller
    for cfg in dev:
        if cfg.idProduct == 3302:
            print("Device connected")
            label["text"] = "Device Connected"
            isConnected = True
            break
    
    if isConnected == False:
        print("Device is Not Connected")
        label["text"] = "Device Not Connected"
        isConnected == False

btn1 = tk.Button(window, text="Button1", command=checkForController)
btn1.pack(pady=20)

window.mainloop()

**Additionally I have checked the .spec file that is created and again if I understood correctly, it should be finding the libusb-1.0.dd file.

I have also tried adding the .dll file to pyinstallers build directory using this line:

pyinstaller --add-binary "C:\Windows\System32\libusb-1.0.dll;libusb-1.0" --onefile isControllerConnected.py

Executable .spec file

Dmitry
  • 1
  • 1

1 Answers1

0

Okay I found a solution that worked for me, hopefully it will do the same for someone else.

The solution was chopped together from these answers:

pyusb fails to find libusb when using pyinstaller

Pyusb on windows - no backend available

  1. First package your [YOUR_FILE_NAME].py script using pyinstaller, this was my cmd line, simplified for now:

pyinstaller --onefile isControllerConnected.py

If you try to the run executable, you are likely to face the No Backend Available error in the terminal

  1. Once the build is complete, you should see a .spec file created in the same directory with your file's name ([YOUR_FILE_NAME].spec).

  2. Open the .spec file with whatever code editor you want.

  3. Edit the "datas" line to include [('C:\Windows\System32\libusb0.dll', '.'),], this is assuming you have the .dll file in C:\Windows\System32\libusb0.dll, you can follow the Pyusb on windows - no backend available link above to resolve this if needs be but I found installing pyusb and libusb did it for me on Windows 10.

a = Analysis(
    ['isControllerConnected.py'],
    pathex=[],
    binaries=[('C:\\Windows\\System32\\libusb-1.0.dll', 'libusb-1.0')],
    datas=[('C:\\Windows\\System32\\libusb0.dll', '.'),],
    hiddenimports=[],
    hookspath=[],
    hooksconfig={},
    runtime_hooks=[],
    excludes=[],
    win_no_prefer_redirects=False,
    win_private_assemblies=False,
    cipher=block_cipher,
    noarchive=False,
)
pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher)

exe = EXE(
    pyz,
    a.scripts,
    a.binaries,
    a.zipfiles,
    a.datas,
  1. Run pyinstaller [YOUR_FILE_NAME].spec in your cmd line, this builds the executable from the edited .spec file.

You can run the same line every time even after you edit the script, this just tells pyinstaller what the build specifications are.

Hope this makes sense.

.spec file of the packaged script

Dmitry
  • 1
  • 1