I have a python script that depends on imported packages, as shown:
#script stored as get_command_Voltage.py
import pyabf
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
#load data
def get_command_V(filepath):
abfdata = pyabf.ABF(filepath)
return abfdata
commandv = get_command_V(filepath1)
and I'd like to store the python output, commandv, in the variable commandV in matlab.
I wrote the following code on Matlab:
pyabf = py.importlib.import_module('pyabf');
filepath_this = "..." %full file path
commandV = pyrunfile("get_command_Voltage.py","commandv",filepath1 = filepath_this)
but it gives me
ModuleNotFoundError: No Module named 'pyabf'
Warning: Unable to load Python object. Saving (serializing) Python objects into a MAT-file is not supported.
> In vclampanal (line 25)
Error using <frozen importlib>_find_and_load_unlocked (line 1140)
Python Error: ModuleNotFoundError: No module named 'pyabf'
Error in <frozen importlib>_find_and_load (line 1176)
Error in <frozen importlib>_gcd_import (line 1204)
Error in __init__>import_module (line 126)
I'm just wondering how I could resolve this issue and run python scripts that depend on imports.
Any advice would be greatly appreciated!
Thank you
MATLAB can only access Python packages installed in the Python environment specified by pyenv. If MATLAB is pointing to a different Python installation, it won't be able to load or recognize packages installed in another environment (like your Anaconda environment).
Step 1. Open a terminal or Anaconda Prompt.
Step 2. Activate the Anaconda environment where pyabf is installed (if you use a specific environment, otherwise skip this step).
Step 3. Run
on macOS/Linux or
on Windows to find the path to the Python executable in the active environment.
Step 4. Copy the path to the Python executable. It should be something like
(for macOS/Linux) or a similar path on Windows.
Step 5. Open MATLAB and use the pyenv command to set the Python executable path to the one you found in the previous step.
Step 6. Run pyenv again in MATLAB to verify that it's now using the correct Python version and path.