Skip to content

Support more platforms in ctypes.find_library #142020

Description

@Samueru-sama

Bug report

Bug description:

I make appimages that bundle glibc and as result are able to work on any linux system (even in musl libc systems).

I had this issue which wasn't fully fixed until I manage to find out that the problem was cpython running /sbin/ldconfig -p which caused everything to fail.

Interestingly enough the output of ldconfig is not needed here, since after doing some patching to bundle ldconfig the issue was fixed, that ldconfig is just outputting the libraries of the alpine system and none of the bundled libraries yet the application finds and uses the bundled libraries without issue.

so this should test if ldconfig -p is supported before attempting to use it.

CPython versions tested on:

3.13

Operating systems tested on:

Linux

Linked PRs

Activity

  1. picnixz commented on Nov 27, 2025

    @picnixz
    Member

    AFAICT, we do check this. Where is the ldconfig call being used? The only call I could find was in ctypes but we have a try-catch already.

  2. added
    pendingThe issue will be closed if no feedback is provided
    on Nov 27, 2025
  3. Samueru-sama commented on Nov 27, 2025

    @Samueru-sama
    Author

    AFAICT, we do check this. Where is the ldconfig call being used? The only call I could find was in ctypes but we have a try-catch already.

    The call is coming from pyudev:

    Traceback (most recent call last):
      File "/tmp/samuel/Volatile/AppDir/bin/oversteer", line 53, in <module>
        sys.exit(app.run(sys.argv))
                 ~~~~~~~^^^^^^^^^^
      File "/tmp/samuel/Volatile/AppDir/lib/python3.13/site-packages/oversteer/application.py", line 63, in run
        self.device_manager.start()
        ~~~~~~~~~~~~~~~~~~~~~~~~~^^
      File "/tmp/samuel/Volatile/AppDir/lib/python3.13/site-packages/oversteer/device_manager.py", line 54, in start
        context = pyudev.Context()
      File "/tmp/samuel/Volatile/AppDir/lib/python3.13/site-packages/pyudev/core.py", line 59, in __init__
        self._libudev = load_ctypes_library("udev", SIGNATURES, ERROR_CHECKERS)
                        ~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/tmp/samuel/Volatile/AppDir/lib/python3.13/site-packages/pyudev/_ctypeslib/utils.py", line 51, in load_ctypes_library
        raise ImportError("No library named %s" % name)
    ImportError: No library named udev
    

    A similar issue happened with another python app that used pyusb.

    Maybe the check for ldconfig -p stops any further lookup?

  4. picnixz commented on Nov 27, 2025

    @picnixz
    Member

    In this case, i think it's an issue with pyudev and not CPython itself. load_ctypes_library is a pyudev function and pyudev/_ctypeslib/utils.py is not the ctypes from CPython.

  5. picnixz commented on Nov 27, 2025

    @picnixz
    Member

    Also, the issue doesn't seem related to ldconfig. It looks like the host machine is missing the .so library for udev (that is the corresponding *-devel or *-dev package depending on the distro)

  6. picnixz commented on Nov 27, 2025

    @picnixz
    Member

    More generally, what you could actually want is for ctypes to still find your library I guess? ctypes.find_library tries quite hard to find libs but it's not always possible to have a very generic solution for that, especially if it's for platforms that we don't support officially as per PEP-11. I would suggest to report this issue to pyudev instead and ask them if it's possible to try harder for finding libraries (it looks like they are doing their own machinery for that).

  7. Samueru-sama commented on Nov 27, 2025

    @Samueru-sama
    Author

    In this case, i think it's an issue with pyudev and not CPython itself. load_ctypes_library is a pyudev function and pyudev/_ctypeslib/utils.py is not the ctypes from CPython.

    Isn't pyudev using find_library from here though?

    Also, the issue doesn't seem related to ldconfig. It looks like the host machine is missing the .so library for udev (that is the corresponding *-devel or *-dev package depending on the distro)

    It is related to ldconfig, after we added it and made sure to redirect all calls to /sbin/ldconfig to the bundled one that supports -p the issue was fixed.

    And as previously mentioned this is weird that it even works, because that bundled ldconfig is outputting the libraries of the host, not the bundled ones, yet it eventually finds the bundled ones without problem.

    In other words this is failing because /sbin/ldconfig -p fails, not because it cannot find the library.

  8. picnixz commented on Nov 27, 2025

    @picnixz
    Member

    Isn't pyudev using find_library from here though?

    I don't know. What I can tell you is that the call to ldconfig is protected by a try-catch. The exception being raised is not raised from CPython but from pyudev.

  9. picnixz commented on Nov 27, 2025

    @picnixz
    Member

    Ref:

    cpython/Lib/ctypes/util.py

    Lines 297 to 310 in 3bbd669

    # XXX assuming GLIBC's ldconfig (with option -p)
    regex = r'\s+(lib%s\.[^\s]+)\s+\(%s'
    regex = os.fsencode(regex % (re.escape(name), abi_type))
    try:
    with subprocess.Popen(['/sbin/ldconfig', '-p'],
    stdin=subprocess.DEVNULL,
    stderr=subprocess.DEVNULL,
    stdout=subprocess.PIPE,
    env={'LC_ALL': 'C', 'LANG': 'C'}) as p:
    res = re.search(regex, p.stdout.read())
    if res:
    return os.fsdecode(res.group(1))
    except OSError:
    pass

  10. picnixz commented on Nov 27, 2025

    @picnixz
    Member

    The exception being raised is here:

    https://github.com/pyudev/pyudev/blob/7588bc8c84bf1e994b31020452124bc6618625ce/src/pyudev/_ctypeslib/utils.py#L49-L51

    IOW, ctypes.find_library returns None which pyudev considers to indicate that the library is missing which is the case I guess. I would suggest reporting this to pyudev. It's not an issue in CPython (unless you want us to allow to specify the path to ldconfig?)

  11. picnixz commented on Nov 27, 2025

    @picnixz
    Member

    cc @encukou as ctypes maintainer (I need to leave)

  12. Samueru-sama commented on Nov 27, 2025

    @Samueru-sama
    Author

    I thought the problem was from here because ctypes is the only thing that contains the /sbin/ldconfing and it attempts to execute /sbin/ldconfig -p

    Image

    This problem gets fixed by bundling an ldconfig that supports -p, even though its output is totally useless here, this fixes the issue I'm having.

    So I guess the exception stops pyudev from trying to keep looking up?

    (this problem also affects pyusb btw)

  13. Samueru-sama commented on Nov 27, 2025

    @Samueru-sama
    Author

    Here is an LD_DEBUG=libs output of the working appimage with a bundled ldconfig: libs.log

    Doesn't attempt any of the libraries reported by the bundled ldconfig -p. (which is good, it is all working fine btw)

    So my current theory is that pyudev fails because find_library errors? stops? when ldconfig -p is not possible.

  14. 21 remaining items

  15. Samueru-sama commented on Dec 4, 2025

    @Samueru-sama
    Author

    I think we've established that the current implementation of find_library is not reliable. The question is: is there a reliable method of doing what you need? If so, we can update the existing function, or add a new one if that would be an incompatible change.

    Alright, the ideal solution is finding a way to dlopen a dummy library and check what directories were looked at, this is the perfect solution since this will 100% reliably list all possible library locations.

    If that is not possible, the other solution is to just not hardcode ldconfig to sbin and just use PATH, you will still ned to check for /sbin/ldconfig since some distros don't expose /sbin in PATH by default.

    note that right now if I attempt to patch this to use the ldconfig in PATH, the variable seems to be reset since it only looks in /bin and /usr/bin on my end.

    Another is having a variable like PYTHON_LIBRARY_PATH that we can list the directories to be looked at, however I'm a bit worried this will cause issues since it can leak to other python instances on the system similar the issues when using LD_LIBRARY_PATH

  16. added a commit that references this issue on Dec 15, 2025
  17. added 2 commits that reference this issue on Dec 15, 2025
  18. added a commit that references this issue on Dec 16, 2025
  19. added 2 commits that reference this issue on Dec 16, 2025
  20. encukou commented on Feb 23, 2026

    @encukou
    Member

    I'm proposing to soft-deprecate ctypes.util.find_library: https://discuss.python.org/t/106232

  21. Samueru-sama commented on Feb 23, 2026

    @Samueru-sama
    Author

    I'm proposing to soft-deprecate ctypes.util.find_library: https://discuss.python.org/t/106232

    I don't think this is needed.

    I while back we noticed with python it is no longer needed to PYTHONHOME anymore, python figures out the location of the pythondir on its own, see for example the output of an appimage that has python bundled without setting PYTHONHOME:

    ./puddletag.AppImage python -c "import sys,os; print('exe=', sys.executable); print('prefix=', sys.prefix); print('paths='); print('\n'.join(sys.path))"                  222ms
    exe= /tmp/samuel/.mount_puddlremp6111792694178396282/bin/python
    prefix= /tmp/samuel/.mount_puddlremp6111792694178396282
    paths=
    
    /tmp/samuel/.mount_puddlremp6111792694178396282/lib/python314.zip
    /tmp/samuel/.mount_puddlremp6111792694178396282/lib/python3.14
    /tmp/samuel/.mount_puddlremp6111792694178396282/lib/python3.14/lib-dynload
    /tmp/samuel/.mount_puddlremp6111792694178396282/lib/python3.14/site-packages

    This logic can be used to find the libraries as well, basically all the *.so files in the directory where the python dir is located.

  22. encukou commented on Mar 11, 2026

    @encukou
    Member

    This logic can be used to find the libraries as well, basically all the *.so files in the directory where the python dir is located.

    Yes. Many approaches are better than find_library's logic of running ldconfig/gcc/objdump/ld.

  23. encukou commented on Mar 20, 2026

    @encukou
    Member

    find_library is now soft-deprecated. It cannot be made to work correctly and consistently on all platforms, and any change runs a chance to break whatever workarounds people already have in place.
    I'm prioritizing stability over new features, and encourage solving this outside ctypes itself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions