10

My ArcGIS Pro install recently updated from 1.2 to 1.3.

In reading Python in ArcGIS Pro 1.3 I discovered that Esri have changed how ArcGIS Pro uses Python/ArcPy.

Legacy:
In ArcGIS Pro 1.0 through 1.2, if you needed to use Python outside of ArcGIS Pro, you also had to install Python for ArcGIS Pro. With changes at 1.3, this is no longer required. To avoid confusion, uninstall Python for ArcGIS Pro.

I uninstalled Python for ArcGIS Pro, but now I can't get my IDE (Visual Studio 2013) to see arcpy anymore. In fact I don't think it's seeing Python 3 at all (switching to Python 2.7 still works)

Broken link to arcpy:
enter image description here

Unable to Select Python 3:
enter image description here

As I read that ArcGIS Pro 1.3 uses Anaconda I tried installing that, but it was installed to a completely new location and still I had no access to arcpy from VS.

How can I make Visual Studio see arcpy again?

11

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.

enter image description here

For more information see Python and ArcGIS Pro 1.3 : Conda, which includes help for other IDEs as well.

| improve this answer | |
  • 2
    How to do this with Visual Studio 2019? There doesn't seem to be an option to add a custom environment – CompanyDroneFromSector7G Mar 2 at 10:02
  • 1
    @CompanyDroneFromSector7G This may be worth a new question if things appear to be different now (link back to this Q&A for reference). Do you have the Python for Visual Studio installed? You probably won't see the custom python environment options if that's not installed (that said I haven't tried this on the newer versions so it may well have changed – Midavalo Mar 2 at 16:53
5

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.

| improve this answer | |
-1

This video works just fine. At least worked for me. https://www.youtube.com/watch?v=B8mk0oY9KIw&lc=z23jhvio3wz3g5sab04t1aokgjuvqlj2thox43y0zdsork0h00410

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.