1

I am trying to use F2py but am getting some error messages or warnings on compilation involving `deprecated numpy'. I'm unsure how to fix this. I'm using python 2.7.17 and fortran90.

I compile by writing
f2py -c f2py_F90.f90 -m fortran

Note, compiling with : f2py -c f2py_F90.f90 -m fortran \
doesn't fix the problem either.

Below are the basic fortran and python codes that indicate the problem I'm having. This example is minimal, complete and verifiable. How can I fix this problem and succesfully have python execute the fortran module I'm passing into it?

The message I get is

warning "Using deprecated NumPy API"

The expected output should give a = [2,1,3] but instead I get the warning described above.

!fortran code
module fmodule
   implicit none
contains
   subroutine fast_reverse(a,n)

   integer, intent(in) :: n
   real, intent(inout) :: a(:)

   a(1:n) = a(n:1:-1)

   end subroutine fast_reverse
end module fmodule

#python code
import fortran
import numpy as np

a = np.array([1,2,3],np.float32)

fortran.fmodule.fast_reverse(a,2)

a #the output should give a = [2,1,3] 
Jeff Faraci
  • 403
  • 13
  • 28
  • 2
    If you're `getting some error messages or warnings`, you need to put them here. – Random Davis Sep 08 '21 at 19:15
  • @RandomDavis I added the error message explicitly I obtain from the terminal, thanks. I also already stated this error message in the original post. Please read it. – Jeff Faraci Sep 08 '21 at 19:19
  • 2
    Testing something: build with `python -m numpy.f2py -c fmodule.f90 -m fortran` so that you know that the same python interpreter is used for both commands. Diagnosis: `which -a python` and `which -a f2py`. Also, any reason for using Python 2? – Pierre de Buyl Sep 09 '21 at 07:21
  • @PierredeBuyl Thanks a lot for your help. I tried building with the command you suggested, I still get the same warning message. I don't understand your sentence starting with Diagnosis: ... Sorry, what do you mean by `which -a python' and `which -a f2py'? So my job only has python 2, unfortunately, I'm not allowed to upgrade it myelf to python 3.... I have suggested they upgrade it asap. (Sorry about that) – Jeff Faraci Sep 10 '21 at 04:00
  • @PierredeBuyl I tried typing your diagonisis into my command line. When I type `which -a python` , it retuns /usr/bin/python, when I type `which -a f2py`, command line returns /usr/bin/f2py – Jeff Faraci Sep 10 '21 at 05:35
  • @PierredeBuyl do you have any other suggestions? Thanks a lot for your help – Jeff Faraci Sep 10 '21 at 20:17
  • @JeffFaraci As a sanity check of your setup, are you able to run the f2py demo here: https://numpy.org/doc/stable/f2py/f2py.getting-started.html – ZaydH Sep 11 '21 at 19:03
  • @ZaydH I just tried it, that example gives me the same exact error/warning message as I have listed above. The goal of this bounty is to figure out what that problem is and how to fix it. Thanks a lot for your help! – Jeff Faraci Sep 11 '21 at 20:08
  • 1
    Hi Jeff, the commands were intented to see whether you had several, possibly conflicting, installs of Python or of some of the packages. Any chance you would have installed packages with pip? – Pierre de Buyl Sep 12 '21 at 05:49
  • @PierredeBuyl Unfortunately, all the installation is not done on my end. I'm not really allowed to do any of that... (Hence why it's still version python 2...) But, perhaps, yes the packages were installed with pip. Thanks again for your help. – Jeff Faraci Sep 12 '21 at 06:03
  • 1
    @JeffFaraci I agree what I proposed would not have fixed your setup. If your setup does not work on the `f2py` demo (which I verified worked on my machine), it feels like you have a setup issue. If the installation is not done on your end, could you try running the demo in a virtual machine? Once you have a setup you know can work, it will make your debugging of your code much easier. – ZaydH Sep 12 '21 at 11:33
  • Other diagnostics: `python -c 'import numpy; print(numpy.__file__)'` and `echo $PYTHONPATH` – Pierre de Buyl Sep 13 '21 at 07:56

1 Answers1

2
  1. Ignore #warning "Using deprecated NumPy API, disable it with ..., see Cython Numpy warning about NPY_NO_DEPRECATED_API when using MemoryView and Cython docs;
  2. Change last line a #the output should give a = [2,1,3] to print(a) #the output should give a = [2,1,3] for printing a to stdout.

For example:

$ python2.7 -m numpy.f2py -c f2py_F90.f90 -m fortran
running build
...
      /opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/core/include/numpy/npy_1_7_deprecated_api.h :it17 :with2 :"          "#define 
  NPY_NO_DEPRECATED_APIwarning : NPY_1_7_API_VERSION" [-W#warnings]

  "Using deprecated NumPy API, disable it with "          "#define
  NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION" [-W#warnings]
#warning "Using deprecated NumPy API, disable it with " \
...

$ cat test_f2py_F90.py 
#python code
import fortran
import numpy as np

a = np.array([1,2,3],np.float32)

fortran.fmodule.fast_reverse(a,2)

print(a) #the output should give a = [2,1,3]



$ python2.7 test_f2py_F90.py 
[2. 1. 3.]
Serge3leo
  • 411
  • 2
  • 4
  • Hi, thanks a lot for your answer. Just so I'm clear, I first changed `a` to `print(a)` in the python code. Then I use `python2.7 test_f2py_F90.py` to compile in command line, right? I tried this, and it gives the correct output...I just want to be sure that this is indeed the solution, right? Thank you so much – Jeff Faraci Sep 13 '21 at 21:04
  • @JeffFaraci, "I tried this, and it gives the correct output..." - congratulations, we did it?! The key fix is `print(a)` (my common mistake when converting jupyter-notebook or interactive sessions into python scripts). In your current environment, `python2.7` is probably equivalent to `python` (in my example, it was used because in my environment `python` is associated with`python3.9`). – Serge3leo Sep 14 '21 at 16:13
  • Thanks for your great answer. You made it seem trivial...Indeed, I tried just `python` instead of `python2.7` and it works just as well! Excellent. Thanks again – Jeff Faraci Sep 14 '21 at 16:19
  • @JeffFaraci, "...python2.7 test_f2py_F90.py to compile in command line, right?" - No, these are inaccurate words. `python2. 7 -m numpy.f2py -c f2py_F90.f90 -m fortran` - compiles the fortran code from the file `f2py_F90.f90` to `fortran.so` or `fortran.cpython-38-darwin.so` or something else, depending on your environment. `python2.7 test_f2py_F90.py` - interprets python code from a file `test_f2py_F90.py`, and, during interpretation, the `import fortran` operator loads the compiled `fortran.so`. – Serge3leo Sep 14 '21 at 16:30
  • @JeffFaraci, P.S. You can also use jupyter-notebook from this directory, or with the command `python2.7` start an interactive session and call all the necessary python statements manually. – Serge3leo Sep 14 '21 at 16:32