Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix loading of Python bindings on Windows when installed in arbitrary directory #3146

Merged
merged 2 commits into from
Nov 12, 2024

Conversation

traversaro
Copy link
Member

@traversaro traversaro commented Oct 24, 2024

Since Python 3.8, Python ignores the PATH env variable when trying to load the DLL of compiled Python extensions.

We kind ignored the problem until now as if you install in %CONDA_PREFIX%\Library\bin in conda environment everything works out of the box, but the problem is present when bipedal-locomotion-framework is installed in an arbitrary install prefix and the Python bindings are found via PYTHONPATH env variable, see robotology/robotology-superbuild#1268 .

This PR fixes the problem by adding some automatically generated code in the __init__.py file that automatically calls os.add_dll_directory as necessary.

As the Python code is generated by CMake it may be a bit cryptic, the actual rendered code is:

import os
library_dll_path = os.path.join(os.path.dirname(__file__),'../../bin')
# Avoid to call add_dll_directory if not necessary,
# for example if the library to find are already found in the proper location in a conda environment
if(library_dll_path != os.path.join(os.environ.get('CONDA_PREFIX', ''),'Library','bin') and library_dll_path != os.path.join(os.environ.get('CONDA_PREFIX', ''),'bin')):
    if(os.path.exists(library_dll_path)):
        os.add_dll_directory(library_dll_path)

Similar to:

Copy link

update-docs bot commented Oct 24, 2024

Thanks for opening this pull request! The maintainers of this repository would appreciate it if you would update the release notes by adding a file in doc/release/<target_branch>, based on your changes.

@traversaro
Copy link
Member Author

The failure in Valgring job 22 is unrelated to this PR, but will be fixed by #3147 .

@randaz81 randaz81 self-assigned this Nov 12, 2024
@randaz81 randaz81 added the Component: Bindings swig, python, java, ruby, perl, octave, matlab, lua, csharp, tcl label Nov 12, 2024
@randaz81 randaz81 added this to the YARP 3.10.0 milestone Nov 12, 2024
@randaz81 randaz81 merged commit 209637c into robotology:master Nov 12, 2024
44 of 50 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: Bindings swig, python, java, ruby, perl, octave, matlab, lua, csharp, tcl
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants