Matlab system() call to anaconda env python script which uses VTK

I’ve spent the last 24hrs scouring the internet to try and find a solution for this but nothing seems to work.

I’m on a Windows OS

I’m using Matlab 2019a and I call a Python script during the execution of the code. The Python script basically creates a file that I need later on during the Matlab execution. I’ve been doing this for the last few weeks/months on a Linux OS without a problem, so I know it’s not the code or method in which I execute the transaction that is causing the problem.

In Matlab I call system(['python create_file.py ',arg1,' ',arg2]), where Matlab defines the arguments, then Python creates a file. I’ve tried with relative paths, absolute paths, no paths, etc. I keep getting the same error. As I said before, it works fine on Linux OS. However, on Windows, I get errors:

Traceback (most recent call last): 
  File "D:\miniconda\envs\work_tools\lib\site-packages\vtkmodules\vtkIOVeraOut.py", line 5, in <module> 
    from .vtkIOVeraOutPython import * 
ImportError: DLL load failed: The specified procedure could not be found. 
 
During handling of the above exception, another exception occurred: 
 
Traceback (most recent call last): 
  File "D:\create_file.py", line 11, in <module> 
    from meshmagick.mesh import Mesh 
  File "D:\miniconda\envs\work_tools\lib\site-packages\meshmagick\mesh.py", line 13, in <module> 
    import vtk 
  File "D:\miniconda\envs\work_tools\lib\site-packages\vtk.py", line 32, in <module> 
    all_spec.loader.exec_module(all_m) 
  File "D:\miniconda\envs\work_tools\lib\site-packages\vtkmodules\all.py", line 88, in <module> 
    from .vtkIOVeraOut import * 
  File "D:\miniconda\envs\work_tools\lib\site-packages\vtkmodules\vtkIOVeraOut.py", line 9, in <module> 
    from vtkIOVeraOutPython import * 
ModuleNotFoundError: No module named 'vtkIOVeraOutPython' 

If it wasn’t clear, I’ve installed my Python interpreter using Miniconda/conda and then created an environment called “work_tools”. When installing Miniconda, they ask if you want to “add to system PATH” and recommend not to. Therefore I did not. Miniconda installs an “Anaconda shell” which activates the base environment. I then activate the “work_tools” environment and run the Python script from there with no problems. I’ve even been able to run the code in VS Code after selecting the “work_tools” interpreter. So I have significant confirmation that what I’ve done to this point works. The problem is calling the Python script from Matlab as shown above.

I’ve tried to launch Matlab from the activated “work_tools” environment and this loads up the same exact PATH as from where I launched Matlab. I have confirmed this by running PATH from the command line and also getenv('PATH') in Matlab. This adds all the libraries that people claim you need (identified in my research) and many more

D:\miniconda\envs\work_tools;
D:\miniconda\envs\work_tools\bin;
D:\miniconda\envs\work_tools\Scripts;
D:\miniconda\envs\work_tools\Library;
D:\miniconda\envs\work_tools\Library\bin;
D:\miniconda\envs\work_tools\Library\lib;
D:\miniconda\envs\work_tools\libs;
D:\miniconda\envs\work_tools\lib;
D:\miniconda\envs\work_tools\Library\mingw-w64\bin;
D:\miniconda\envs\work_tools\Library\usr\bin;
D:\miniconda\condabin;

I’m convinced that the PATH outside and inside Matlab is the same. Again, I can execute my Python script with no problem from the command line, but not from within Matlab using system(). I’ve looked for the modules in question (VTK) and they do exist. Again, I’ve run this from command line and VS Code without issues. So it can’t be something from Miniconda…has to be something with the way Matlab sees things.

What am I missing? Why can’t Matlab run the Python code as a system command?

It really puzzles me that this works everywhere else but inside a Windows Matlab session.

UPDATE

So I tested a different Python script (one that doesn’t call VTK) that calls a library form the same Anaconda “work_tools” environment and it works fine using the approach taken above (using system() call from within Matlab). It therefore seems it is something specific in the way VTK is built/programed that Matlab (Windows) doesn’t like?

Community Feedback

“ImportError: DLL load failed: The specified procedure could not be found.“ This indicates that the DLL loaded does not have a function that is expected to be there. It is likely that VTK uses some DLL, and that MATLAB also uses that DLL, but includes an older version. When running from within MATLAB, the system finds MATLAB’s copy of the DLL instead of the one from VTK. These things are not easy to fix under Windows, it usually involves copying a DLL to the Windows directory and deleting the MATLAB copy.

Moving Forward

I’m trying to reach out specifically to the VTK community to see if they can offer any advice on what the problem is and suggest some potential solutions, debugging approaches, etc.

I meet the similar problem.
In IPython, there is a error.

In [6]: import vtk
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-6-7c7f66f02cd3> in <module>
----> 1 import vtk

~\miniconda3\envs\cadquery\lib\site-packages\vtk.py in <module>
     28     import importlib
     29     # import vtkmodules.all
---> 30     all_m = importlib.import_module('vtkmodules.all')
     31
     32     # import vtkmodules

~\miniconda3\envs\cadquery\lib\importlib\__init__.py in import_module(name, package)
    125                 break
    126             level += 1
--> 127     return _bootstrap._gcd_import(name[level:], package, level)
    128
    129

~\miniconda3\envs\cadquery\lib\site-packages\vtkmodules\all.py in <module>
     34 from .vtkRenderingLOD import *
     35 from .vtkRenderingImage import *
---> 36 from .vtkIOVeraOut import *
     37 from .vtkIOTecplotTable import *
     38 from .vtkIOImage import *

ImportError: DLL load failed: The specified procedure could not be found.

But in Python, it is OK.

import vtk