How to configure Visual Studio Code with ArcGIS Pro’s Python Environment
Want to know how to get started with writing Python code for your ArcGIS workflows? Here’s how you can get started by using Visual Studio Code as your IDE.
ArcGIS API for Python and ArcPy are both popular Python libraries for GIS users interested in automating workflows. However, to start programming and writing scripts for GIS workflows, an Integrated Development Environment (IDE) is highly recommended. This blog post will teach you how to configure your VS Code for ArcGIS Python development. VS Code is a popular IDE for developers due to its available extensions, IntelliSense code completion, and its general ability to work with other programming languages. If you are new to programming and have ArcGIS Pro installed, this article is for you!
Note: This is not an official recommendation from Esri Canada to use VS Code, but an example of how you can configure your IDE to work with Python environments in ArcGIS Pro.
1. Download Visual Studio Code (VS Code)
The first step is to download Visual Studio Code from the official website. After downloading VS Code, run the application as an administrator and install the program using the default settings.
Official site page for Visual Studio Code
2. Launch VS Code and Install the Python Extension
After successfully installing the program. Launch VS Code and it should bring you to the following interface like shown below. Don’t worry if your application doesn’t look exactly like the following picture:
A view of VS Code after successful installation
On the leftmost bar, click the last icon at the bottom and search for Python. Install the extension. You might see that it installs multiple extensions at once and that is OK.
Installing Python Extension in VS Code
Note: You may get an error message saying Python is not installed. VS Code does not know Python is installed with ArcGIS Pro. So, we need to specify where the Python interpreter is located in the next step.
VS Code Python Warning Error Message
3. Change your Python Interpreter back in VS Code
Press Control + Shift + P and search for “>Python: Select Interpreter”.
The following command palette will drop down, and you should see something like this following screenshot. Choose the first option when it says, “Enter interpreter path…”:
Then click “Find...”, which will result in the following window:
4. Locate your ArcGIS Pro Python Interpreter
To find your interpreter in its default location, go to the following path using the File Explorer:
After navigating to this folder, select the python.exe interpreter and click “Select Interpreter”.
The directory location of ArcGIS Pro’s Python Interpreter
If you are using ArcGIS Pro 2.9 and above, this interpreter will be a Python 3.7.x installation.
Note: If you have cloned environments in ArcGIS Pro, you are also able to find your specific Python Interpreter at C:\Users\<user>\AppData\Local\ESRI\conda\envs. For more information, please see tips below for cloning Python environments in ArcGIS Pro.
5. Check that you have selected the right interpreter.
Click File > New File… > Select “Python File”
You should now be able to see the right python interpreter selected looking at the bottom bar.
If you hover above the part where it says ‘arcgispro-py3’ and you see the correct path, then you are set for some coding!
Tips & Tricks:
Cloning Python Environments
If you are planning to make changes to your ArcGIS Pro Python environment, clone your default environment. The default Python Interpreter and environment is not meant to be modified and you may see unexpected and undesired behaviours. Please visit the following links for more information:
How To: Clone a Python environment with the Python Command Prompt
Default Editor for Python Scripts in ArcGIS Pro
If you are working with scripts in ArcGIS Pro’s toolboxes, you can edit your scripts in VS Code after setting it as the default program for opening Python files. If you prefer using other IDEs or editors, this workflow applies too.
One way to change the default program is to use the right click menu from the Python file.
When you edit the scripts in toolboxes, it will launch the default program to edit your Python files.
This post was translated to French and can be viewed here.