Skip to content

Commit f400ae3

Browse files
New version, check for module yfinance without starting Python and setting property Status = Loaded. This allow the user to set pyenv property ExecutionMode to InProcess and retain it
1 parent a575b4a commit f400ae3

1 file changed

Lines changed: 20 additions & 27 deletions

File tree

toolbox/utilities/getFundamentals.m

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -199,21 +199,29 @@
199199
% save ExecutionMode status
200200
% ExecutionModeStatus=pe.ExecutionMode;
201201

202-
% Ensure Python ExecutionMode is set to OutOfProcess
203-
if pe.Status == "Loaded" && pe.ExecutionMode == "OutOfProcess"
204-
terminate(pe);
205-
elseif pe.Status == "Loaded" && pe.ExecutionMode == "InProcess"
206-
error('FSDA:getFundamentals',"Python is loaded InProcess. End/restart the MATLAB Online session, then rerun.");
207-
elseif pe.Status == "NotLoaded" && pe.ExecutionMode == "InProcess"
208-
% force OutOfProcess
209-
pyenv ("ExecutionMode", "OutOfProcess")
210-
end
202+
% similar to !/home/matlab/venvs/yfenv/bin/python -m pip list
203+
% check if yfinance module is available
204+
[status,out] = system(sprintf('"%s" -c "import importlib.util; print(importlib.util.find_spec(''yfinance'') is not None)"', pyenv().Executable));
205+
tf = strcmp(strtrim(out), 'True');
206+
if tf == true && status == 0
207+
% do nothing!
208+
else
211209

210+
% Ensure Python ExecutionMode is set to OutOfProcess
211+
if pe.Status == "Loaded" && pe.ExecutionMode == "OutOfProcess"
212+
terminate(pe);
213+
elseif pe.Status == "Loaded" && pe.ExecutionMode == "InProcess"
214+
error('FSDA:getFundamentals',"Python is loaded InProcess. End/restart the MATLAB Online session, then rerun.");
215+
elseif pe.Status == "NotLoaded" && pe.ExecutionMode == "InProcess"
216+
% force OutOfProcess
217+
pyenv ("ExecutionMode", "OutOfProcess")
218+
end
219+
end
212220
% Import yfinance
213221
try
214222
py.importlib.import_module('yfinance');
215223
catch
216-
system("rm -rf " + venvDir)
224+
system("rm -rf " + venvDir);
217225

218226
% Create the venv, if it throws an error is still functioning
219227
system("python -m venv " + venvDir);
@@ -227,7 +235,7 @@
227235
% terminate Python if Status is Loaded
228236
pe = pyenv;
229237
if pe.Status == "Loaded"
230-
terminate(pyenv)
238+
terminate(pyenv);
231239
end
232240
% point MATLAB to the virtual Python environment CRITICAL
233241
pyenv(Version=venvPy, ExecutionMode="OutOfProcess");
@@ -238,26 +246,11 @@
238246
if pe.ExecutionMode == "OutOfProcess"
239247
disp("ExecutionMode = python is currently running OutofProcess and can be slow")
240248
disp("To change execution mode type: terminate(pyenv) in the Command Window")
241-
disp("quit and restart MATLAB")
249+
% disp("quit and restart MATLAB")
242250
disp("type: pyenv('ExecutionMode', 'InProcess') in the Command Window")
243251
end
244252
end
245253

246-
% Import yfinance
247-
try
248-
py.importlib.import_module('yfinance');
249-
catch
250-
linkCmd = ['<a href="matlab:setupPythonEnv(''', ...
251-
'PipCommand'',''install yfinance'')">', ...
252-
'setupPythonEnv(''PipCommand'',''install yfinance'')', ...
253-
'</a>'];
254-
255-
error('FSDA:getFundamentals:MissingPackage', ...
256-
['Python package "yfinance" is not installed. Run from the terminal: python -m pip install yfinance or' ...
257-
newline 'call FSDA routine setupPythonEnv' ...
258-
newline 'with the following syntax: ' ...
259-
newline linkCmd]);
260-
end
261254

262255
n = numel(ticker);
263256
Fields='all';

0 commit comments

Comments
 (0)