How can I import python libraries inside a python script that am calling from Matlab?

168 views (last 30 days)
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 (this method is tested) from 'test.m' in Matlab:
path = '/Users/drewthayer/Dropbox/path/to/current/';
script = '';
cmdstr = sprintf('python %s%s', path, script);
[status, cmdout] = system(cmdstr, '-echo'); % echo optional
if status==0
fprintf('data shape= %s\n',cmdout);
I get the following error:
>> run test.m
ans =
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/'>
Traceback (most recent call last):
File "/Users/drewthayer/Dropbox/path/to/current/", line 10, in <module>
import pymodule.iq_parser
File "/Users/drewthayer/Dropbox/path/to/current/pymodule/", line 7, in <module>
from . import s3io
File "/Users/drewthayer/Dropbox/path/to/current/pymodule/", line 9, in <module>
import boto3
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 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

Answers (3)

Jim Hokanson
Jim Hokanson on 29 Nov 2022 at 22:55
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
Rafael Rebouças on 19 Jan 2021
My solution:
% Simple way to load your library
copyfile(my_library_file_path, pwd, 'f');
% It's necessary to load library
delete '';
Now, your Python library is loaded to use in any directory.
Reno Filla
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?

Sign in to comment.

Pierre Harouimi
Pierre Harouimi on 23 Apr 2021
Try py.importlib.reload(yourmodulename)

Community Treasure Hunt

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

Start Hunting!