Azzera filtri
Azzera filtri

pyrunfile fails to run functional python scripts

17 visualizzazioni (ultimi 30 giorni)
I have this python script:
from collections import OrderedDict
from docopt import docopt
from zephir.__version__ import __version__
from skimage.transform import resize
from zephir.utils.utils import *
from zephir.utils.io import *
from getters import * # This is a script located in the same folder as the script itself.
import numpy as np
import sys
Which I call using the following MATLAB code:
[fullfile(app.script_dir, sprintf('recommend_frames%s', app.script_ext)),sprintf(' --channel=1 --dataset="%s" --nx=%.f --ny=%.f --nz=%.f --nc=%.f --nt=%.f',app.video_path, app.video_info.nx, app.video_info.ny, app.video_info.nz, app.video_info.nc, app.video_info.nt)]
t_ref = pyrunfile([fullfile(app.script_dir, sprintf('recommend_frames%s', app.script_ext)),sprintf(' --channel=1 --dataset="%s" --nx=%.f --ny=%.f --nz=%.f --nc=%.f --nt=%.f',app.video_path, app.video_info.nx, app.video_info.ny, app.video_info.nz, app.video_info.nc, app.video_info.nt)],'t_ref');
Which produces this output:
ans =
'C:\Users\sep27\Documents\GitHub\NeuroPAL_ID\+Wrapper\recommend_frames.py --channel=1 --dataset="D:\demo\20221014_m6.h5" --nx=512 --ny=500 --nz=23 --nc=3 --nt=4981'
Error using <string>><module>
Python Error: ModuleNotFoundError: No module named 'getters'
However, the script executes just fine when run through a shell.
This is not the only issue I'm running into with MATLAB's python wrapper -- I also have a separate script, main.py, which executes fine in a shell, but yields a syntax error when run using pyrunfile:
py_cmd = sprintf('%s %s %s %s', fullfile(app.script_dir, sprintf('converter%s', app.script_ext)), fullfile(pwd, 'cache.mat'), fullfile(pwd, 'meta_cache.mat'), app.video_path)
pyrunfile(py_cmd);
Output:
py_cmd =
'C:\Users\sep27\Documents\GitHub\NeuroPAL_ID\+Wrapper\converter.py C:\Users\sep27\Documents\GitHub\NeuroPAL_ID\cache.mat C:\Users\sep27\Documents\GitHub\NeuroPAL_ID\meta_cache.mat D:\demo\20221014_m6.h5'
Error using matlab.apps.AppBase>@(source,event)executeCallback(ams,app,callback,requiresEventData,event)
Python Error: SyntaxError: invalid syntax (<string>, line 84)
I've checked pyenv and ensured that the correct executable is selected. Here's the output for reference:
>> pyenv
ans =
PythonEnvironment with properties:
Version: "3.10"
Executable: "C:\Users\sep27\AppData\Local\Programs\Python\Python310\python.exe"
Library: "C:\Users\sep27\AppData\Local\Programs\Python\Python310\python310.dll"
Home: "C:\Users\sep27\AppData\Local\Programs\Python\Python310"
Status: Loaded
ExecutionMode: OutOfProcess
ProcessID: "29480"
ProcessName: "MATLABPyHost"

Risposte (1)

Maneet Kaur Bagga
Maneet Kaur Bagga il 2 Mag 2024
Hi Kevin,
Please refer the following as a possible workaround for the error:
  • ModuleNotFoundError for "getters" Module : The error encountered suggests that the Python environment invoked by MATLAB does not have the same "sys.path" configuration as your shell environment therefore local modules or scripts in the same directory could not be found.
The possible workaround for this could be before calling the "pyrunfile" the MATLAB's current working directory (pwd) should be set to the directory containing both script and the "getters" module.
cd(app.script_dir); % Change MATLAB's current directory to the script's directory
An alternate way to do this is - at the beginning of the python script, append the script directory to "sys.path" to ensure Python can find the "getters" module.
import sys
from pathlib import Path
sys.path.append(str(Path(__file__).parent))
  • SyntaxError in another python script : The workaround for this could be, instead of using "pyrunfile", you can use the "system" command in MATLAB to call Python directly as done from a shell.
commandStr = sprintf('"%s" "%s" %s %s %s', ...
pyenv.Executable, ...
fullfile(app.script_dir, 'converter.py'), ...
fullfile(pwd, 'cache.mat'), ...
fullfile(pwd, 'meta_cache.mat'), ...
app.video_path);
[status, cmdout] = system(commandStr);
if status == 0
disp('Python script ran successfully');
else
disp('Error running Python script');
disp(cmdout);
end
Hope this helps!
  1 Commento
Kevin Rusch
Kevin Rusch il 2 Mag 2024
So the pwd already contains both scripts, and if I call getters directly, that works without issue. Using the system call doesn't work for me because I need to retrieve a variable (and would like to avoid having to use a cache file).

Accedi per commentare.

Prodotti


Release

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by