No module or function named error in Python custom function with MATLAB

43 views (last 30 days)
Hello
I wish to run Python function from MATLAB command window. I have a set of function in the folder.
To make a seemless integration between the two, I did the following:
pyenv
ans =
PythonEnvironment with properties:
Version: "3.12"
Executable: "C:\Users\poulo\AppData\Local\Programs\Python\Python312\python.exe"
Library: "C:\Users\poulo\AppData\Local\Programs\Python\Python312\python312.dll"
Home: "C:\Users\poulo\AppData\Local\Programs\Python\Python312"
Status: Loaded
ExecutionMode: InProcess
ProcessID: "50204"
ProcessName: "MATLAB"
The output appeared as below:
Next, I added the Python Path using the below command
moduleDirPath = 'C:\pganguli\Pyscripts\atmos-main\atmos-main\atmos\'
insert(py.sys.path, int64(0), moduleDirPath)
It added my package 'atmos' to CPython directory. Now, while checking the dir, it showed all the functions
. __pycache__ moisture.py thermo.py
.. constant.py parcel.py utils.py
__init__.py kinematic.py pseudoadiabat.py
Now, I tried to call my function and I receive No module/function error. The python within MATLAB is working fine, Checked with simple calculation, which is working fine
py.numpy.sqrt([2.0 3.0 4.0])
ans =
Python ndarray:
1.4142 1.7321 2.0000
Kindly let me know where I am missing
P = py.numpy.array(101300); T = py.numpy.array(21.3)
q = py.moisture.specific_humidity_from_dewpoint_temperature(P,T) % specific_humidity_from_dewpoint_temperature is the function available in moisture.py module, which require two variable P & T
No module or function named 'moisture'.

Accepted Answer

Umar
Umar on 28 Aug 2025 at 4:49

Hi @Poulomi,

I reviewed your Python-MATLAB integration issue and can help you resolve the "No module or function named 'moisture'" error. Let me explain the root cause of error, you have successfully added the atmos folder to Python's path, but you're calling py.moisture.function_name() directly. Since moisture.py is a submodule within the atmos package, Python requires you to reference it through the complete package hierarchy. So, I will suggest following solutions in order of preference

Option 1: Use Full Package Path (Recommended) q = py.atmos.moisture.specific_humidity_from_dewpoint_temperature(P,T)

Option 2: Explicit Module Import py.importlib.import_module('atmos.moisture') q = py.atmos.moisture.specific_humidity_from_dewpoint_temperature(P,T)

Option 3: Fresh MATLAB Session If the above options fail, restart MATLAB to refresh the Python environment: 1. Close MATLAB completely 2. Restart MATLAB 3. Re-execute your setup:moduleDirPath = 'C:\pganguli\Pyscripts\atmos-main\atmos-main\atmos\'insert(py.sys.path, int64(0), moduleDirPath) 4. Try Option 1 again

When you add a package directory to sys.path, Python treats it as a namespace package. Modules within that package must be accessed using dot notation: package.module.function rather than just module.function. So, as quick verification, confirm your package is properly loaded:

py.list(py.sys.path) % Verify your path appears in the list

I will suggest try Option 1 first, it should resolve your issue immediately.

References:

Let me know if you need any clarification!

  6 Comments
Poulomi
Poulomi about 2 hours ago
Yes, I got it now, my calculated q value. But when I ran
available_functions = py.dir(py.atmos.moisture)
The output shows
TypeError: 'moisture' object is not callable.
But I could get the q value after fixing the parent directory and running the script
q = py.atmos.moisture.specific_humidity_from_dewpoint_temperature(P, T)
Thanks a bunch for this help!
Umar
Umar about 4 hours ago

Hi @Poulomi,

I am very pleased to hear that you were able to compute the specific humidity successfully after correcting the parent directory path. This confirms that MATLAB and Python are now interfacing properly, and that the package hierarchy is being resolved as intended.

With regard to the TypeError you encountered when executing

   available_functions = py.dir(py.atmos.moisture);

this arises because py.atmos.moisture is treated as a module object, whereas dir should be applied to an explicit Python object reference. A more robust approach is to import the module first and then query its attributes, for example:

moisture_module = py.importlib.import_module('atmos.moisture');
available_functions = py.dir(moisture_module);
disp(available_functions);

This will return the functions, classes, and variables defined within the module. That said, since your primary objective of obtaining the humidity calculation is functioning correctly, this step is primarily diagnostic and not essential for your workflow.

In summary: * The path configuration and import mechanism are now correctly established. * Function calls within the atmos.moisture module execute as expected. * Introspection of available functions can be performed via explicit import followed by dir.

You should now be well-positioned to extend this workflow to other modules within the atmos package.

Sign in to comment.

More Answers (0)

Products


Release

R2024b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!