将基于 f2py 的 Python 模块从 numpy.distutils 移动到 scikit-build-core 时出现问题

问题描述 投票:0回答:1

我维护一个名为 Tsyganenko 的 Python 模块,当前有一个 setup.py

 文件,如下所示:

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", )
由于 

scikit-build-core

 已被弃用,我正在尝试将其移至使用 
numpy.distutils
。我正在尝试按照 
scikit-build-core
 文档
来执行此操作,并得到如下所示的
pyproject.toml

[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"]
和一个

CMakeLists.txt

文件,如下所示:

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 .)
据称,它已成功安装,但在尝试导入模块时出现以下错误。我不太了解 
scikit-build-core

文档,无法调试它。我相当确定这是一个问题,

T96.f
T02.f
CMakeLists.txt
中的任何地方都没有提到,但我不知道如何添加它们,而且我不确定是否需要包括
geopack_py.pyf 
。有人可以帮忙吗?
如果你想直接访问代码,都在GitHub上模块的change-setup分支中。
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_'

	
python cmake f2py scikit-build
1个回答
0
投票
add_custom_command

以包含此 .pyf 文件和其他 Fortran 文件:

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

您也可以尝试将所有 Fortran 文件包含在
python_add_library

命令中

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"
  "${CMAKE_CURRENT_SOURCE_DIR}/src/tsyganenko/T96.f"
  "${CMAKE_CURRENT_SOURCE_DIR}/src/tsyganenko/T02.f"
  WITH_SOABI
)

在此之后使用 scikit-build-core 重建一切:

pip install .

并尝试
import geopack_py

    

© www.soinside.com 2019 - 2024. All rights reserved.