You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows10 Spyder环境中调用Fortran函数时出现AttributeError: module 'myflib2' has no attribute 'foo'的问题求助

Windows10 Spyder环境中调用Fortran函数时出现AttributeError: module 'myflib2' has no attribute 'foo'的问题求助

大家好,我最近在尝试从Python脚本里调用Fortran代码中的函数,遇到了一个棘手的问题,想请教一下各位大佬。

我按照步骤先编写了Fortran代码文件my_fortran_lib.f90,代码内容如下:

! my_fortran_lib.f90:
function foo(a) result(b)
implicit none
real(kind=8), intent(in) :: a(:,:)
complex(kind=8) :: b(size(a,1),size(a,2))
b = exp((0,1)*a)
end function foo

接着在Windows10的命令提示符里执行了编译命令:

f2py3 -c -m myflib2 my_fortran_lib.f90

编译完成后,生成了myflib2.cp311-win_amd64.pyd和libmy_fortr.VGRWKJX2MMMS3CK3OCJQHKECLLH3CKTE.gfortran-win_amd64.dll两个文件,我把它们放到了myflib2文件夹中,这个文件夹和我的其他Python库处于同一目录下。

之后我在Spyder中运行测试代码:

import myflib2
a = np.array([[1,2,3,4], [5,6,7,8]], order='F')
myflib2.foo(a)

结果却抛出了以下错误:

Traceback (most recent call last):
Cell In[10], line 1
myflib2.foo(a)
AttributeError: module 'myflib2' has no attribute 'foo'

可以看到程序能成功找到myflib2模块,但就是找不到里面的foo函数。我已经尝试过更换不同的Fortran文件格式(比如.f和.f90),也试过编写其他简单函数测试,但问题依然存在。有没有朋友能帮我分析一下问题出在哪里呀?提前谢谢大家的帮助!

备注:内容来源于stack exchange,提问作者Eva

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.21 08:45:29