|
199 | 199 | % save ExecutionMode status |
200 | 200 | % ExecutionModeStatus=pe.ExecutionMode; |
201 | 201 |
|
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 |
211 | 209 |
|
| 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 |
212 | 220 | % Import yfinance |
213 | 221 | try |
214 | 222 | py.importlib.import_module('yfinance'); |
215 | 223 | catch |
216 | | - system("rm -rf " + venvDir) |
| 224 | + system("rm -rf " + venvDir); |
217 | 225 |
|
218 | 226 | % Create the venv, if it throws an error is still functioning |
219 | 227 | system("python -m venv " + venvDir); |
|
227 | 235 | % terminate Python if Status is Loaded |
228 | 236 | pe = pyenv; |
229 | 237 | if pe.Status == "Loaded" |
230 | | - terminate(pyenv) |
| 238 | + terminate(pyenv); |
231 | 239 | end |
232 | 240 | % point MATLAB to the virtual Python environment CRITICAL |
233 | 241 | pyenv(Version=venvPy, ExecutionMode="OutOfProcess"); |
|
238 | 246 | if pe.ExecutionMode == "OutOfProcess" |
239 | 247 | disp("ExecutionMode = python is currently running OutofProcess and can be slow") |
240 | 248 | disp("To change execution mode type: terminate(pyenv) in the Command Window") |
241 | | - disp("quit and restart MATLAB") |
| 249 | + % disp("quit and restart MATLAB") |
242 | 250 | disp("type: pyenv('ExecutionMode', 'InProcess') in the Command Window") |
243 | 251 | end |
244 | 252 | end |
245 | 253 |
|
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 |
261 | 254 |
|
262 | 255 | n = numel(ticker); |
263 | 256 | Fields='all'; |
|
0 commit comments