From dfed135bc3395d25048f423a751a5eb7d5200abc Mon Sep 17 00:00:00 2001 From: Pramod S Kumbhar Date: Mon, 23 Sep 2024 16:48:59 +0200 Subject: [PATCH] Do not link to libpython on MacOS with Anaconda Python - Anaconda distribution on MacOS ship binary with static libpython and that causes segfault - See details in https://github.com/neuronsimulator/nrn/issues/2358#issuecomment-2368515786 Fixes #2358 --- cmake/PythonHelper.cmake | 9 +++++++++ docs/install/install_instructions.md | 10 ++++++++++ src/nrnpython/CMakeLists.txt | 5 ++++- 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/cmake/PythonHelper.cmake b/cmake/PythonHelper.cmake index b4afd4ca4e..2e87b9cca7 100644 --- a/cmake/PythonHelper.cmake +++ b/cmake/PythonHelper.cmake @@ -178,6 +178,15 @@ function(nrn_find_python) PARENT_SCOPE) endfunction() +# Check if Python from Conda is being used on MacOS +if(NRN_MACOS_BUILD) + string(FIND "${PYTHON_EXECUTABLE}" "/anaconda" anaconda_found) + string(FIND "${PYTHON_EXECUTABLE}" "/miniconda" miniconda_found) + if(anaconda_found GREATER -1 OR miniconda_found GREATER -1) + set(NRN_WITH_MACOS_CONDA_PYTHON TRUE) + endif() +endif() + # For each Python in NRN_PYTHON_EXECUTABLES, find its version number, its include directory, and its # library path. Store those in the new lists NRN_PYTHON_VERSIONS, NRN_PYTHON_INCLUDES and # NRN_PYTHON_LIBRARIES. Set NRN_PYTHON_COUNT to be the length of those lists, and diff --git a/docs/install/install_instructions.md b/docs/install/install_instructions.md index dcce802840..a5024dd9df 100644 --- a/docs/install/install_instructions.md +++ b/docs/install/install_instructions.md @@ -493,3 +493,13 @@ If it prints `2.0` or higher, try installing an older version: pip install "numpy<2" ``` (mind the quotes.) Then delete the build directory, reconfigure and compile. If the error persists, carefully check which version of Python NEURON picked up by checking the output of the CMake configure command and make sure that that exact version of Python doesn't pick up an incompatible version of Numpy. + + +* **NEURON segfaults when using the Anaconda Python distribution. What can I do?** + +Some Anaconda distributions (e.g., macOS) ship Python binaries with `libpython` statically linked, +which has caused issues in NEURON and other packages (see discussion [here](https://github.com/neuronsimulator/nrn/issues/2358)). + +On the macOS platform, NEURON attempts to detect the use of Anaconda Python by checking for the `/anaconda` +prefix in the Python binary path. An alternative solution is to build NEURON with the dynamic Python +option enabled, using the CMake flag `-DNRN_ENABLE_PYTHON_DYNAMIC=ON`. diff --git a/src/nrnpython/CMakeLists.txt b/src/nrnpython/CMakeLists.txt index 92f28c651f..1a9696de9d 100644 --- a/src/nrnpython/CMakeLists.txt +++ b/src/nrnpython/CMakeLists.txt @@ -69,7 +69,10 @@ else() set_property(TARGET nrnpython PROPERTY POSITION_INDEPENDENT_CODE ON) target_include_directories(nrnpython PUBLIC ${INCLUDE_DIRS}) target_include_directories(nrnpython PUBLIC "${NRN_DEFAULT_PYTHON_INCLUDES}") - target_link_libraries(nrnpython ${NRN_DEFAULT_PYTHON_LIBRARIES}) + # see nrn/issues/2358 + if(NOT NRN_WITH_MACOS_CONDA_PYTHON) + target_link_libraries(nrnpython ${NRN_DEFAULT_PYTHON_LIBRARIES}) + endif() target_link_libraries(nrnpython fmt::fmt) target_include_directories(nrnpython PUBLIC ${PROJECT_SOURCE_DIR}/${NRN_3RDPARTY_DIR}/eigen) target_include_directories(nrnpython PUBLIC ${PROJECT_BINARY_DIR}/src/nrniv/oc_generated)