I would like to import an Fortran module into python with f2py
.
I successfully compiled the module with the command f2py -c primes.f90 -m primes
.
This command generates primes.cp39-win_amd64.pyd
and an directory:
primes --.lib ----libprimes.E32HNUCUWH2OOA5WTEOFP7BSXKY3WIRC.gfortran-win_amd64.dll
When I am trying to import the module as described here with import primes
it imports something but not the module. So e.g I cannot see the docstring or access the functions.
I suspect that Python tries to import the folder ‘primes’ and not the module itself.
How can I fix this?
Thanks.
EDIT:
I have used the example from here to demonstrate the problem:
My Python code to showcase the problem:
import primes print(primes) print(primes.__doc__) # should return "This module 'primes' is auto-generated with f2py (version:2). Functions: [...]" print(type(primes)) # as @VladimirF mentioned should return fortran print(primes.logical_to_integer.__doc__) # should return "prime_numbers = logical_to_integer(is_prime,num_primes,[n]) Wrapper for ``logical_to_integer``. [...]"
Actual return:
Traceback (most recent call last): File "xxx", line 6, in <module> print(primes.logical_to_integer.__doc__) AttributeError: module 'primes' has no attribute 'logical_to_integer' <module 'primes' (namespace)> None <class 'module'>
Edit 1
I have followed up the suggestion of @roygvib. I have tried several versions of python and conda and part of the problem seems the be that I was using the systems interpreter for compiling the module and the conda for running the python code.
For all the other combinations the following exception is raised:
ImportError: DLL load failed while importing primes: Das angegebene Modul wurde nicht gefunden.
can be translated to:
ImportError: DLL load failed while importing primes: The given Module was not found.
So the actual error was that no error was raised in the given combination.
Advertisement
Answer
The solution from @zlamere from this github issue worked for me.
instead of f2py -c primes.f90 -m primes
I used python -m numpy.f2py -c --fcompiler=gnu95 --compiler=mingw32 primes.f90 -m primes
I don’t know why this command works and the other one doesn’t.