Stream: jupyterlab-hub

Topic: import f2py generated module in notebook?


view this post on Zulip Frank Bryan (May 22 2020 at 19:03):

I am trying to merge some old analysis code in f90 into a workflow. I have it working in a python script invoked from the command line, but cannot get it to work in a notebook. Looking for guidance on how to inform jupyter where to find the resulting shared object file.

Here is a very simplified example exhibiting the same problematic behavior:

The fortran code:
function hello(n) result(err)
integer :: err
integer, intent(in) :: n
err = 0
if ( n > 0 ) then
do i=1,n
print *,'hello',i
enddo
err = 1
endif
end function hello

I compile with f2py:
python -m numpy.f2py -c fhello.f90 -m fhello
[a bunch of warning messages result, but no fatal errors, following file produced:]
fhello.cpython-37m-x86_64-linux-gnu.so

works correctly from the command line:
(analysis) bryan@casper21: python -c "import fhello ; fhello.hello(2)"
hello 1
hello 2

In a Notebook with source in the same directory:
import fhello

produces the following error:


ImportError Traceback (most recent call last)
<ipython-input-1-5173bb57af75> in <module>
----> 1 import fhello

ImportError: libifport.so.5: cannot open shared object file: No such file or directory

view this post on Zulip Anderson Banihirwe (May 22 2020 at 19:08):

@Frank Bryan, what is the output of

view this post on Zulip Anderson Banihirwe (May 22 2020 at 19:09):

It's likely that Conda packages and system libraries are colliding

view this post on Zulip Matt Long (May 22 2020 at 19:10):

@Frank Bryan, you may need to do

module purge
conda activate my-env

before you compile.

view this post on Zulip Frank Bryan (May 22 2020 at 19:10):

analysis) bryan@casper21: echo $LD_LIBRARY_PATH
/glade/u/apps/dav/opt/openmpi/3.1.4/intel/18.0.5/lib:/glade/u/apps/opt/intel/2018u4/compilers_and_libraries/linux/lib/intel64:/glade/u/apps/dav/opt/usr/lib64
(analysis) bryan@casper21: module list

Currently Loaded Modules:
1) intel/18.0.5 3) openmpi/3.1.4 5) ncarenv/1.3 7) ncview/2.1.7 9) idl/8.7.2
2) ncarcompilers/0.5.0 4) netcdf/4.7.3 6) nco/4.7.9 8) ncl/6.6.2

view this post on Zulip Anderson Banihirwe (May 22 2020 at 19:10):

Frank Bryan, you may need to do

module purge
conda activate my-env

before you compile.

Try this :point_up:

view this post on Zulip Matt Long (May 22 2020 at 19:11):

See here: https://github.com/matt-long/remap_z_to_sigma/tree/master
for an example of where I do something like this.

view this post on Zulip Matt Long (May 22 2020 at 19:12):

though it looks like I neglected appropriate environment conditioning in my build script:
https://github.com/matt-long/remap_z_to_sigma/blob/master/remap_z/build.sh

view this post on Zulip Frank Bryan (May 22 2020 at 19:15):

Worked (mostly). Thanks @Anderson Banihirwe .

Not an issue for my real problem, but where does output to stdout from the f90 code go ? Doesn't show up in the output cell but the return code is correct so I think it worked.

view this post on Zulip Alea Kootz (Oct 01 2020 at 23:46):

Frank, prior to compiling the .f90 code with f2py, did you use f2py to generate a .pyf header file, then update the header file with intent(<in/out/inout/hide>) for each variable?

view this post on Zulip Frank Bryan (Oct 02 2020 at 01:27):

That was a long time ago and I ended up moving to a pure python implementation, so I'm not remembering well, but it looks like no, I just compile the Fortran source with
python -m numpy.f2py -c land_fillin.f90 -m land_fillin

No pyf file was required. My F90 subroutine already had all arguments declared with intent


Last updated: May 16 2025 at 17:14 UTC