Importing custom python module fails
14 ビュー (過去 30 日間)
古いコメントを表示
Fernando Bechtelar
2016 年 1 月 26 日
コメント済み: Ronan Fleming
2021 年 4 月 12 日
I am using Matlab2014b and try to import python packages. I am on a linux computer and executing the command py.numpy.* works and py.importlib.import_module('numpy') gives me the correct output. However, numpy lives in my /usr/lib/pymodules/python2.7/ directory, which according to the output of py.sys.path is in the search directory, so the import should work.
However, I have another package that I would like to include, which is in /usr/lib/python2.7/dist-packages/. Again, according to py.sys.path, this directory is included in the search path, however, importing the package fails ("Import argument 'py.fabio' cannot be found or cannot be imported."). I hope someone has an advice how to solve this problem.
PS: pyversion returns my current installation (2.7) and shows isloaded: 1
PPS: Adding the directory to the search path, as outlined in http://www.mathworks.com/help/matlab/matlab_external/call-python-from-matlab.html has failed.
PPPS: When I try to get feedback from py.importlib.import_module, Matlab crashes.
4 件のコメント
Robert Snoeberger
2016 年 1 月 27 日
/usr/bin/python2.7.so is the shared library for Python. It might be helpful to attach the stack trace.
採用された回答
Robert Snoeberger
2016 年 1 月 28 日
編集済み: Robert Snoeberger
2016 年 1 月 28 日
Based on the stack trace, your issue looks like a library conflict with libhdf5.so. It seems like the libhdf5 that is in the stack trace is a library that is installed with MATLAB; I am assuming this based on the '/matlab_2015b/bin/glnxa64/' in the path to the library. I would guess that the appropriate version of libhdf5.so for Python should be located at /usr/lib/libhdf5.so. Try the following command to look for the correct libhdf5.so.
ldd /usr/lib/python2.7/dist-packages/h5py/h5f.so | grep libhdf5
My hunch about the library conflict is further confirmed by the fact that the libhdf5.so function that is executing is called H5check_version (line [2] of the stack trace). The documentation I found for that function says, "If this check fails, H5check_version causes the application to abort (by means of a standard C abort() call)..." [1].
Assuming you aren't using MATLAB's interface to HDF5, you should be able to use LD_PRELOAD to load the correct version of libhdf5.so [2,3].
References
- https://www.hdfgroup.org/HDF5/doc1.6/RM_H5.html#Library-VersCheck
- http://man7.org/linux/man-pages/man8/ld.so.8.html -- Search for "LD_PRELOAD". It should say, "A list of additional, user-specified, ELF shared objects to be loaded before all others..."
- http://stackoverflow.com/questions/426230/what-is-the-ld-preload-trick
6 件のコメント
Michael Thieme
2018 年 10 月 18 日
I'm also having this issue on Windows 10, it would be great if someone is able to find a workaround!
Michael Thieme
2018 年 10 月 18 日
編集済み: Michael Thieme
2018 年 10 月 18 日
All,
I found a solution to this problem. My problem was related to tensorflow/keras rather than fabio. This thread (https://github.com/NeurodataWithoutBorders/api-python/issues/28) led me to the solution. Basically my order of operations was:
- conda uninstall hdf5
- conda uninstall h5py
- pip uninstall hdf5
- pip uninstall h5py
- conda install hdf5=1.8.12 h5py=2.7.0 -c jonc
- pip install keras
- pip install tensorflow-gpu
その他の回答 (1 件)
Rafael Rebouças
2021 年 1 月 19 日
編集済み: Rafael Rebouças
2021 年 1 月 19 日
My solution:
% Simple way to load your library
copyfile(my_library_file_path, pwd, 'f');
% It's necessary to load library
py.my_library.any_method_or_function();
delete 'my_library.py';
Now, your Python library is loaded to use in any directory.
1 件のコメント
Ronan Fleming
2021 年 4 月 12 日
https://nl.mathworks.com/help/releases/R2021a/matlab/matlab_external/out-of-process-execution-of-python-functionality.html
参考
カテゴリ
Help Center および File Exchange で Call Python from MATLAB についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!