Skip to content

FindNumPy finds system numpy, not virtual env numpy #524

@roryyorke

Description

@roryyorke

I'm using scikit-build to build python-control/Slycot in a conda virtual environment. I don't think I have any unusual environment variables or any other strange setup. I also think it's reasonable to expect the virtual-env Numpy (etc.) to be preferred to the system Numpy (etc.).

The CMakeLists.txt below demonstrates what I think is the problem; it's unfortunately specific to my virtual-env for _numpy_include_dir, which is the result of numpy.get_include().

One could argue that if numpy.get_include() succeeds, one doesn't need find_path.

cmake_minimum_required(VERSION 3.11.0)

project(noproject VERSION 0.0.1 LANGUAGES NONE)

message(STATUS "CMAKE_SYSTEM_INCLUDE_PATH: ${CMAKE_SYSTEM_INCLUDE_PATH}")
message(STATUS "CMAKE_SYSTEM_PREFIX_PATH: ${CMAKE_SYSTEM_PREFIX_PATH}")

set(_numpy_include_dir "/home/rory/.miniconda3/envs/slycotdev/lib/python3.8/site-packages/numpy/core/include")

find_path(NumPy_INCLUDE_DIR_1
  numpy/arrayobject.h
  PATHS "${_numpy_include_dir}"
  NO_DEFAULT_PATH
  )

find_path(NumPy_INCLUDE_DIR_2
  numpy/arrayobject.h
  PATHS "${_numpy_include_dir}"
  )

message(STATUS "NumPy_INCLUDE_DIR_1: ${NumPy_INCLUDE_DIR_1}")
message(STATUS "NumPy_INCLUDE_DIR_2: ${NumPy_INCLUDE_DIR_2}")

Output:

-- CMAKE_SYSTEM_INCLUDE_PATH: /usr/include/X11
-- CMAKE_SYSTEM_PREFIX_PATH: /usr/local;/usr;/;/home/rory/.miniconda3/envs/slycotdev;/usr/local;/usr/X11R6;/usr/pkg;/opt
-- NumPy_INCLUDE_DIR_1: /home/rory/.miniconda3/envs/slycotdev/lib/python3.8/site-packages/numpy/core/include
-- NumPy_INCLUDE_DIR_2: /usr/include
-- Configuring done
-- Generating done
-- Build files have been written to: /home/rory/projects/slycot/cmake-weirdness/build

This is a feature of CMake; see end of https://cmake.org/cmake/help/latest/command/find_path.html. Why the search order is first CMAKE_SYSTEM_PREFIX_PATH and then the specified paths I don't understand (that page says "The default search order is designed to be most-specific to least-specific for common use cases.").

A simple (and so possibly entirely incorrect!) fix is to do what is suggested in the find_pathdocs:

find_path(NumPy_INCLUDE_DIR
  numpy/arrayobject.h
  PATHS "${_numpy_include_dir}" "${PYTHON_INCLUDE_DIR}"
  PATH_SUFFIXES numpy/core/include
  NO_DEFAULT_PATH
  )

find_path(NumPy_INCLUDE_DIR
  numpy/arrayobject.h
  PATHS "${_numpy_include_dir}" "${PYTHON_INCLUDE_DIR}"
  PATH_SUFFIXES numpy/core/include
  )

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions