Setting up Python/ArcPy with ArcGIS Pro and Visual Studio?
Python/ArcPy for ArcGIS Pro 1.3 is now installed in
C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\
So your IDE needs to be pointed at that environment to pick up the link to arcpy
.
In Visual Studio open the Python Environments window, click "+ Custom..." and select "Configure".
Give the new environment a name e.g. Python 64-bit 3.5 ArcPy and populate the path fields with the path above, specifying the relevant file in that path for each field.
- Prefix Path:
C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\
- Interpreter Path:
C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\python.exe
- Windowed Interpreter:
C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\pythonw.exe
- Library Path:
C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\Lib
- Language Version:
3.5
- Architecture:
64-bit
Click Apply on the right.
For more information see Python and ArcGIS Pro 1.3 : Conda, which includes help for other IDEs as well.
I had some difficulty integrating ArcPy with ArcGIS Pro and VS Code. Here's how to get things working on Windows 10 and ArcGIS Pro 2.5.0
It is confusing at first as it depends where you have come from. You may be familiar with Python, but not ArcGIS 10.x or Pro. Or you may have Visual Studio experience but not VS Code. So this might be helpful for some.
Firstly, it is important to be careful with the terminology. ArcGIS Pro is significantly different from ArcGIS Desktop 10.x. It includes its own Conda package manager and runs Python3. VS Code is also different from Visual Studio, which is the OP's question, but most of the principles will be the same.
Its also important to know that there is a Python API SDK and a Python Toolbox and these have different uses. ArcPy is mainly used for running scripts on the desktop. whereas the API is for web interfaces.
In ArcGIS Pro, Under the Project menu, Open Python and clone the default environment. You probably want to do this in case you need to install any python conda packages that are not part of the default environment shipped with ArcGIS Pro. Click Manage Environments and clone arcgispro-py3. I called the clone C:\mypath\arcgispro-env. If cloning fails, restart ArcGIS Pro as administrator and try again. Once it succeeds, activate it by selecting it in the python package manager UI.
For VS Code, install Python extension for Visual Studio Code. In VS Code select the Python Interpreter. Hit Ctrl-Shift-P then select option Python: Select Interpreter. The name of the cloned active environment is C:\mypath\arcgispro-env. So select this in VS Code and it is now linked in to the same python environment used by ArcGIS Pro.
The next issue you will find is that arcpy.mapping doesn't seem to exist! it was replaced with arcpy.mp. The structure of the library was redesigned for ArcGIS Pro/Python3. So, while most code examples I found start with:
import arcpy
mxd = arcpy.mapping.MapDocument("CURRENT")
The new syntax is
import arcpy
mxd = arcpy.mp.ArcGISProject("C:/mypath/arcgisprojects/myproject.aprx")
It is important to be aware of the many differences between the old arcpy for Python2.7 and new arcpy for Python3. Not just the language changes, but the structural changes. This is because many of the examples and tutorials you will find might not have been updated for ArcGIS Pro.
'CURRENT' was not defined outside the ArcGIS window so I updated the script to reference my project file explicitly. You could define CURRENT in your environment somewhere.
That should do it. If not, check licenses.
IntelliSense highlighting should now work. To debug the script in VS Code, just set a breakpoint and click Run-> Start Debugging or F5.
ArcGIS is a great technical platform, but first you have to learn how to wade past the product managers and licence managers to get to it.