How to change python path?
Afficher commentaires plus anciens
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...
pyversion
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');
addpath(pyFolder);
% 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 'loadNNmodel.py').
py_loadModel = py.importlib.import_module('loadNNmodel')
Réponses (2)
Dominik Mattioli
le 18 Juin 2019
Hussein Adel
le 19 Oct 2020
1 vote
I did a restart to matlab and it works

Catégories
En savoir plus sur Call Python from MATLAB dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!