Issues Using python from MATLAB

조회 수: 36 (최근 30일)
Stephen Hummel
Stephen Hummel 2022년 1월 13일
답변: Yongjian Feng 2022년 1월 19일
I am trying to use python from MATLAB to run a simple python script that writes information to a website. My python script works great in python but my other code is in MATLAB so I would like to make my process continuous and call python from MATLAB. I set my pyenv to the python used by anaconda on my computer but this seems to consistently cause MATLAB to crash.
Subsequently, I updated the python on my computer, changed the python environment in MATLAB to the newer version, but am struggling to get things to work on several fronts. (I am new to this MATLAB-python interface so please forgive me).
Ideally, I would like to use the MATLAB function pyrunfile, but I end up getting the error:
pyrunfile("autofill.py")
Error using <string>><module> (line 2)
Python Error: ModuleNotFoundError: No module named 'selenium'
I used pip3 install selenium and installed the module. Anytoughts on what I am doing wrong or ways to improve?
Code for Changing Environment:
pyenv('Version', '/usr/local/bin/python3.9')
ans =
PythonEnvironment with properties:
Version: "3.9"
Executable: "/usr/local/opt/python@3.9/bin/python3.9"
Library: "/usr/local/opt/python@3.9/Frameworks/Python.framework/Versions/3.9/lib/libpython3.9.dylib"
Home: "/usr/local/opt/python@3.9/Frameworks/Python.framework/Versions/3.9"
Status: NotLoaded
ExecutionMode: InProcess
  댓글 수: 2
Ive J
Ive J 2022년 1월 13일
편집: Ive J 2022년 1월 13일
Can you verify you are using the exact version of python as the one pip3 uses?
If you have different versions, you can try to install the modules specifiying py version:
py -3.9 -m pip3 install <modules>
Can you also verify that pip3 installed the modules on py 3.9?
pip3 -V
Also, from MATLAB what happens if you check
mod = py.importlib.import_module("numpy") % I get no error for numpy
Stephen Hummel
Stephen Hummel 2022년 1월 13일
I used pip3.9 which shows pip version 20.2.3.
I just confirmed install of numpy and it shows:
Requirement already satisfied: numpy in /Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages (1.22.0)
When I tried:
mod = py.importlib.import_module("numpy") % I get no error for numpy
I got the error:
mod = py.importlib.import_module("numpy")
Error using <frozen importlib>_find_and_load_unlocked (line 984)
Python Error: ModuleNotFoundError: No module named 'numpy'
Error in <frozen importlib>_find_and_load (line 1007)
Error in <frozen importlib>_gcd_import (line 1030)
Error in __init__>import_module (line 127)
This made me realize that the path to the module library were not correct. I corrected it and was able to recognize the numpy library. Thank you.

댓글을 달려면 로그인하십시오.

답변 (1개)

Yongjian Feng
Yongjian Feng 2022년 1월 19일
python has trouble to find the package selenium. One easy way out is to set the environment variable PYTHONPATH inside matlab to point to the folder you installed this package.
This page is about setting env var inside matlab.

카테고리

Help CenterFile Exchange에서 Call Python from MATLAB에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by