首页 > 解决方案 > Python f2py 重新加载更改的模块

问题描述

我是编程新手和python. 我已经设法使用f2py将 fortran 模块导入到python. 但是有一个小问题我无法弄清楚。当我更改 fortran 代码并再次使用f2py刷新模块时,编译器仍会识别模块的第一个版本。如何强制它刷新模块?我已经阅读了关于 的内容importlib.reload,但它对我没有任何改变。我指的是python3.7

fortran 代码 two.f90 是:

module two
 implicit none

 contains

 subroutine twofunc(x,y)
     real,intent(in) :: x
     real,intent(out) :: y
     y = x*x
 end subroutine twofunc

end module

然后我将其更改为:

module two
implicit none

contains

subroutine twofunc(x,y)
    real,intent(in) :: x
    real,intent(out) :: y
    y = x*x*x
  end subroutine twofunc

end module.

这只是一个测试代码。然后,我运行了 f2py。然后在 Spyder 中:

import importlib
import two 
importlib.reload(one10)
y=two.two.twofunc(4)
print(y)

尽管将代码从 x^2 更改为 x^3,但结果仍然是 16。

标签: python-3.xf2py

解决方案


reload命令不适用于所有情况。f2py 模块,可能是因为它们包含已编译的代码,通常无法重新加载。

您需要重新启动内核以迭代扩展模块开发(菜单“控制台”-> 重新启动内核或在控制台选项卡上右键单击 -> 重新启动内核)。


推荐阅读