Related
Esri Community threads about this topic:
If you use ArcGIS Pro for education or work, you will at some point be required to use Python scripts to automate some processes without the need to launch the programme’s graphical interface. ArcGIS Pro has a Conda environment with all Python requirements which can be cloned and modified as needed.
I generally prefer using Windows Terminal to launch command prompts instead of launching a separate window for a Python executable. Here’s how I set up Windows Terminal to launch my cloned ArcGIS Pro Python environment.
Prerequisites
Make sure you have the following installed:
- ArcGIS Pro
- either Anaconda or Miniconda
- Windows Terminal
Note
See the Conda user guide to determine whether you should install Anaconda or Miniconda. In the following examples, I am using a single-user installation of Miniconda. Just replace miniconda3
with anaconda3
if you are using Anaconda instead. The Conda paths should also be changed accordingly if a system-wide installation is used.
Cloning the ArcGIS Pro environment
This can be done either using the graphical interface or the command line.
Using the graphical interface
Launch ArcGIS Pro without a template, click on the ‘Project’ menu, click on ‘Python’, then click ‘Manage Environments’. In the window that opens, click on either ‘Clone Default’ or the ‘Clone’ icon of the default environment, called arcgispro-py3
. Selecting the latter will allow you to set a custom path for the cloned environment. Once the environment has been cloned successfully, activate it and close ArcGIS Pro.
Using the command line
Launch the Python Command Prompt which is situated within the ArcGIS folder in the Start menu (use the search function to find it). If you can’t find it, launch it with the following command (which is essentially the target of the Python Command Prompt):
cmd.exe /k "%USERPROFILE%\\AppData\\Local\\Programs\\ArcGIS\\Pro\\bin\\Python\\Scripts\\proenv.bat"
Note
The command above assumes a single-user installation of ArcGIS Pro. If the programme was installed for all users or in a custom path, then the target would be different, and administrator privileges may be required. To get the target of an application shortcut, right-click the file, click ‘Properties’, go to the ‘Shortcut’ tab, and copy the value of ‘Target’.
To clone the default ArcGIS Pro environment, run the following in the prompt1 2:
conda create --clone arcgispro-py3 --name ${path_to_env}
In the command above, either specify a full custom path (e.g. G:\Documents\ArcGIS\envs\arcgispro-py3-clone
) or just the name of the environment (e.g. arcgispro-py3-clone
) in place of ${path_to_env}
. If only the name is specified, the environment will be cloned to the default directory, i.e. %USERPROFILE%\AppData\Local\Programs\ArcGIS\Pro\bin\Python\envs
.
Adding the prompt to Windows Terminal
Now, launch Windows Terminal, click on the dropdown arrow, and click on ‘Settings’. settings.json
will now open in your text editor.
In this file, there will be a list of unique profiles for the terminal, the defaults being Windows PowerShell, Command Prompt, and Azure Cloud Shell. I added my ArcGIS Pro Conda prompt at the end of the list, and it looks like this (see Microsoft Docs3 for more information):
{
"guid": "{e1e3667b-a5a6-45ad-8ea2-9409be7ba466}",
"name": "ArcGIS Pro",
"commandline": "powershell.exe -ExecutionPolicy ByPass -NoExit -Command \"& '%USERPROFILE%\\miniconda3\\shell\\condabin\\conda-hook.ps1' ; conda activate '${path_to_env}'\"",
"startingDirectory": "${starting_directory}",
"icon": "${path_to_icon}"
}
The three main keys required are guid
, name
, and commandline
.
A unique GUID must be assigned to each new item in the list. To generate a unique one for a new profile, run [guid]::NewGuid()
in PowerShell4. A descriptive name should also be assigned in the name
key (I chose ArcGIS Pro).
Now, the commandline
value needs to be added. I’m going to use PowerShell, so the value should be of the following format:
powershell.exe -ExecutionPolicy ByPass -NoExit -Command \"& '%USERPROFILE%\\miniconda3\\shell\\condabin\\conda-hook.ps1' ; conda activate '${path_to_env}'\"
I came up with this command based on the target set for the Anaconda PowerShell Prompt shortcut (which can be found in the Anaconda3
Start menu folder). The slashes in the paths, as well as double quotes within the command line, must be escaped as shown above.
Optionally, set the starting directory to your usual ArcGIS working directory in place of ${starting_directory}
, and an icon for the terminal profile in place of ${path_to_icon}
.
After saving settings.json
, the prompt will appear in Windows Terminal’s dropdown menu.
Comments
Zack Stauber
Hey, this worked! Thank you so much! Esri has never included a working PowerShell prompt for their Conda distribution, but PowerShell is so much better than Command Prompt for scripts. I am glad to finally have this in my Windows Terminal.
Oliver Daniel
@Zack I submitted a request to ESRI about better Powershell support, primarily because many corporate users access ArcGIS Pro on workstations where cmd.exe is disabled. One caveat to this method is that you are using the version of conda installed with miniconda/Anaconda when activating an environment associated with the conda included with ArcGIS Pro. I modified the conda-hook.ps1 & Conda.psm1 to work with ArcGIS Pro alone which is useful if one can’t install miniconda because of permisions
Comments are closed