Skip to content

Commit

Permalink
Do not link to libpython on MacOS with Anaconda Python (#3088)
Browse files Browse the repository at this point in the history
- Anaconda distribution on MacOS ship binary with static libpython and that causes segfault
- See details in #2358 (comment)

Fixes #2358
  • Loading branch information
pramodk authored Sep 23, 2024
1 parent 80a2c01 commit d4ec267
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
9 changes: 9 additions & 0 deletions cmake/PythonHelper.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions docs/install/install_instructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
5 changes: 4 additions & 1 deletion src/nrnpython/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit d4ec267

Please sign in to comment.