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

Merged
merged 2 commits into from
Sep 29, 2024

Conversation

traversaro
Copy link
Member

@traversaro traversaro commented Sep 25, 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 iDynTree 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
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)

Were the correct value for '../../../bin' is automatically generated by CMake.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant