Python 3.8 Compatibility Issue w/ 2023a

20 views (last 30 days)
Nick Donatelli
Nick Donatelli on 21 Dec 2023
Edited: Giacomo Viganò on 26 Jun 2024
I'm having an issue that seems to do with compatibility of python 3.8 with 2023a.
Currently when I try to convert a 2-D array into a python function I am met with the ofllowing error:
Python Error: ModuleNotFoundError: No module named 'libmwbuffer'
Python function '' might not be able to accept at least one input argument at position 1. The function
may require a specific data type that you can construct from the MATLAB array. For more information, see
the documentation for Python function '' and working with Python arrays.
I have found a similar issue catalogued in this question from 2021: https://www.mathworks.com/matlabcentral/answers/856060-matlab-not-converting-2d-arrays-to-python-arrays-no-module-named-libmwbuffer-error?s_tid=srchtitle in which this person was having the exact same problem.
The following link was provided in an answer which highlights compatibility, and clearly shows that now (2023) the 2023a distribution of Matlab should support python 3.8: https://www.mathworks.com/support/requirements/python-compatibility.html
For clarity, the rest of the python functionality I am looking for is working; I can even pass a 1xN array in matlab to python and get an array
Example 1 (1xN):
input: np = py.importlib.import_module('numpy');
np.array(ones(1, 5))
output:
ans =
Python ndarray with properties:
T: [1×1 py.numpy.ndarray]
base: [1×1 py.NoneType]
ctypes: [1×1 py.numpy.core._internal._ctypes]
data: [1×5 py.memoryview]
dtype: [1×1 py.numpy.dtype[float64]]
flags: [1×1 py.numpy.core.multiarray.flagsobj]
flat: [1×1 py.numpy.flatiter]
imag: [1×1 py.numpy.ndarray]
itemsize: [1×1 py.int]
nbytes: [1×1 py.int]
ndim: [1×1 py.int]
real: [1×1 py.numpy.ndarray]
shape: [1×1 py.tuple]
size: [1×1 py.int]
strides: [1×1 py.tuple]
[1. 1. 1. 1. 1.]
Example 2 (M x N):
input: np = py.importlib.import_module('numpy');
np.array(ones(2, 5))
output: Python Error: ModuleNotFoundError: No module named 'libmwbuffer'
Python function '' might not be able to accept at least one input argument at position 1. The function
may require a specific data type that you can construct from the MATLAB array. For more information, see
the documentation for Python function '' and working with Python arrays.
*Note: I have also tried directly using py.numpy.array(ones(2, 5)), this also gives me the same results above
  3 Comments
Nick Donatelli
Nick Donatelli on 11 Apr 2024
@Jeremy Ward I did not get this resolved, I just started getting mathworks emails not in spam...
There were two options I was able to leverage in the mean-time:
1) Use something like this code, which modifies the data as a python type (so if you have a 1xN vector you can convert it to a "1xN" matrix
if any(size(data) == 1)
data = py.numpy.reshape(data, py.tuple([int64(-1), int64(1)]));
else
data = data;
end
You can do the same by making the datatype a 1xMN vector, then reshaping to the dimensions you need and see if that works
2) Use another version of Python/Matlab if you're able to, I recall Python 3.7 working the way you would expect it to in MATLAB 2022a
Jeremy Ward
Jeremy Ward on 18 Apr 2024
@Nick Donatelli Hey thanks for the information! Without the ability to pass arrays this interface is not super useful. I hope they fix it soon.

Sign in to comment.

Answers (2)

Giacomo Viganò
Giacomo Viganò on 26 Jun 2024
Edited: Giacomo Viganò on 26 Jun 2024
Had the same exactly issue. Restarting matlab solved it. I don't know why.

Karanjot
Karanjot on 4 Jan 2024
Hi Nick,
I understand that you are encountering the following while using python functions within MATLAB:
Python Error: ModuleNotFoundError: No module named 'libmwbuffer'
Python function '' might not be able to accept at least one input argument at position 1. The function
may require a specific data type that you can construct from the MATLAB array. For more information, see
the documentation for Python function '' and working with Python arrays.
I recommend using the 'pyenv' function in MATLAB to check which Python environment is being used. Ensure that it's the correct one where 'numpy' is installed. If it's not the correct environment or if you want to change it, you can set it using:
pyenv(Version="executable")
where executable is the full path to the Python executable file.
Aditionally, If numpy is not installed in the Python environment that MATLAB is using, you will need to install it. You can do this outside of MATLAB by activating the Python environment and using pip:
pip install numpy
If you still experience the error after trying these troubleshooting steps, I recommend contacting MathWorks Technical Support: http://www.mathworks.com/support/contact_us/index.html
I hope this helps!
  1 Comment
Nick Donatelli
Nick Donatelli on 11 Apr 2024
Thank you for attempting to answer this question, I'll have to follow up with Technical Support; these were all things I've checked through already :^). I even tried changing the version of Python being used but didn't get a different result.

Sign in to comment.

Tags

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!