Unable to import arcpy into MATLAB

9 次查看(过去 30 天)
Filip
Filip 2024-4-16
编辑: Karanjot 2024-4-22
Using MATLAB 2023b, trying to run Python commands, mainly ArcGIS Pro related stuff via arcpy library. pyenv is set to use the correct Python installation (the one containing arcpy). Arcpy in Python works fine while I´m unable to import it into MATLAB. The import error seems to be more arcpy related.
Arcpy works fine on my computer. Also tried restarting both the computer and ArcGIS Pro. Even reinstalled ArcGIS Pro. Made an arcpy clone, unsuccessfully tried to update all packages it contains (via Package manager in ArcGIS Pro options menu). I get the same problem for both original arcpy and the clone.
It crashes while importing it. This is weird because I can import and use math library, which is installed in the Python intepreter by default. I use "a = py.importlib.import_module('arcpy');" to import it.
The error I get:
Python Error: ImportError: DLL load failed while importing _arcgisscripting: <'listed procedure not found' - kind of message in my system´s language>.
Will be grateful for any help!
*UPDATE*
1) Adding arcpy-Python interpreter into PATH as suggested here didn´t help.
2) Tested importing arcpy into MATLAB on different PC, setting pyenv to arcpy interpreter worked, so did working with other libraries installed in it (e. g. math, mentioned in 2st paragraph). Got exactly the same error.

回答(1 个)

Karanjot
Karanjot 2024-4-22
编辑:Karanjot 2024-4-22
Hey Filip,
Kindly share details about your python installation, and also confirm if you are using the Anaconda distribution for python. The above error is common with Anaconda distribution.
This is because Anaconda maps the DLLs in a non-standard spot and adds them to the path when you start Anaconda's Prompts or Navigators. The math library uses python directly. To be fast, libraries uses compiled C-code, hence those extra DLLs.
In case you are using Anaconda, Here's a workaround:
  1. Launch Anaconda Prompt or Anaconda Powershell Prompt
  2. Type matlab at the prompt. Matlab will launch with extra environment variables configured by Anaconda's startup.
  3. Test in MATLAB.
Otherwise, If you are using any Python version > 3.8 with Windows, DLLs are only loaded from trusted locations as suggested in Python documentation: https://docs.python.org/3/whatsnew/3.8.html#ctypes
This can be fixed by adding the dll path in python using:
os.add_dll_directory("PATH_TO_DLL")
In case you still encounter the error, I recommend going through below documentation to ensure that your system is configured correctly:
I hope this helps!

类别

Help CenterFile Exchange 中查找有关 Call Python from MATLAB 的更多信息

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by