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

200 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 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
if status==0
fprintf('data shape= %s\n',cmdout);
end
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/parse.py'>
Traceback (most recent call last):
File "/Users/drewthayer/Dropbox/path/to/current/test.py", line 10, in <module>
import pymodule.iq_parser
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>
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 test.py script, e.g.
sys.path.insert(-1,'/anaconda3/lib/python3.6/site-packages/boto3')
  • 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
insert(P,int32(0),modpath);
end
Any help would be very much appreciated
  1 Comment
Julian
Julian on 12 Dec 2023
Have you loaded your library in your Python script?
It seems strange (wrong) to me to load the Python library into Matlab and execute a Python script/function in which this has not been done.

Sign in to comment.

Answers (3)

Jim Hokanson
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
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
py.my_library.any_method_or_function();
delete 'my_library.py';
Now, your Python library is loaded to use in any directory.
  4 Comments
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?
DGM
DGM on 5 Mar 2024 at 5:41
Posted as a comment-as-flag by Ron
this answer has been put out in many similar queries but the user has not given any kind of explanation regarding where/how to use it even after many other users have requested him to do so.

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!