Skip to content
This repository has been archived by the owner on Jul 30, 2023. It is now read-only.

DLL folder not ensured with pathtub.ensure() #2

Closed
fohrloop opened this issue Apr 12, 2020 · 2 comments
Closed

DLL folder not ensured with pathtub.ensure() #2

fohrloop opened this issue Apr 12, 2020 · 2 comments
Assignees
Labels
bug Something isn't working

Comments

@fohrloop
Copy link
Owner

Steps to reproduce

  • pathtub 1.1.1
  • pyusb
  • Python 3.8.2 32.bit
from pathtub import ensure
dll_folder = r'C:\path to\libusb-1.0.21\MS64\dll'
import usb.backend.libusb1

ensure(dll_folder)
usb.backend.libusb1._load_library()

Expected behaviour

  • The DLL is found after adding to to Windows (Process) PATH
  • usb.backend.libusb1._load_library() returns without Exceptions

Actual behaviour

  • The DLL is not found by the pyusb library DLL search
  • usb.backend.libusb1._load_library() throws LibraryNotLoadedException
@fohrloop fohrloop self-assigned this Apr 12, 2020
@fohrloop fohrloop added the bug Something isn't working label Apr 12, 2020
@fohrloop
Copy link
Owner Author

Seems that on Python 3.8+ searching for DLLs is done a bit differently. Prior Python 3.8 you could just add DLL folder to Process PATH and DLL's would be loaded.

  • PATH and current working directory are not used in Python 3.8+.
  • New function called os.add_dll_directory was added in Python 3.8. It adds a path to DLL search path. It is used when resolving dependencies for imported extension modules and by ctypes.

Python 3.8 and usb.backend.libusb1._load_library()

  • Tried the following and it still throws errors
import os
import usb.backend.libusb1

dll_folder = r'C:\path to\libusb-1.0.21\MS64\dll'

h = os.add_dll_directory(dll_folder)
# throws error
usb.backend.libusb1._load_library()

from pathtub import ensure
ensure(dll_folder)

# throws error
usb.backend.libusb1._load_library()

It means, even with the DLL folder added with os.add_dll_directory and that same folder in the PATH the pyusb libusb1._load_library() does not seem to find the dll.

@fohrloop
Copy link
Owner Author

It seems that the problem was that I tried to add 64-bit libusb-1.0.dll using 32-bit Python 3.8.

The usb.backend.libusb1._load_library() started working by just adding the 32-bit libusb-1.0.dll to the PATH with pathtub.ensure. However, in Python 3.8, one should

  • Add DLLs folders with os.add_dll_directory, as this is the official "new way".
  • Add the folder also to the Process PATH, since some python packages (like this pyusb, for example) might have their own ways of finding the dlls, which rely on the PATH.

New function: ensure_dll()

I added to pathtub v.1.1.2: a new function, ensure_dll that takes care of this all. See also: https://github.com/np-8/pathtub/blob/master/docs/dll_paths.md

The correct way to include DLL folder is now

from pathtub import ensure_dll
ensure_dll(r'C:\path to my favourite\dlls')

there is also a function forget_dll to forget added dll folder (even though they are just added for the process, sometimes this might be needed)

Why not just use ensure() for DLLs?

The reason for not using pathtub.ensure for ensuring that python (and other DLLs) find DLLs is that the ensure is used to ensure that a folder is found in PATH, and it is completely different task than ensuring DLLs to be found. Also, the ensure has for example parameter permanent, which at least for now would be difficult to implement in every DLL case.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant