Filter löschen
Filter löschen

How to change python path?

157 Ansichten (letzte 30 Tage)
Dominik Mattioli
Dominik Mattioli am 13 Jun. 2019
Beantwortet: Hussein Adel am 19 Okt. 2020
I'm trying to call a python script that imports tensorflow. I point python to my Anaconda-built python executable and I add the environment containing my installed libraries (including tensorflow) to the python path but I get the following error:
Error using loadNNmodel><module> (line 2)
Python Error: ModuleNotFoundError: No module named 'tensorflow'
Am I setting the python path correctly? Could the error be a result of my use of an anaconda environment? See code below:
% Specify Anaconda Python Executable Library.
pyExec = 'C:\Users\xxxx\AppData\Local\Continuum\anaconda3\python.exe';
NNenvFolder = 'C:\Users\xxxx\AppData\Local\Continuum\anaconda3\envs\NN';
[ver, exec, loaded] = pyversion(pyExec);
This works fine...
version: '3.7'
executable: 'C:\Users\xxxx\AppData\Local\Continuum\anaconda3\python.exe'
library: 'C:\Users\xxxx\AppData\Local\Continuum\anaconda3\python37.dll'
home: 'C:\Users\xxxx\AppData\Local\Continuum\anaconda3'
isloaded: 1
But then the error comes from:
% Ensure python-matlab integration code is on matlab path.
pyFolder = fullfile(matlabroot, 'toolbox', 'matlab', 'external', 'interfaces', 'python');
% Add folders to python system path.
insert(py.sys.path, int64(0), pyExec);
insert(py.sys.path, int64(0), NNenvFolder);
insert(py.sys.path, int64(0), pyFolder);
% Load model via python script (named '').
py_loadModel = py.importlib.import_module('loadNNmodel')

Antworten (2)

Dominik Mattioli
Dominik Mattioli am 18 Jun. 2019
After some research, I've discovered that MATLAB needs to be able to find the correct version and install of python, but adding an anaconda environment adds some additional overhead that would need to be accounted for in the environment variables to allow MATLAB to find the python modules. It is probably doable, but probably more trouble than it is worth.
Instead, install python and the relevant libraries from the command line using pip and then point to that python version and folders.
pcPythonExe = 'C:\Users\dmattioli\AppData\Local\Programs\Python\Python37\python.exe';
[ver, exec, loaded] = pyversion(pcPythonExe); pyversion
>> pyversion
version: '3.7'
executable: 'C:\Users\dmattioli\AppData\Local\Programs\Python\Python37\python.exe'
library: 'C:\Users\dmattioli\AppData\Local\Programs\Python\Python37\python37.dll'
home: 'C:\Users\dmattioli\AppData\Local\Programs\Python\Python37'
isloaded: 1
% Add folders to python system path.
pyLibraryFolder = 'C:\Users\dmattioli\.PyCharm2019.1\system\python_stubs\278535617';
insert(py.sys.path, int64(0), pyLibraryFolder)

Hussein Adel
Hussein Adel am 19 Okt. 2020
I did a restart to matlab and it works


Mehr zu Call Python from MATLAB finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by