Python not recocgnising matlab.engine import
27 ビュー (過去 30 日間)
古いコメントを表示
Hi, i'm trying to use Matlab R2023b with a Student license (installed not in the default directory) from Python. I'm using python from Anaconda on a virtual enviroment using version 3.9.18.
I've installed it from pip on some test and other times from the extern folder of Matlab.
When I try to execute I get the following error
Traceback (most recent call last):
File "c:\[...]\matlab.py", line 5, in <module>
import matlab.engine
File "c:\[...]\matlab.py", line 5, in <module>
import matlab.engine
ModuleNotFoundError: No module named 'matlab.engine'; 'matlab' is not a package
My python code is:
matlab_path = r'D:\Programs\Matlab'
# Start MATLAB engine
import matlab.engine
eng = matlab.engine.start_matlab(matlab_path)
# Call MATLAB function
result = eng.sqrt(4.0)
print(result)
# Stop MATLAB engine
eng.quit()
0 件のコメント
回答 (1 件)
Karl
2023 年 12 月 21 日
The error message is telling you that Python isn't able to locate the matlab package. It's difficult to know why this is without knowing the details of how you performed the installation, and your environment setup. A workaround could be to add at the beginning of your script:
import sys
sys.path.append("/path/to/folder/containing/your/installation/of/matlab/package")
A better approach would be to work out why the matlab package can't currently be found. From the Python prompt, does:
import sys
print(sys.path)
return the search path that you expect? Again from the Python prompt, does:
import matlab.engine
give an error both with and without the virtual environment activated?
4 件のコメント
Karl
2023 年 12 月 22 日
Could you post the contents of _arch.txt? It looks as if there could still be a problem there.
参考
カテゴリ
Help Center および File Exchange で Call MATLAB from Python についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!