Importing custom python module fails
Mostrar comentarios más antiguos
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 comentarios
Robert Snoeberger
el 26 de En. de 2016
To clarify, what do you mean by "Matlab crashes" and what was the full command given? I'm using Python 3.4 and it works for me:
>> py.importlib.import_module('fabio')
ans =
Python module with properties:
version: [1x5 py.str]
HiPiCimage: [1x1 py.module]
COMPRESSORS: [1x1 py.dict]
openheader: [1x1 py.function]
pilatusimage: [1x1 py.module]
edfimage: [1x1 py.module]
...
Note: I truncated the display of ans because it is quite long.
Fernando Bechtelar
el 26 de En. de 2016
Robert Snoeberger
el 27 de En. de 2016
/usr/bin/python2.7.so is the shared library for Python. It might be helpful to attach the stack trace.
Fernando Bechtelar
el 27 de En. de 2016
Respuesta aceptada
Más respuestas (1)
Rafael Rebouças
el 19 de En. de 2021
Editada: Rafael Rebouças
el 19 de En. de 2021
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 comentario
Ronan Fleming
el 12 de Abr. de 2021
https://nl.mathworks.com/help/releases/R2021a/matlab/matlab_external/out-of-process-execution-of-python-functionality.html
Categorías
Más información sobre Introduction to Installation and Licensing en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!