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] 
12
  • 2
    If you're getting some error messages or warnings, you need to put them here. Commented Sep 8, 2021 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. Commented Sep 8, 2021 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? Commented Sep 9, 2021 at 7:21
  • 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? Commented Sep 12, 2021 at 5:49
  • 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. Commented Sep 12, 2021 at 11:33

1 Answer 1

2
+100
  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.]
Sign up to request clarification or add additional context in comments.

5 Comments

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
@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 withpython3.9).
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
@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.
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.