f2py - Cannot open module file: No such file or directory

134 views Asked by At

I'm trying to create a Fortran module to use in Python, but it is not working on Windows because Cannot open module file 'fmodule.mod' for reading at (1): No such file or directory.

I have this script module.f90:

module fmodule
contains
   subroutine sub(a)
      real a(3)
      a(1) = 1.0
   end subroutine sub
end module fmodule

and trying to run f2py directly

python -m numpy.f2py -c -m module module.f90

or with a signature file

python -m numpy.f2py module.f90 -m module -h module.pyf
python -m numpy.f2py -c module.pyf module.f90 

it returns:

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

So, why I have this problem? Need I to reinstall something? Or am I doing something wrong?

My version of the important thing, if needed:

Python 3.11.7
Numpy: 1.26.2
f2py: 1.26.2
GNU Fortran (GCC): 13.2.0
Microsoft Windows: 10.0.19045.3693

Edit 1:

As @VladimirFГероямслава notice, f2py was changing my user name, so I forced the folder to create the files:

python -m numpy.f2py -c -m fmodule module.f90 --build-dir C:\Users\tanus.szabo\CODE\PySolidGUI\teste\temp

It created the folder with this structure:

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

But it still returned the same error:

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.
1

There are 1 answers

0
Tanus Szabo On

I got it!

Following this answer of the issue "BUG: f2py does not properly wrap functions", I added a third step to compile the module:

:: 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

It worked, not only for the example of my post, but also on my actual module!