我正在尝试创建一个在 Python 中使用的 Fortran 模块,但它无法在 Windows 上运行,因为
Cannot open module file 'fmodule.mod' for reading at (1): No such file or directory
。
我有这个脚本
module.f90
:
module fmodule
contains
subroutine sub(a)
real a(3)
a(1) = 1.0
end subroutine sub
end module fmodule
并尝试直接运行f2py
python -m numpy.f2py -c -m module module.f90
或带有签名文件
python -m numpy.f2py module.f90 -m module -h module.pyf
python -m numpy.f2py -c module.pyf module.f90
它返回:
C:\Users\tanus.szabo\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\numpy\f2py\f2py2e.py:686: VisibleDeprecationWarning: distutils has been deprecated since NumPy 1.26.Use the Meson backend instead, or generate wrapperswithout -c and use a custom build script
builder = build_backend(
running build
running config_cc
INFO: unifing config_cc, config, build_clib, build_ext, build commands --compiler options
running config_fc
INFO: unifing config_fc, config, build_clib, build_ext, build commands --fcompiler options
running build_src
INFO: build_src
INFO: building extension "fmodule" sources
INFO: f2py options: []
INFO: f2py:> C:\Users\TANUS~1.SZA\AppData\Local\Temp\tmpicd2x_7_\src.win-amd64-3.11\fmodulemodule.c
creating C:\Users\TANUS~1.SZA\AppData\Local\Temp\tmpicd2x_7_\src.win-amd64-3.11
Reading fortran codes...
Reading file 'module.f90' (format:free)
Post-processing...
Block: fmodule
Block: fmodule
Block: sub
Applying post-processing hooks...
character_backward_compatibility_hook
Post-processing (stage 2)...
Block: fmodule
Block: unknown_interface
Block: fmodule
Block: sub
Building modules...
Building module "fmodule"...
Constructing F90 module support for "fmodule"...
Constructing wrapper function "fmodule.sub"...
sub(a)
Wrote C/API module "fmodule" to file "C:\Users\TANUS~1.SZA\AppData\Local\Temp\tmpicd2x_7_\src.win-amd64-3.11\fmodulemodule.c"
Fortran 90 wrappers are saved to "C:\Users\TANUS~1.SZA\AppData\Local\Temp\tmpicd2x_7_\src.win-amd64-3.11\fmodule-f2pywrappers2.f90"
INFO: adding 'C:\Users\TANUS~1.SZA\AppData\Local\Temp\tmpicd2x_7_\src.win-amd64-3.11\fortranobject.c' to sources.
INFO: adding 'C:\Users\TANUS~1.SZA\AppData\Local\Temp\tmpicd2x_7_\src.win-amd64-3.11' to include_dirs.
copying C:\Users\tanus.szabo\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\numpy\f2py\src\fortranobject.c -> C:\Users\TANUS~1.SZA\AppData\Local\Temp\tmpicd2x_7_\src.win-amd64-3.11
copying C:\Users\tanus.szabo\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\numpy\f2py\src\fortranobject.h -> C:\Users\TANUS~1.SZA\AppData\Local\Temp\tmpicd2x_7_\src.win-amd64-3.11
INFO: adding 'C:\Users\TANUS~1.SZA\AppData\Local\Temp\tmpicd2x_7_\src.win-amd64-3.11\fmodule-f2pywrappers2.f90' to sources.
INFO: build_src: building npy-pkg config files
C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.2032.0_x64__qbz5n2kfra8p0\Lib\site-packages\setuptools\command\install.py:34: SetuptoolsDeprecationWarning: setup.py install is deprecated. Use build and pip and other standards-based tools.
warnings.warn(
running build_ext
INFO: No module named 'numpy.distutils._msvccompiler' in numpy.distutils; trying from distutils
INFO: customize MSVCCompiler
INFO: customize MSVCCompiler using build_ext
INFO: get_default_fcompiler: matching types: '['gnu', 'intelv', 'absoft', 'compaqv', 'intelev', 'gnu95', 'g95', 'intelvem', 'intelem', 'flang']'
INFO: customize GnuFCompiler
WARN: Could not locate executable g77
WARN: Could not locate executable f77
INFO: customize IntelVisualFCompiler
WARN: Could not locate executable ifort
WARN: Could not locate executable ifl
INFO: customize AbsoftFCompiler
WARN: Could not locate executable f90
INFO: customize CompaqVisualFCompiler
WARN: Could not locate executable DF
INFO: customize IntelItaniumVisualFCompiler
WARN: Could not locate executable efl
INFO: customize Gnu95FCompiler
INFO: Found executable C:\Program Files (x86)\gcc\bin\gfortran.exe
INFO: customize Gnu95FCompiler
INFO: customize Gnu95FCompiler using build_ext
INFO: building 'fmodule' extension
INFO: compiling C sources
creating C:\Users\TANUS~1.SZA\AppData\Local\Temp\tmpicd2x_7_\Release\Users
creating C:\Users\TANUS~1.SZA\AppData\Local\Temp\tmpicd2x_7_\Release\Users\TANUS~1.SZA
creating C:\Users\TANUS~1.SZA\AppData\Local\Temp\tmpicd2x_7_\Release\Users\TANUS~1.SZA\AppData
creating C:\Users\TANUS~1.SZA\AppData\Local\Temp\tmpicd2x_7_\Release\Users\TANUS~1.SZA\AppData\Local
creating C:\Users\TANUS~1.SZA\AppData\Local\Temp\tmpicd2x_7_\Release\Users\TANUS~1.SZA\AppData\Local\Temp
creating C:\Users\TANUS~1.SZA\AppData\Local\Temp\tmpicd2x_7_\Release\Users\TANUS~1.SZA\AppData\Local\Temp\tmpicd2x_7_
creating C:\Users\TANUS~1.SZA\AppData\Local\Temp\tmpicd2x_7_\Release\Users\TANUS~1.SZA\AppData\Local\Temp\tmpicd2x_7_\src.win-amd64-3.11
INFO: C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.38.33130\bin\HostX86\x64\cl.exe /c /nologo /O2 /W3 /GL /DNDEBUG /MD -DNPY_DISABLE_OPTIMIZATION=1 -IC:\Users\TANUS~1.SZA\AppData\Local\Temp\tmpicd2x_7_\src.win-amd64-3.11 -IC:\Users\tanus.szabo\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\numpy\core\include -IC:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.2032.0_x64__qbz5n2kfra8p0\include -IC:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.2032.0_x64__qbz5n2kfra8p0\Include -IC:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.38.33130\include -IC:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Auxiliary\VS\include -IC:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\ucrt -IC:\Program Files (x86)\Windows Kits\10\\include\10.0.22621.0\\um -IC:\Program Files (x86)\Windows Kits\10\\include\10.0.22621.0\\shared -IC:\Program Files (x86)\Windows Kits\10\\include\10.0.22621.0\\winrt -IC:\Program Files (x86)\Windows Kits\10\\include\10.0.22621.0\\cppwinrt /TcC:\Users\TANUS~1.SZA\AppData\Local\Temp\tmpicd2x_7_\src.win-amd64-3.11\fmodulemodule.c /FoC:\Users\TANUS~1.SZA\AppData\Local\Temp\tmpicd2x_7_\Release\Users\TANUS~1.SZA\AppData\Local\Temp\tmpicd2x_7_\src.win-amd64-3.11\fmodulemodule.obj
INFO: C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.38.33130\bin\HostX86\x64\cl.exe /c /nologo /O2 /W3 /GL /DNDEBUG /MD -DNPY_DISABLE_OPTIMIZATION=1 -IC:\Users\TANUS~1.SZA\AppData\Local\Temp\tmpicd2x_7_\src.win-amd64-3.11 -IC:\Users\tanus.szabo\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\numpy\core\include -IC:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.2032.0_x64__qbz5n2kfra8p0\include -IC:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.2032.0_x64__qbz5n2kfra8p0\Include -IC:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.38.33130\include -IC:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Auxiliary\VS\include -IC:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\ucrt -IC:\Program Files (x86)\Windows Kits\10\\include\10.0.22621.0\\um -IC:\Program Files (x86)\Windows Kits\10\\include\10.0.22621.0\\shared -IC:\Program Files (x86)\Windows Kits\10\\include\10.0.22621.0\\winrt -IC:\Program Files (x86)\Windows Kits\10\\include\10.0.22621.0\\cppwinrt /TcC:\Users\TANUS~1.SZA\AppData\Local\Temp\tmpicd2x_7_\src.win-amd64-3.11\fortranobject.c /FoC:\Users\TANUS~1.SZA\AppData\Local\Temp\tmpicd2x_7_\Release\Users\TANUS~1.SZA\AppData\Local\Temp\tmpicd2x_7_\src.win-amd64-3.11\fortranobject.obj
INFO: compiling Fortran 90 module sources
INFO: Fortran f77 compiler: C:\Program Files (x86)\gcc\bin\gfortran.exe -Wall -g -ffixed-form -fno-second-underscore -O3 -funroll-loops
Fortran f90 compiler: C:\Program Files (x86)\gcc\bin\gfortran.exe -Wall -g -fno-second-underscore -O3 -funroll-loops
Fortran fix compiler: C:\Program Files (x86)\gcc\bin\gfortran.exe -Wall -g -ffixed-form -fno-second-underscore -Wall -g -fno-second-underscore -O3 -funroll-loops
INFO: compile options: '-IC:\Users\TANUS~1.SZA\AppData\Local\Temp\tmpicd2x_7_\src.win-amd64-3.11 -IC:\Users\tanus.szabo\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\numpy\core\include -IC:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.2032.0_x64__qbz5n2kfra8p0\include -IC:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.2032.0_x64__qbz5n2kfra8p0\Include -c'
extra options: '-JC:\Users\TANUS~1.SZA\AppData\Local\Temp\tmpicd2x_7_\Release\ -IC:\Users\TANUS~1.SZA\AppData\Local\Temp\tmpicd2x_7_\Release\'
INFO: Program:f90: module.f90
f951.exe: Warning: Nonexistent include directory 'C:\Users\TANUS~1.SZA\AppData\Local\Temp\tmpicd2x_7_\Release\' [-Wmissing-include-dirs]
f951.exe: Warning: Nonexistent include directory 'C:\Users\TANUS~1.SZA\AppData\Local\Temp\tmpicd2x_7_\Release\' [-Wmissing-include-dirs]
INFO: compiling Fortran sources
INFO: Fortran f77 compiler: C:\Program Files (x86)\gcc\bin\gfortran.exe -Wall -g -ffixed-form -fno-second-underscore -O3 -funroll-loops
Fortran f90 compiler: C:\Program Files (x86)\gcc\bin\gfortran.exe -Wall -g -fno-second-underscore -O3 -funroll-loops
Fortran fix compiler: C:\Program Files (x86)\gcc\bin\gfortran.exe -Wall -g -ffixed-form -fno-second-underscore -Wall -g -fno-second-underscore -O3 -funroll-loops
INFO: compile options: '-IC:\Users\TANUS~1.SZA\AppData\Local\Temp\tmpicd2x_7_\src.win-amd64-3.11 -IC:\Users\tanus.szabo\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\numpy\core\include -IC:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.2032.0_x64__qbz5n2kfra8p0\include -IC:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.2032.0_x64__qbz5n2kfra8p0\Include -c'
extra options: '-JC:\Users\TANUS~1.SZA\AppData\Local\Temp\tmpicd2x_7_\Release\ -IC:\Users\TANUS~1.SZA\AppData\Local\Temp\tmpicd2x_7_\Release\'
INFO: Program:f90: C:\Users\TANUS~1.SZA\AppData\Local\Temp\tmpicd2x_7_\src.win-amd64-3.11\fmodule-f2pywrappers2.f90
f951.exe: Warning: Nonexistent include directory 'C:\Users\TANUS~1.SZA\AppData\Local\Temp\tmpicd2x_7_\Release\' [-Wmissing-include-dirs]
f951.exe: Warning: Nonexistent include directory 'C:\Users\TANUS~1.SZA\AppData\Local\Temp\tmpicd2x_7_\Release\' [-Wmissing-include-dirs]
C:\Users\TANUS~1.SZA\AppData\Local\Temp\tmpicd2x_7_\src.win-amd64-3.11\fmodule-f2pywrappers2.f90:7:11:
7 | use fmodule, only : sub
| 1
Fatal Error: Cannot open module file 'fmodule.mod' for reading at (1): No such file or directory
compilation terminated.
error: Command "C:\Program Files (x86)\gcc\bin\gfortran.exe -Wall -g -fno-second-underscore -O3 -funroll-loops -IC:\Users\TANUS~1.SZA\AppData\Local\Temp\tmpicd2x_7_\src.win-amd64-3.11 -IC:\Users\tanus.szabo\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\numpy\core\include -IC:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.2032.0_x64__qbz5n2kfra8p0\include -IC:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.2032.0_x64__qbz5n2kfra8p0\Include -c -c C:\Users\TANUS~1.SZA\AppData\Local\Temp\tmpicd2x_7_\src.win-amd64-3.11\fmodule-f2pywrappers2.f90 -o C:\Users\TANUS~1.SZA\AppData\Local\Temp\tmpicd2x_7_\Release\Users\TANUS~1.SZA\AppData\Local\Temp\tmpicd2x_7_\src.win-amd64-3.11\fmodule-f2pywrappers2.o -JC:\Users\TANUS~1.SZA\AppData\Local\Temp\tmpicd2x_7_\Release\ -IC:\Users\TANUS~1.SZA\AppData\Local\Temp\tmpicd2x_7_\Release\" failed with exit status 1
那么,为什么我会遇到这个问题?我需要重新安装一些东西吗?还是我做错了什么?
如果需要的话,我对重要事情的看法:
Python 3.11.7
Numpy: 1.26.2
f2py: 1.26.2
GNU Fortran (GCC): 13.2.0
Microsoft Windows: 10.0.19045.3693
@VladimirFГероямслава 注意到,f2py 正在更改我的用户名,所以我强制文件夹创建文件:
python -m numpy.f2py -c -m fmodule module.f90 --build-dir C:\Users\tanus.szabo\CODE\PySolidGUI\teste\temp
它创建了具有以下结构的文件夹:
C:\Users\tanus.szabo\CODE\PySolidGUI\teste\temp\
┣ Release\
┃ ┣ .libs\
┃ ┣ Users\
┃ ┃ ┗ tanus.szabo\
┃ ┃ ┗ CODE\
┃ ┃ ┃ ┗ PySolidGUI\
┃ ┣ fmodule.mod
┃ ┗ module.o
┗ src.win-amd64-3.11\
┣ fmodule-f2pywrappers2.f90
┣ fmodulemodule.c
┣ fortranobject.c
┗ fortranobject.h
但它仍然返回相同的错误:
INFO: Program:f90: C:\Users\tanus.szabo\CODE\PySolidGUI\teste\temp\src.win-amd64-3.11\fmodule-f2pywrappers2.f90
f951.exe: Warning: Nonexistent include directory 'C:\Users\tanus.szabo\CODE\PySolidGUI\teste\temp\Release\' [-Wmissing-include-dirs]
f951.exe: Warning: Nonexistent include directory 'C:\Users\tanus.szabo\CODE\PySolidGUI\teste\temp\Release\' [-Wmissing-include-dirs]
C:\Users\tanus.szabo\CODE\PySolidGUI\teste\temp\src.win-amd64-3.11\fmodule-f2pywrappers2.f90:7:11:
7 | use fmodule, only : sub
| 1
Fatal Error: Cannot open module file 'fmodule.mod' for reading at (1): No such file or directory
compilation terminated.
我明白了!
在问题“BUG:f2py没有正确包装函数”的这个答案之后,我添加了第三步来编译模块:
:: Create function signatures
f2py module.f90 -m fortranmodule -h module.pyf
:: Generate module
gfortran -c -O3 module.f90
:: Build extension module
f2py -c module.pyf module.f90 --fcompiler=gnu95
它不仅适用于我的帖子示例,而且适用于我的实际模块!