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

211 ビュー (過去 30 日間)
Drew Thayer
Drew Thayer 2019 年 2 月 14 日
コメント済み: DGM 2024 年 3 月 5 日
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
  1 件のコメント
Julian 2023 年 12 月 12 日
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.


回答 (3 件)

Jim Hokanson
Jim Hokanson 2022 年 11 月 29 日
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 2021 年 1 月 19 日
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.
  4 件のコメント
Reno Filla
Reno Filla 2021 年 4 月 23 日
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 2024 年 3 月 5 日
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.


Pierre Harouimi
Pierre Harouimi 2021 年 4 月 23 日
Try py.importlib.reload(yourmodulename)


Help Center および File ExchangeCall Python from MATLAB についてさらに検索


Community Treasure Hunt

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

Start Hunting!

Translated by