numpy.f2py - 使用 pip

问题描述

我正在尝试围绕 f2py 进行思考,因为我的组织有很多遗留的 Fortran 代码,我想将它们合并到我正在编写的一些基于 Python 的新工具中。理想情况下,我会将这些工具打包在源代码包或轮子中,以便更轻松地分发给组织的其他人员。

我根据我见过的一些其他示例编写了一个小型测试包,这些示例只是对浮点数组求和。包装内容如下。如果我使用 py setup.py sdist 构建源代码分发包,一切看起来都正常。它甚至看起来像 pip 成功安装了它。但是,如果我打开一个 python shell 并尝试导入新安装的模块,我会在初始化脚本的 from fastadd import fadd 行上收到一个错误,说

AttributeError: module 'fastadd' has no attribute 'fastadd'

所以看起来它实际上并没有成功构建 f2py 模块。做一些故障排除,如果我在包文件夹中打开一个 powershell 窗口并运行

py -m numpy.f2py -c fadd.pyf fadd.f90

然后在同一个文件夹中打开一个 python shell 并尝试 import fastadd,我得到一个错误ImportError: DLL load Failed: The specified module Could not be found.(这是在我安装了 Visual Studio 构建工具之后,建议在多个线程上进行修复)。按照 this thread 上的建议,将命令更改为

py -m numpy.f2py -c --fcompiler=gnu95 --compiler=mingw32 fadd.pyf fadd.f90

将构建一个我可以成功导入和使用的模块文件。好的,太好了。

但是,当我在设置文件中更改 config.add_extension 以包含关键字参数 f2py_options=["--fcompiler=gnu95","--compiler=mingw32"] 并尝试使用 setup.py sdist 命令构建包分发文件时然后使用 py -m pip install fastadd-1.0a1.tar.gz 安装,我得到一个不同的错误

ERROR: No .egg-info directory found in C:\Users\username\AppData\Local\Temp\pip-pip-egg-info-c7406k03

现在我完全糊涂了。 f2py_options 的其他配置要么导致 setup.py 抛出错误,要么完全无法创建扩展,与上面类似。对选项使用简单的字符串会产生错误,因此显然 f2py_options 实际上确实需要列表输入。我似乎找不到关于我是否正确使用 f2py_options 的任何好的文档,而且我不知道为什么只添加该选项会导致 pip 不知道它的 info 目录在哪里。这对我来说没有意义。我真的很感激这方面的一些帮助。

我在 Windows 10 机器上运行 Python 3.7.0 32 位、numpy 1.20.1 和 pip 21.0.1。

--编辑--

查看测试模块的安装目录,我发现了这个问题的一个新问题:安装目录实际上不包含MANIFEST中列出的任何文件,甚至没有包含__init__.py文件。如果我将 __init__.py 复制到目录中,尝试导入模块会出现与我遇到的相同的 ImportError: DLL load Failed 错误

此外,检查 py -m pip install输出,看起来 numpy.distutils 不将 --fcompiler--compiler 识别为有效选项并且只是忽略它们,即使 {{ 1}} 确实认识他们。

--结束编辑--

包装内容

numpy.f2py

+-fastadd ---__init__.py ---fadd.f90 ---fadd.pyf -MANIFEST.in -README -setup.py 有以下内容

fadd.f90

subroutine fadd(vals,n,mysum) integer,intent(in) :: n real*8,intent(out):: mysum real*8,dimension(n),intent(in) :: vals mysum = sum(vals) end subroutine fadd 有以下内容

fadd.pyf

python module fastadd ! in interface ! in :fastadd subroutine fadd(vals,mysum) ! in :fastadd:fadd.f90 real*8 dimension(n),intent(in) :: vals integer,optional,intent(in),check(len(vals)>=n),depend(vals) :: n=len(vals) real*8 intent(out) :: mysum end subroutine fadd end interface end python module fastadd

__init__.py

"""This is the documentation!""" from .fastadd import fadd

MANIFEST.in

最后,include README recursive-include fastadd *.f90 recursive-include fastadd *.pyf recursive-include fastadd *.py

setup.py

如果有帮助,运行安装脚本后最终的 def configuration(pth=None): from numpy.distutils.misc_util import Configuration config = Configuration( 'fastadd',top_path=pth,version='1.0a1',author='John Doe',author_email='[email protected]',url='fake-org.biz/fastadd',description="Testing f2py build process. Sums an arbitrary-length list of numbers.") config.add_extension( 'fastadd',sources=['fastadd\\fadd.pyf','fastadd\\fadd.f90'] ) return config if __name__ == '__main__': from numpy.distutils.core import setup setup(**configuration('fastadd').todict()) 文件如下所示:

MANIFEST

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)