MATLAB <-> Python problem with import of lxml

4 views (last 30 days)
Markus
Markus on 20 May 2015
Commented: Robert Snoeberger on 31 May 2015
Hello community,
I have strange troubles when calling Python functions from MATLAB. It seems that importing some libraries cause problems.
I use Python 2.7 and have lxml for Python installed (PyPI). MATLAB version is currently 2014b.
The in MATLAB non-working Python function (filename is test.py):
from lxml import etree
def test():
return 'This was an include test'
returns the following output when I try to include and run in MATLAB:
>>insert(py.sys.path,int32(0),pwd)
>> py.test.test
Undefined variable "py" or function "py.test.test".
>> py.importlib.import_module('test')
Error using py.importlib.import_module
The class 'matlab.exception.PyException' contains a parse error, cannot
be found on MATLAB's search path, or is shadowed by another file with
the same name.
If the import line in the Python file is commented out:
# from lxml import etree
def test():
return 'This was an include test'
Everything is fine in MATLAB (I did not forget to restart MATLAB):
>> insert(py.sys.path,int32(0),pwd)
>> py.test.test
ans =
Python str with no properties.
This was an include test
>> py.importlib.import_module('test')
ans =
Python module with properties:
test: [1x1 py.function]
<module 'test' from 'C:\XXXXXXXXXXXXXXXXXX\test.py'>
Within Python both versions of the Python function "test" work.
I have the same/similar problem with a vendor delivered Python module for accessing some specialized communication hardware.
I think that both problems are related. All Python programs work from IDLE or command line, but they do not within MATLAB.
Does anyone have a clue?
Thanks! Markus

Answers (2)

Robert Snoeberger
Robert Snoeberger on 20 May 2015
Edited: Robert Snoeberger on 20 May 2015
It looks like you have two problems:
  1. the class matlab.exception.PyException isn't found
  2. import of lxml
This answer is only for the first problem. The error message "The class 'matlab.exception.PyException' contains a parse error, cannot be found on MATLAB's search path, or is shadowed by another file with the same name." isn't good. This indicates a problem with your path. I've seen an answer to another question where the solution was to reinstall MATLAB. See here .

Markus
Markus on 22 May 2015
  1. It seems that the first problem is similar to the thread in the link you have posted in your answer. Re-Installing is not the prefered option (causing troubles with IT department...). I found out that starting matlab from command line or clicking on MATLAB.exe in the \bin directory has the paths correctly set for Python(py.print('Hi') returns the string 'Hi' to MATLAB command prompt). Starting from the Windows-Start menu does not work properly. I checked the properties of the MATLAB starter in the Windows start menu - it is linked to the MATLAB.exe file in the \bin directory without any extra options. So there should not be differences, but there are. An additional difference is that when MATLAB is started from command line or the .exe directly, the working directory is not changed to my standard working directory. MATLAB starts in the directory where the command was executed (without changing to standard work directory).
  2. Using the dependency walker for DLLs from sysinternals/Microsoft it turns out that even the dependency walker is not able to find all related DLLs. The missing DLLs are stored (in several versions and folders) on the system, it seems that some path information is missing. It seems that Python "knows" the paths to the DLLs somehow, as the Python functions using lxml work properly (from command line respectively IDLE). The MATLAB integration of Python seems not knowing the correct path(s). I tried to copy one missing DLL into the path where etree.pyd (from lxml) is stored, but MATLAB returned some C-runtime error (maybe I have chosen the wrong version of the DLL or some additional stuff is missing). I´ll try to figure out the correct path and set it with insert in the MATLAB shell, but I don´t think that there will be any success.
Are there any additional ideas to problem 1. and 2.?
Thanks, Markus
  3 Comments
Markus
Markus on 26 May 2015
  1. The base directory (including all subfolders) as suggested in your link was added - the output to MATLAB prompt works now! Thanks
  2. MSVCR90.DLL, API-MS-WIN-APPMODEL-RUNTIME-L1-1-0.DLL, API-MS-WIN-CORE-WINRT-ERROR-L1-1-0.DLL, API-MS-WIN-CORE-WINRT-L1-1-0.DLL, API-MS-WIN-CORE-WINRT-ROBUFFER-L1-1-0.DLL, API-MS-WIN-CORE-WINRT-STRING-L1-1-0.DLL, API-MS-WIN-SHCORE-SCALING-L1-1-1.DLL
  3. R6034 (after copying the MSVCR90.DLL into the directory of etree.pyd, i did not assume that this will work)
Robert Snoeberger
Robert Snoeberger on 31 May 2015
Hi Markus,
I have an update on problem 2. I was able to import lxml.etree in MATLAB when using WinPython 2.7.9.5. The WinPython distribution bundles lxml.
I looked at etree.pyd with dependency walker. In this case, dependency walker was able to find MSVCR90.DLL. The other DLLs you mentioned, however, were not found. In the WinPython installation, I found MSVCR90.DLL at the following location.
WinPython-32bit-2.7.9.5\python-2.7.9\msvcr90.dll
Two other DLLs were with it, MSVCM90.dll and MSVCP90.dll.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!