Python not recocgnising matlab.engine import

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()

Answers (1)

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 Comments

If i look in Anaconda Navigator it is listed. (I've also been making sure to run it in the right environment.) I also added the path (below) and check that it was being added, without any success on the import. Running the scritpt without using the enviroment is going to give an error becouse as far as i checked I have no global installation of python and the base enviroment does not have the package on it's listing.
The only problem I could see is that there are 3 folders refering to Matlab.
sys.path.append(r"C:\Users\grmap\anaconda3\envs\coppelia\Lib\site-packages\matlab")
sys.path print:
'C:\\Users\\grmap\\Master\\Robots Autonomos\\Scripts',
'C:\\Users\\grmap\\anaconda3\\envs\\coppelia\\python39.zip',
'C:\\Users\\grmap\\anaconda3\\envs\\coppelia\\DLLs',
'C:\\Users\\grmap\\anaconda3\\envs\\coppelia\\lib',
'C:\\Users\\grmap\\anaconda3\\envs\\coppelia',
'C:\\Users\\grmap\\AppData\\Roaming\\Python\\Python39\\site-packages',
'C:\\Users\\grmap\\anaconda3\\envs\\coppelia\\lib\\site-packages',
'C:\\Users\\grmap\\anaconda3\\envs\\coppelia\\lib\\site-packages\\matlabengine-23.2-py3.9.egg',
'C:\\Users\\grmap\\anaconda3\\envs\\coppelia\\Lib\\site-packages\\matlab'
Anaconda env packages:
So your path looks fine, and your didn't need the append, because the matlab folder is in:
'C:\\Users\\grmap\\anaconda3\\envs\\coppelia\\lib\\site-packages'
Could you try a cd to this directory, and from there start Python and again try:
import matlab.engine
From the package's parent directory, Python must find the package! (This doesn't solve the problem that you're having, but is a sanity check along the way.)
I've finally found the problem. I've made a cd to that path and it imported it but throwing an error that until that moment was not being showed. The matlab path was not correct. I looked into the __init__.py code and there was a file that was storing the paths called _arch.txt, I've corrected it and now it works. The only problem is that it only works using the interpreter in that specific folder.
There is another weird thing becouse if I update the package the _arch.txt takes the worng paths. I've checked the pip install script and it does not make sense, becouse it takes the matlab root path from the Matlab registry in HKEY_LOCAL_MACHINE and it seems to be ok.
The message that i've got after making the cd:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\grmap\anaconda3\envs\coppelia\Lib\site-packages\matlab\__init__.py", line 47, in <module>
add_dirs_to_path(bin_folder, engine_folder, extern_bin)
File "C:\Users\grmap\anaconda3\envs\coppelia\Lib\site-packages\matlab\__init__.py", line 19, in add_dirs_to_path
raise RuntimeError("Could not find directory: {0}".format(engine_dir))
RuntimeError: Could not find directory: D:\Programs\extern\engines\python\dist\matlab\engine\win64
Could you post the contents of _arch.txt? It looks as if there could still be a problem there.

Sign in to comment.

Products

Release

R2023b

Tags

Asked:

on 21 Dec 2023

Edited:

on 22 Dec 2023

Community Treasure Hunt

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

Start Hunting!