Cannot Import Compiled Python Package

I received a python package that had been compiled from a matlab function (MyFunction.m) with the SDK Compiler. I then installed the package in a conda virtual environment, so that I could call the package in a python script.
I attempted to import the package into python using Matlab's instructions:
import MyFunction
Unfortunately, I could not import the package and python returned this error (note that I commented the code for readability):
% In [31]: import MyFunction
% Exception caught during initialization of Python interface. Details: DLL load failed while importing matlabruntimeforpython3_9: The specified procedure could not be found.
% Traceback (most recent call last):
%
% File "C:\Users\RDCHLLAA\AppData\Local\Temp\1\ipykernel_5708\289095444.py", line 1, in <cell line: 1>
% import convert_fort61_to_mat
%
% File "C:\Users\RDCHLLAA\Anaconda3\envs\python39\Lib\site-packages\MyFunction\__init__.py", line 245, in <module>
% _pir.import_cppext()
%
% File "C:\Users\RDCHLLAA\Anaconda3\envs\python39\Lib\site-packages\MyFunction\__init__.py", line 239, in import_cppext
% self.cppext_handle = importlib.import_module(module_name)
%
% File "C:\Users\RDCHLLAA\Anaconda3\envs\python39\lib\importlib\__init__.py", line 127, in import_module
% return _bootstrap._gcd_import(name[level:], package, level)
%
% ImportError: DLL load failed while importing matlabruntimeforpython3_9: The specified procedure could not be found.
"matlabruntimeforpython3_9.pyd", the missing function, is actually in my "C:\Program Files\MATLAB\MATLAB Runtime\v912\bin\win64" folder. How do I get python to call that "matlabruntimeforpython3_9" module so I can import the MyFunction package? Do I need to install Matlab Runtime in my conda virtual environment folder somehow? I could not find any useful Matlab documentation to help.

Risposte (1)

Hi Debbie,
As per my understanding the error message suggests that the required DLL file, "matlabruntimeforpython3_9.pyd", could not be found. Please refer to the steps below to troubleshoot the error:
  • It is mentioned that MATLAB runtime environment is located in "C:\Program Files\MATLAB\MATLAB Runtime\v912\bin\win64". Verify that this directory contains the necessary files, including "matlabruntimeforpython3_9.pyd".
  • Activate your conda virtual environment by running the following command in your terminal or command prompt:
conda activate <your_virtual_environment_name>
  • Add the directory containing the MATLAB Runtime DLL file to your system's PATH environment variable. Replace "C:\Program Files\MATLAB\MATLAB Runtime\v912\bin\win64" with the actual path to the directory where the MATLAB Runtime is installed.Please run the following command in your terminal or command prompt:
set PATH=C:\Program Files\MATLAB\MATLAB Runtime\v912\bin\win64;%PATH%
  • Try importing the package again in your Python script:
import MyFunction
The import should now work without any DLL-related errors.
Hope this helps!
Regards,
Maneet Bagga

Categorie

Prodotti

Release

R2022a

Richiesto:

il 21 Apr 2022

Risposto:

il 29 Set 2023

Community Treasure Hunt

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

Start Hunting!

Translated by