Search code examples
pythoncmakef2pyscikit-build

Problems moving f2py-based Python module from numpy.distutils to scikit-build-core


I maintain a Python module called Tsyganenko which currently has a setup.py file as below:

from numpy.distutils.core import Extension, setup

ext = Extension('geopack',
                sources=['src/tsyganenko/geopack_py.pyf',
                         'src/tsyganenko/geopack_py.f',
                         'src/tsyganenko/T96.f',
                         'src/tsyganenko/T02.f'])

with open("README.md", "r", encoding="utf-8") as f:
    long_description = f.read()

setup(author="John Coxon and Sebastien de Larquier",
      author_email="[email protected]",
      classifiers=[
          "Development Status :: 4 - Beta",
          "Intended Audience :: Science/Research",
          "Natural Language :: English",
          "Programming Language :: Python/Fortran"
      ],
      description="A wrapper to call GEOPACK routines in Python.",
      ext_modules=[ext],
      install_requires=[
          "numpy",
          "matplotlib",
          "pandas"
      ],
      keywords=['Scientific/Space'],
      long_description=long_description,
      long_description_content_type="text/markdown",
      name="Tsyganenko",
      package_dir={"": "src"},
      packages=setuptools.find_packages(where="src"),
      python_requires=">=3.9",
      url="https://github.com/johncoxon/tsyganenko",
      version="2020.1",
      )

I am trying to move this over to use scikit-build-core due to numpy.distutils being deprecated. I am trying to follow the scikit-build-core documentation to do this, and have arrived at a pyproject.toml that looks like this:

[build-system]
requires = ["scikit-build-core", "numpy"]
build-backend = "scikit_build_core.build"

[project]
name = "Tsyganenko"
version = "2020.2"
dependencies = ["numpy"]

[tool.scikit-build]
ninja.version = ">=1.10"
cmake.version = ">=3.17.2"

[tool.setuptools.packages.find]
where = ["src"]

and a CMakeLists.txt file which looks like this:

cmake_minimum_required(VERSION 3.17.2...3.29)
project(${SKBUILD_PROJECT_NAME} LANGUAGES C Fortran)

find_package(
  Python
  COMPONENTS Interpreter Development.Module NumPy
  REQUIRED)

# F2PY headers
execute_process(
  COMMAND "${PYTHON_EXECUTABLE}" -c
          "import numpy.f2py; print(numpy.f2py.get_include())"
  OUTPUT_VARIABLE F2PY_INCLUDE_DIR
  OUTPUT_STRIP_TRAILING_WHITESPACE)

add_library(fortranobject OBJECT "${F2PY_INCLUDE_DIR}/fortranobject.c")
target_link_libraries(fortranobject PUBLIC Python::NumPy)
target_include_directories(fortranobject PUBLIC "${F2PY_INCLUDE_DIR}")
set_property(TARGET fortranobject PROPERTY POSITION_INDEPENDENT_CODE ON)

add_custom_command(
  OUTPUT geopack_pymodule.c geopack_py-f2pywrappers.f
  DEPENDS src/tsyganenko/geopack_py.f
  VERBATIM
  COMMAND "${Python_EXECUTABLE}" -m numpy.f2py
          "${CMAKE_CURRENT_SOURCE_DIR}/src/tsyganenko/geopack_py.f" -m geopack_py --lower)

python_add_library(
  geopack_py MODULE "${CMAKE_CURRENT_BINARY_DIR}/geopack_pymodule.c"
  "${CMAKE_CURRENT_BINARY_DIR}/geopack_py-f2pywrappers.f"
  "${CMAKE_CURRENT_SOURCE_DIR}/src/tsyganenko/geopack_py.f" WITH_SOABI)
target_link_libraries(geopack_py PRIVATE fortranobject)

install(TARGETS geopack_py DESTINATION .)

It successfully installs, allegedly, but when trying to import the module I get the following error. I don't understand the scikit-build-core documentation well enough to be able to debug it. I'm fairly certain that it's a problem that T96.f and T02.f aren't mentioned anywhere in CMakeLists.txt but I'm not sure how to add them, and I'm not sure whether I need to include geopack_py.pyf. Can anyone assist? If you want to access the code directly, it's all in the change-setup branch of the module on GitHub.

In [1]: import geopack_py
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
Cell In[1], line 1
----> 1 import geopack_py

ImportError: dlopen(/opt/homebrew/Caskroom/mambaforge/base/envs/test/lib/python3.12/site-packages/geopack_py.cpython-312-darwin.so, 0x0002): symbol not found in flat namespace '_t01_01_'

Solution

  • Your stumbling block is that for some reason numpy.f2py doesn't like the name geopack_py -- specifically, it doesn't like the _py. So, renaming the relevant files to geopack.f and geopack.pyf, and modifying your CMakeLists.txt to:

    add_custom_command(
      OUTPUT geopackmodule.c geopack-f2pywrappers.f
      DEPENDS
        src/tsyganenko/geopack.pyf
        src/tsyganenko/geopack.f
        src/tsyganenko/T96.f
        src/tsyganenko/T02.f
      COMMAND "${Python_EXECUTABLE}" -m numpy.f2py
        -m geopack
        --lower
        "${CMAKE_CURRENT_SOURCE_DIR}/src/tsyganenko/geopack.pyf"
        "${CMAKE_CURRENT_SOURCE_DIR}/src/tsyganenko/geopack.f"
        "${CMAKE_CURRENT_SOURCE_DIR}/src/tsyganenko/T96.f"
        "${CMAKE_CURRENT_SOURCE_DIR}/src/tsyganenko/T02.f"
    )
    
    python_add_library(
      geopack MODULE "${CMAKE_CURRENT_BINARY_DIR}/geopackmodule.c"
      "${CMAKE_CURRENT_BINARY_DIR}/geopack-f2pywrappers.f"
      "${CMAKE_CURRENT_SOURCE_DIR}/src/tsyganenko/geopack.f"
      "${CMAKE_CURRENT_SOURCE_DIR}/src/tsyganenko/T96.f"
      "${CMAKE_CURRENT_SOURCE_DIR}/src/tsyganenko/T02.f"
      WITH_SOABI)
    target_link_libraries(geopack PRIVATE fortranobject)
    
    install(TARGETS geopack DESTINATION .)
    

    we now have something that builds and can be imported.