Jupyter notebook can't load a local f2py module

Hi there,

I am trying to recycle my old Fortran codes to be used with Python. I started with a very simple Fortran code and used f2py to generate the module to be loaded in Python.

On the command line, the following Python commands work just fine.

import sys
import numpy as np
sys.path.append(‘/Users/emmendes/old_fortran’)
import centered_difference

However when I try the same in a jupyter notebook, python complains with “Module not found”.

What am I missing?

That’s four lines of code. What is the specific one that gives that error.
And please when you share such an error, share the full traceback.

The last one, “import centered_difference”, gives the following error:


ModuleNotFoundError Traceback (most recent call last)
Cell In[4], line 1
----> 1 import centered_difference

ModuleNotFoundError: No module named ‘centered_difference’

If it works with Python on the command line vs. working in code called in a Jupyter Notebook then it is most likely that the environment where your notebook is running is different than where Python runs. Is there a file named centered_difference.py? Can your active Jupyter Notebook see it? The easiest way would be to set things up before you open Jupyyter. Put the file named centered_difference.py alongside the .ipynb file you are going to run and then open Jupyter and run the import statement. If you don’t get the module not found error then it can see the file.
There would be other ways to check this by looking into the PATH information.
We cannot tell you much more with the little information you have provided. You haven’t discussed code related to centered_difference here other than say it is triggering an error.

Many thanks. I will try to answer your questions as best as I can (though I am a newbie with Python).

  1. No, there is not a centered_difference.py file, but there is a centered_difference.xx.so file. I am trying to use my old Fortran code within Python.
  2. No, I don’t think the Jupyter Notebook can see it initially. I started another Jupyter session from Anaconda after I copied centered_difference.xx.so to the same location as the Jupyter Notebook. Now, import centered_difference works within the Jupyter Notebook.

The centered_difference.centered_difference call can now be made, but unfortunately, it returns an error.


error Traceback (most recent call last)
Cell In[5], line 12
9 dx = np.zeros((n, m), dtype=np.float64)
11 # Call the Fortran subroutine
—> 12 centered_difference.centered_difference(n, m, x, dx)
14 # Print the result
15 print(dx)

error: (shape(x, 1) == m) failed for 2nd keyword m: centered_difference:m=0

although x.shape returns (100, 3) , I would appreciate it if you could point me to an f2py forum where I can get help with this issue or is this forum the right one to get it?

I wonder why the command sys.path.append(‘/Users/emmendes/old_fortran’) won’t do the job in this case.

Many thanks.

That’s progress. And so that was the original issue and why Python was giving you a different result than running in a Jupyter Notebook .ipynb file. Jupyter handles environments independent of the system Python, and so switching between the two different contexts isn’t just plug-and-play always.

To answer that we’d have to do more investigating than you’ve provided the ability to pursue. I would suggest tentatively maybe I already answered the question above. The system on your machine and system python are different things than what Jupyter is involved with. You need to provide that path to your running kernel. I think though you have other issues so following up on that may not be your priority right now.

This would seem like a better fit for a python forum than here. However, you did say all this code ran just fine with Python, right? That would argue it belongs here. But maybe you just meant the import worked?

I will say though that as posted not many could provide you with much more guidance, unless that is a standard function in fortran that those in the know would inherently be familiar with. You have not provided much in the sense of code. (Personally, I have a feeling if you reverse engineered what arguments worked in calls to centered_difference.centered_difference(), you may be able to troubleshoot this yourself.)
So in line with you not providing much in the terms of code that those trying to help you could go on… I’d urge you to please read Getting good answers to your questions. It will help you in other forums as well if you realize this forum isn’t pertinent.

1 Like

Many thanks.

Sorry for my late reply. Somehow, my tardiness turned out to be a good thing.

Starting a brand new Jupyter session seemed to resolve the issue when calling centered_difference. Additionally, explicitly adding dtype=np.float32 allowed me to run the entire notebook without any problems.

(Please note that I am using real(kind=QR_K) in Fortran, with integer, parameter :: QR_K = real64 but I had to add dtype=np.float32 in python to get the fortran function to work).

import sys
#import os
import numpy as np
#import ctypes
#from numpy.ctypeslib import ndpointer
# Add the directory containing the compiled module to the Python path
#sys.path.append(os.path.abspath("/Users/eduardo/Dropbox/Fortran/example_rk45/How_iterate_dynamical_systems/macM1/Davide"))
sys.path.append('~/Dropbox/Fortran/example_rk45/How_iterate_dynamical_systems/macM1/Davide')
display(sys.path)
import centered_difference
# Define input parameters
n = 100  # Number of rows
m = 3    # Number of columns

# Create an example input array
x = np.random.rand(n, m).astype(np.float32)

# Prepare an output array
#dx = np.zeros((n, m), dtype=np.float64, order='F')
dx = np.zeros((n, m), dtype=np.float32, order='F')

# Call the Fortran subroutine
centered_difference.centered_difference(x, dx, n , m)

# Print the result
print(dx)
1 Like

Thanks for updating with the further details of what worked so that it may help others or the future you.