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 #905

Merged
merged 3 commits into from
Oct 25, 2024

Conversation

traversaro
Copy link
Collaborator

@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:

@GiulioRomualdi GiulioRomualdi merged commit a60b73d into master Oct 25, 2024
11 of 12 checks passed
@GiulioRomualdi GiulioRomualdi deleted the traversaro-patch-1 branch October 25, 2024 06:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants