Skip to content

Fix importing build extension for Python >= 3.8 in Windows#12

Merged
diegoferigo merged 4 commits intomasterfrom
fix/win
May 27, 2021
Merged

Fix importing build extension for Python >= 3.8 in Windows#12
diegoferigo merged 4 commits intomasterfrom
fix/win

Conversation

@diegoferigo
Copy link
Copy Markdown
Owner

@diegoferigo diegoferigo commented May 27, 2021

Fixes #8

Apparently the problem is not a bug but a change of behavior in Windows. Refer to add_dll_directory for more details.

Differently from many, I implemented this fix using a context manager that automatically closes the dll cookies. Since this functionality is necessary to properly import SWIG bindings in the affected platform (I suspect that also pybind11 is affected), I added the context manager to cmake-build-extension so that it can be used downstream. The only change is that, if the context manager is used e.g. in the __init__.py file, the downstream package has to mark cmake-build-extension as install_require and no longer as only setup_require (or, in a more modern setup, in the requires field of pyproject.toml).

from cmake_build_extension import build_extension_env

with build_extension_env():
    # Assuming that 'bindings' is the name of the SWIG / pybind11 module
    from . import bindings

Related resources from which this PR was inspired:

@diegoferigo diegoferigo added the enhancement New feature or request label May 27, 2021
@diegoferigo diegoferigo self-assigned this May 27, 2021
@diegoferigo diegoferigo merged commit cefa43e into master May 27, 2021
@diegoferigo diegoferigo deleted the fix/win branch May 27, 2021 20:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Example cannot be imported in Windows if python >= 3.8

1 participant