简体   繁体   中英

matlab python interface import tensorflow error

I am trying to blend matlab codes with a python solver, but a error

Python Error: ImportError: No module named tensorflow

pop when I execute

py.importlib.import_module('myModule')

to test if it goes through. I have try to update the py.sys.path , but it does not work. Settting are Matlab2018a; python2.7, tensorflow1.60(both install via Anaconda2) . I am sure the tensorflow is well installed and the python codes is running well. Any one can help?

This is 1 year late, but I just encountered this issue.

It may be a result of building your environment through Anaconda -- this adds some unnecessary overhead vis a vis setting the path. Instead, install python and your relevant libraries via pip in your command line, then find the location of the directories of both the (new) python executable and site-packages and add them to your python path before calling the python script.

% Specify python executable directory and directory of relevant python libraries.
pyScriptDir = '...';
pcPythonExeDir = 'C:\Users\xxxx\AppData\Local\Programs\Python\Python37\python.exe'
[ver, exec, loaded] = pyversion(pcPythonExeDir)
pyLibraryDir    = 'C:\Users\xxxx\AppData\Local\Programs\Python\Python37\Lib\site-packages';

% Ensure python-matlab integration code is on matlab path.
pyMatDir = fullfile(matlabroot, 'toolbox', 'matlab', 'external', 'interfaces', 'python');
addpath(pyMatDir);

% Add folders to python system path.
insert(py.sys.path, int64(0), pyScriptDir);
insert(py.sys.path, int64(0), pyFolder);
insert(py.sys.path, int64(0), pyLibraryFolder);

% Call your script ("inputs" should be python objects).
py_myScript = py.importlib.import_module('myScript')
out = py_myScript.my_func(inputs)

Alternatively, this works very well too: https://www.mathworks.com/matlabcentral/answers/153867-running-python-script-in-matlab

Make sure to convert any .pynb files to .py files (I'm not sure if this is necessary, but I did it this way to be safe).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM