How do get all of my python libraries in matlab?

56 次查看(过去 30 天)
I wrote a python script that will open a .BIN file, I wrote it in python because the file is really complicated. Some data within the file are 16 bit, some are 8 bit, and some is 32 bit, either way it was easier to write in python. Now im trying to open this file in matlab as part of an app that im working on. The original data are these .BIN files, I want to convert them to CSV (which the python code does) and then open those CSVs in my matlab code for processing. The python code works great, and my matlab code works great if I just use the output of the python file but when I try to run the python code within the matlab code I get all sorts of errors.
First I ran pyenv to make sure im running from the right directory and with the right version of python, here is the result:
ans =
PythonEnvironment with properties:
Version: "3.12"
Executable: "D:\pythonw.exe"
Library: "D:\python312.dll"
Home: "D:"
Status: Loaded
ExecutionMode: InProcess
ProcessID: "33048"
ProcessName: "MATLAB"
Idk if thats what I need but it looks similar to what I see in other tutorials. Afterwords I tried pyrunfile('mypythonfile.py') and got this:
Error using <string>><module> (line 4)
Python Error: ModuleNotFoundError: No module named 'pandas'
I then found the pandas module in my python library and added to my path. I got another error when running agian for a different library, I kept adding to path until I eventually got to this:
Error using __init__><module> (line 32)
Python Error: ImportError: Unable to import required dependencies:
numpy: No module named 'numpy'
pytz: No module named 'pytz'
dateutil: No module named 'dateutil'
I tried adding a path to all 3 of these and got this error:
Error using __init__><module> (line 32)
Python Error: ImportError: Unable to import required dependencies:
numpy: Error importing numpy: you should not try to import numpy from
its source directory; please exit the numpy source tree, and relaunch
your python interpreter from there.
My question is: what am I doing wrong here? Ive tried numerous times to open this file in matlab with no success, im not sure if I over complicating it or im missing something obvious. Ive read through the matlab documentation on running python codes but none of them worked, how do I make sure I have all the right libraries where I need them? Thanks!
  2 个评论
Abhaya
Abhaya 2024-12-17,12:35
Hi Bradley, Could you run the same Python script successfully outside of Matlab to confirm that all the required libraries are correctly installed and working?
Bradley
Bradley 2024-12-17,13:55
Yes, I should have added that bit of information. If I run the python code in VS Code it runs fine with no issue.

请先登录,再进行评论。

采纳的回答

Mike Croucher
Mike Croucher 2024-12-17,12:20
Your python environment looks strange to me. I've never seen pythonw.exe used in MATLAB Python environment. It's usually python.exe
Here's what mine looks like
PythonEnvironment with properties:
Version: "3.12"
Executable: "C:\Users\walki\AppData\Local\Programs\Python\Python312\python.EXE"
Library: "C:\Users\walki\AppData\Local\Programs\Python\Python312\python312.dll"
Home: "C:\Users\walki\AppData\Local\Programs\Python\Python312"
Status: NotLoaded
ExecutionMode: InProcess
Where did you get your Python from? You shouldn't have to add Python modules to your MATLAB path. It should be enough to pip install them to your Python environment. Does anything work with your Python? For example, does this work?
>> py.math.sqrt(12)
ans =
3.4641
A different suggestion - MATLAB Online
You could try getting this to work on MATLAB Online. MATLAB Online has a working Python environment and it was recently made possible to pip install modules. You pip install whatever you need by putting a ! before the pip command
!pip install numpy
Collecting numpy
Downloading numpy-2.2.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (62 kB)
Downloading numpy-2.2.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (16.4 MB)
Installing collected packages: numpy
Successfully installed numpy-2.2.0
!pip install pandas
Collecting pandas
Downloading pandas-2.2.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (89 kB)
Requirement already satisfied: numpy>=1.22.4 in /home/matlab/.local/lib/python3.10/site-packages (from pandas) (2.2.0)
Requirement already satisfied: python-dateutil>=2.8.2 in /usr/lib/python3/dist-packages (from pandas) (2.8.2)
Collecting pytz>=2020.1 (from pandas)
Downloading pytz-2024.2-py2.py3-none-any.whl.metadata (22 kB)
Collecting tzdata>=2022.7 (from pandas)
Downloading tzdata-2024.2-py2.py3-none-any.whl.metadata (1.4 kB)
Downloading pandas-2.2.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (13.1 MB)
Downloading pytz-2024.2-py2.py3-none-any.whl (508 kB)
Downloading tzdata-2024.2-py2.py3-none-any.whl (346 kB)
Installing collected packages: pytz, tzdata, pandas
Successfully installed pandas-2.2.3 pytz-2024.2 tzdata-2024.2
% I'm now going to run a Numpy command in MATLAB online
py.numpy.sqrt([2.0 3.0 4.0])
ans =
Python ndarray with properties:
T: [1x1 py.numpy.ndarray]
base: [1x1 py.NoneType]
ctypes: [1x1 py.numpy._core._internal._ctypes]
data: [1x3 py.memoryview]
device: [1x3 py.str]
dtype: [1x1 py.numpy.dtypes.Float64DType]
flags: [1x1 py.numpy._core.multiarray.flagsobj]
flat: [1x1 py.numpy.flatiter]
imag: [1x1 py.numpy.ndarray]
itemsize: [1x1 py.int]
nbytes: [1x1 py.int]
ndim: [1x1 py.int]
real: [1x1 py.numpy.ndarray]
shape: [1x1 py.tuple]
size: [1x1 py.int]
strides: [1x1 py.tuple]
[1.41421356 1.73205081 2. ]
A couple of points:
  • Make sure you pip install everything you need before you run any Python commands.
  • The Python module installations do not persist between MATLAB Online sessions
  3 个评论
Bradley
Bradley 2024-12-17,19:09
I uninstalled and reinstalled python and then used pyenv('Version', 'C:\correct path') and it showed a correct version of python without the w at the end. Then I restarted my computer and sure enough it worked. I used pyrunfile and that seemed to work fine. Thanks again!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Call Python from MATLAB 的更多信息

标签

产品


版本

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by