'f2py does not properly import, despite successfully compiling

I am successfully compiling my Fortran subroutine with f2py. However, a folder is created as well as a pyd file.

If I do not put the created pyd file in the generated folder I get this error

ImportError: DLL load failed: The specified module could not be found.

If I put the generated .pyd file in the generated folder, my python script will run, and the import seems to work. However, if I run print(energy_f2py.__doc__) None is the output, and when I try to use the subroutine I get an error AttributeError: module 'energy_f2py' has no attribute 'potential_3'

potential_3 is the name of the subroutine inside energy_f2py.f90

I have also tried making energy_f2py.f90 a module containing the subroutine, with no difference in results.

This is the pyd file that is generated energy_f2py.cp36-win_amd64.pyd. I am using the commandline in Pycharm (windows 10 64 bit) to compile the Fortran file using

  1. f2py -c -m energy_f2py energy_f2py.f90

  2. python -m numpy.f2py -c -m energy_f2py energy_f2py.f90

The same thing happens.

It seems weird to me that both the pyd file is created as well as a folder, see the below image enter image description here

I am stuck at this point... it seems like it is 95% of the way to working...

Lastly, this other post had a solution saying to name the Python and Fortran modules different names. I have tried this, and it did not make a difference either.



Solution 1:[1]

The best solution I found which worked was to partition my hardrive and install Linux. I chose to use Lubuntu, but you could use anything.

Everything works as it should on Linux.

Solution 2:[2]

Anther option to is to make it a static build, with the -static flag. This builds everything needed into a single file:

python -m numpy.f2py -c -m energy_f2py energy_f2py.f90 -static

You should then be able to use that .pyd file no matter what folder you've placed it in.

Solution 3:[3]

If you are using windows, I think adding --compiler=mingw32 may help. The defualt compiler is msvc on windows.

Solution 4:[4]

It occurs to me that the resulting .pyd module is looking for the DLL library in the same directory it itself resides in. Therefore, try moving the DLL from .libs directory to energy_f2py - it worked for me on Windows 10.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Charlie Crown
Solution 2 Status
Solution 3 Junda Feng
Solution 4 sup3r_martin