Skip to content

FIX: Use dynamic_lookup for Python Library in macOS #98

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

Merged
merged 4 commits into from
Jun 29, 2025

Conversation

bewithgaurav
Copy link
Collaborator

@bewithgaurav bewithgaurav commented Jun 24, 2025

Summary

This pull request simplifies and standardizes the linking process for Python symbols on macOS in the mssql_python/pybind/CMakeLists.txt file. The change ensures compatibility across all Python environments, including virtual environments, by always using dynamic lookup for Python symbols.

MacOS-specific changes:

  • mssql_python/pybind/CMakeLists.txt: Removed conditional logic for linking against the Python framework or a specific Python library. Instead, the code now always uses the -undefined dynamic_lookup flag for Python symbols, ensuring better compatibility across Python environments.

Issue Reference

Fixes AB#37773
Fixes the GH Issue - #99

###Additional context
The root cause is that the C extension binary was built with a hardcoded path to the Python framework at /Library/Frameworks/Python.framework/Versions/3.13/Python. This can be verified using otool -L on the .so file:

mssql_python/ddbc_bindings.cp313-universal2.so (architecture x86_64):
    /Library/Frameworks/Python.framework/Versions/3.13/Python (compatibility version 3.13.0, current version 3.13.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1351.0.0)
    /usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 1900.180.0)

The fix is to modify CMakeLists.txt to use the -undefined dynamic_lookup linking approach, which is the recommended practice for Python extensions on macOS. This ensures the extension works regardless of where or how Python is installed.

@Copilot Copilot AI review requested due to automatic review settings June 24, 2025 12:30
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR simplifies and standardizes the linking process for Python symbols on macOS by always using the dynamic lookup flag, eliminating conditional logic previously required to link against the Python framework or a Python library.

  • Removed conditional branching for library vs. framework linking.
  • Always applies the -undefined dynamic_lookup flag to support various Python environments.
Comments suppressed due to low confidence (2)

mssql_python/pybind/CMakeLists.txt:244

  • Include a brief note clarifying why eliminating the conditional logic improves compatibility across Python environments to assist future maintainers.
    message(STATUS "Using dynamic lookup for Python symbols on macOS (best practice for Python extensions)")

mssql_python/pybind/CMakeLists.txt:245

  • Review whether linking against 'dl' (done on the subsequent line) is still required when using dynamic_lookup on macOS, and remove it if redundant.
    set_target_properties(ddbc_bindings PROPERTIES LINK_FLAGS "-undefined dynamic_lookup")

@bewithgaurav bewithgaurav merged commit 74037cf into main Jun 29, 2025
9 checks passed
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.

Python extension fails on macOS due to hardcoded Python framework path
3 participants