3

I have a Fortran 95 code that I want to compile to a Python library using f2py. In a matter of fact I've already done it, and it works beautifully. Does the resulting .pyd (.so) depend on numpy after compilation? Could it be used without numpy installation and are they some other options to embed the needed parts inside the final library so it has no dependencies?

I am considering this to be a library accompanying a commercial product and I want the end user to have as little as possible to install on his system, so suggesting to my future customers to install numpy does not suit me. I've searched extensively for an answer, but I cannot seem to find one.

In case it is not possible, could you please refer me to a dependence free way to wrap Fortran code using Python.

  • At the web site [www.fortran90.org](http://www.fortran90.org/index.html), check out the section [Interfacing with C](http://www.fortran90.org/src/best-practices.html#interfacing-with-c), and the subsequent section [Interfacing with Python](http://www.fortran90.org/src/best-practices.html#interfacing-with-python). – Warren Weckesser Mar 18 '18 at 22:01

2 Answers2

0

I'm afraid there is no way of doing this without numpy, as f2py internally produces numpy dependencies. Does your code need to be FORTRAN? If it could be C/C++ there might be a chance to get around dependencies, check out chapter 7 of this book for more insights on that.

rammelmueller
  • 1,092
  • 1
  • 14
  • 29
  • One can bind Fortran (not FORTRAN sonce 1990) the very same way as C and C++. No need to to rewrite to C++. – Vladimir F Героям слава Mar 18 '18 at 19:27
  • Thank you for your answer! I was afraid that the answer would be similar. I presume it could be redone using C/C++, but will be a moderate pain. There is a lot of matrix math involved, that could be easily redone using numpy, but the reason that I've headed towards FORTRAN was to avoid numpy in first place... I presume compiling with f2py towards numeric or numarray would create dependace for those two modules... – Vassil Kateliev Mar 18 '18 at 19:32
  • Thnak you @VladimirF. Yes to be precise it is Fortran 95 code. Maybe I should have mentioned that earlier. – Vassil Kateliev Mar 18 '18 at 19:33
  • @VassilKateliev I repeat, you can bind Fortran to Python the same way you bind C or C++. There is no need to rewrite your Fortran code. The questiin is whether you need NumPy in your Python code and you may or may not needed for C++ and for Fortran equally once you use the same methonds to call them. – Vladimir F Героям слава Mar 18 '18 at 23:05
0

On the website fortran90.org, there are sections dedicated to the Fortran C interface. While it is of course possible to use NumPy, plain C interfacing using iso_c_binding is certainly possible. And it is part of the standard!

See Interfacing with C and the following sections. The link refers to calling C from Fortran but is directly relevant nonetheless. The next section uses Cython for calling Fortran from Python "à la C" but uses NumPy. The part using ctypes uses no NumPy.

Note: you need a Fortran 2003 compiler. Fortran 95 code is of course legal Fortran 2003 so you can just add the wrapper/interface part to your code.

Pierre de Buyl
  • 7,074
  • 2
  • 16
  • 22
  • Thank you! Yes I am aware of fortran90.org guide, but all the examples given use numpy array. As Python does not have a "native" array implementation, I presume that is one way to provide Fortran code with "adequate storage" for its data, but numpy does not suit me. I'll try both Cython and ctypes, hoping that at least Cython's memory view array would do the job... I am open for any suggestions, concerning how (and witch types) can I pass arrays to the Fortran library and back. – Vassil Kateliev Mar 19 '18 at 08:47
  • Python has the [memoryview](https://docs.python.org/3/library/stdtypes.html#memoryview) datatype. It is related to [PEP 3118](https://www.python.org/dev/peps/pep-3118/) and the purpose is to make it possible to bind to array-dependent code without NumPy. For C/Fortran extensions, you must do some work (you must manage the data buffer struct yourself) but the possibility is real :-) – Pierre de Buyl Mar 19 '18 at 09:06