Skip to content
Advertisement

fortran subroutine import in python fails

I am trying to import a fortran subroutine into python code. Following this guide https://notmatthancock.github.io/2017/02/10/calling-fortran-from-python.html I have added the following line into my fortran code:

!f2py intent(in) :: ear,ne,parames,ifl
!f2py intent(out) photar,photer

However when I try to use f2py -c fireball_ES_param.f -m fireball to create fireball.so to import in my python code I get several hundreds of warnings, which might be the problem but I am not sure: https://www.4shared.com/s/f2ynHZ_Wjda (it’s too long to be posted here).

In any case, the fireball.cpython-34m.so file gets created, but when I try to import that from python I get:

$ python

Python 2.7.6 (default, Nov 13 2018, 12:45:42)  [GCC 4.8.4] on
linux2 Type "help", "copyright", "credits" or "license" for more
information.
>>> import fireball Traceback (most recent call last):
File "<stdin>", line 1, in <module> ImportError:
  No module named fireball

I tried:

import sys

sys.path.append('/path/to/folder/containing/fireball.cpython-34m.so')

but got the same result. I am not sure if there is a problem with the creation of fireball.so, or with its import.

Advertisement

Answer

f2py links to Python 3.4 (hence the 34 in fireball.cpython-34m.so) but python3 is actually version 3.6 (see you last comment).

Your options:

  • Start your program with Python 3.4 which should be available as python3.4
  • Update f2py to Python 3.6. If you installed via pip, pip install --user -U numpy should do. Then rebuild the extension.
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement