How can I import python libraries inside a python script that am calling from Matlab?
196 views (last 30 days)
Show older comments
Drew Thayer on 14 Feb 2019
Answered: Jim Hokanson on 29 Nov 2022
This is a question from someone who primary works in python, and has some Matlab experience (but not much in nuances of Matlab environment).
I have a custom python module 'pymodule' on my os (mac) and I need to call it from inside a Matlab routine. I have placed it inside my Matlab working directory. There is a script inside the module that needs to import some standard and pip-installed libaries, such as 'urllib' and 'boto3'. In my case, urllib came standard with my Anaconda 3.6 install, and I installed boto3 with conda.
In both cases, Matlab can call these modules directly, and they work (I can use them). From the matlab cmd window:
mod = py.importlib.import_module('boto3');
mod = py.importlib.import_module('urllib');
However, when I call my python module test.py (this method is tested) from 'test.m' in Matlab:
path = '/Users/drewthayer/Dropbox/path/to/current/';
script = 'test.py';
cmdstr = sprintf('python %s%s', path, script);
[status, cmdout] = system(cmdstr, '-echo'); % echo optional
fprintf('data shape= %s\n',cmdout);
I get the following error:
>> run test.m
Python module with properties:
splitattr: [1×1 py.function]
ParseResult: [1×1 py.type]
%%%%% etc... (many attributes and methods)
<module 'urllib.parse' from '/anaconda3/lib/python3.6/urllib/parse.py'>
Traceback (most recent call last):
File "/Users/drewthayer/Dropbox/path/to/current/test.py", line 10, in <module>
File "/Users/drewthayer/Dropbox/path/to/current/pymodule/iq_parser.py", line 7, in <module>
from . import s3io
File "/Users/drewthayer/Dropbox/path/to/current/pymodule/s3io.py", line 9, in <module>
ImportError: No module named boto3
It seems like Matlab, working through python, can recognize and import urllib, but can't recognize and import boto3, even though it CAN import boto3 through the py.importlib.import_module('boto3') routine.
In trying to debug this, I've tried the following steps:
- adding paths to urllib and boto3 to my $PYTHONPATH in my ~/.bash_profile (they are visibile to python via sys.path)
- manually adding boto3 path to sys.path in the test.py script, e.g.
- manually adding boto3 path inside the matlab script to py.sys.path (python's path known to Matlab)
>> modpath = '/anaconda3/lib/python3.6/site-packages/boto3';
% method from the matlab documentation
P = py.sys.path;
if count(P,modpath) == 0
Any help would be very much appreciated
Jim Hokanson on 29 Nov 2022
Was just working on some Python/MATLAB things and saw this question. The issue is that you are making a system call that is completely unrelated to the Python interpreter that MATLAB has loaded into "py"
You need to run your script via the py package.
In newer versions of MATLAB you can do pyrun or pyrunfile. However, in older versions this doesn't exist (new as of 2021b???). It looks like you would need to import your code as a module and execute.
test = py.importlib.import_module('test');
Note, I am assuming that importing the "module" is sufficient to run it but you may want to wrap your code in a function that gets called after importing.
Rafael Rebouças on 19 Jan 2021
% Simple way to load your library
copyfile(my_library_file_path, pwd, 'f');
% It's necessary to load library
Now, your Python library is loaded to use in any directory.
Reno Filla on 23 Apr 2021
I am sorry but I can't get this solution to work. Can you please show how this would make matplotlib accessible in Python for Matlab?
Pierre Harouimi on 23 Apr 2021
Find more on Call Python from MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!