How do I use arcpy module from within matlab?

3 views (last 30 days)
Jeff Burkey
Jeff Burkey on 1 Apr 2025
Answered: Adarsh on 3 Apr 2025
I want to do some ArcPro GIS operations using esri arcpy library (plus a few more extensions). I have a licensed version of arcpro v 3.3.2 and it's running conda when running python 3.11. I am accessing a cloned arcgispro-py3-clone environment. So when I first set the pyenv it seems to be successful.
pyenv('Version', 'C:\Users\jbur\AppData\Local\ESRI\conda\envs\arcgispro-py3-clone\python.exe')
ans =
PythonEnvironment with properties:
Version: "3.11"
Executable: "C:\Users\jbur\AppData\Local\ESRI\conda\envs\arcgispro-py3-clone\python.exe"
Library: "C:\Users\jbur\AppData\Local\ESRI\conda\envs\arcgispro-py3-clone\python311.dll"
Home: "C:\Users\jbur\AppData\Local\ESRI\conda\envs\arcgispro-py3-clone"
Status: NotLoaded
ExecutionMode: InProcess
I have also added paths in my windows environmental paths to make sure it can find the library.
When I tried to load arcpy I get the following error.
>>arcpy = py.importlib.import_module('arcpy');
Error using __init__><module> (line 131)
Python Error: ImportError: DLL load failed while importing _arcgisscripting:
The specified procedure could not be found.
Error in _base><module> (line 14)
Error in __init__><module> (line 14)
Error in __init__><module> (line 77)
Error in <frozen importlib>_call_with_frames_removed (line 241)
Error in <frozen importlib>exec_module (line 940)
Error in <frozen importlib>_load_unlocked (line 690)
Error in <frozen importlib>_find_and_load_unlocked (line 1147)
Error in <frozen importlib>_find_and_load (line 1176)
Error in <frozen importlib>_gcd_import (line 1204)
Error in __init__>import_module (line 126)
But when I check the pyenv again, it shows something loaded. So I am perplexed a little.
>>pyenv
ans =
PythonEnvironment with properties:
Version: "3.11"
Executable: "C:\Users\jbur\AppData\Local\ESRI\conda\envs\arcgispro-py3-clone\python.exe"
Library: "C:\Users\jbur\AppData\Local\ESRI\conda\envs\arcgispro-py3-clone\python311.dll"
Home: "C:\Users\jbur\AppData\Local\ESRI\conda\envs\arcgispro-py3-clone"
Status: Loaded
ExecutionMode: InProcess
ProcessID: "15588"
ProcessName: "MATLAB"
As a test...I did try to load in arcpy into my VisualStudio IDE after instally the python package in VSS. And it worked fine. But I really need to be able to code in matlab (for obvious reason!).

Answers (1)

Adarsh
Adarsh on 3 Apr 2025
I understand that you are facing the “DLL load failed while importing _arcgisscripting” error while importing the “arcpy” library in MATLAB’s “pyenv” python environment.
This error can arise due to some of the following reasons:
  • Conflict of DLL between MATLAB and Python module specified – The module that you are trying to import might be using a DLL file that is conflicting with the version of DLL file being used by MATLAB.
Solution:
  • Use the “OutOfProcess” Execution mode of “pyenv” this runs the python scripts and functions in a separate process, through which we can use some third-party libraries in python that are not compatible with MATLAB
pyenv(ExecutionMode="OutOfProcess")
  • Libraries are present in Conda Environment, and they are not accessible by MATLAB – As you are using the “conda” environment the paths of libraries and the modules might be localised to the “conda” environment and not accessible my MATLAB
Solution 1:
  • Open the Anaconda prompt and run the MATLAB from the anaconda command prompt to ensure it has access to the files in anaconda path and then load the python environment and import the required library.
Solution 2:
  • Manually add the paths to the DLL file to the “pyenv” ’s python environment path by inserting the path to “py.sys.path” variable using the insert function as shown below:
insert(py.sys.path,int32(0),"<absolute path to module directory>")
  • Import the library and check if it gets imported without any errors.
Solution 3:
For more information on “pyenv” you can refer to the following documentation links & MATLAB Answers posts:
  1. https://www.mathworks.com/help/releases/R2024b/matlab/ref/pyenv.html
  2. https://www.mathworks.com/help/releases/R2024b/matlab/matlab_external/out-of-process-execution-of-python-functionality.html
  3. https://www.mathworks.com/help/releases/R2024b/matlab/matlab_external/undefined-variable-py-or-function-py-command.html
  4. https://www.mathworks.com/matlabcentral/answers/1750425-python-virtual-environments-with-matlab
I hope this helps!

Products


Release

R2024b

Community Treasure Hunt

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

Start Hunting!