Until ArcGIS Pro 1.3, I successfully used virtualenv-based workflows with ArcGIS in order to isolate my package installations from the main Python installation that contains ArcGIS’ arcpy package. With ArcGIS Pro 1.3, I had the opportunity to leverage conda, a package manager and environment manager that makes scientific computing a little easier, so I gave it a try – migration wasn’t super straightforward though.
First, ArcGIS Pro no longer needs a separate Python install for out-of-application Python. In fact, if you installed ArcGIS Pro Python previously, they recommend you remove it to avoid confusion. Assuming you installed ArcGIS Pro to the default location, Python is now located at C:\Program Files\ArcGIS\Pro\bin\Python.
Unfortunately, this means you now need admin privileges to install packages – probably a good thing in many cases, but it also means that created conda environments based off the main environment requires admin privileges, so you should run any commands necessary in an admin command prompt. What’s nice about the setup though is that ArcGIS Pro actually uses its own conda environment to isolate packages from the main installation, which seems like a wise way to do things.
As an aside, the base python executable seems to be at version 3.5 while the executable in the main conda environment is 3.4.5 (the previous version of Pro was at 3.4.3 or 3.4.4). I’m not sure why there is a discrepancy in major version between the main copy and the environment, but it seems worth noting.
Making our own conda environment, usually
Getting my own conda environment with all of the ArcGIS packages set up took a bit of work – here’s how I initially approached the problem:
- Open a command prompt with administrative privileges
- Change the directory to C:\Program Files\ArcGIS\Pro\bin\Python\ (assuming ArcGIS Pro installed to default location)
- Come up with a name for your new environment – here I’ll call it newenv
- run conda create -n newenv ––clone arcgispro-py3
That last command should clone the existing ArcGIS Pro virtual environment. Should, but didn’t – that method isn’t yet supported by ArcGIS Pro – I’m told by an Esri engineer that they’re working on it. Read on for the working way.
Making our own conda environment: Take 2
I was unable to get that last command to run completely without failing on either pandas, numpy, scipy, or another package, despite multiple attempts and multiple variants. I ultimately got it working by manually making a new conda environment. Here’s how I got it to actually work:
- In Windows Explorer, navigate to C:\Program Files\ArcGIS\Pro\bin\Python\envs (Once again, assuming ArcGIS Pro installed to default location)
- Copy and paste the existing folder to duplicate it, named arcgispro-py3. This folder is ArcGIS Pro’s default conda environment, and we need to clone it to have access to arcpy, and the other dependencies
- Rename the pasted folder to what you want your conda environment to be named.
That’s it for creating it- it might be a silly hack, but it works, and maintains isolation when installing packages into it. Still, it needs to be worked with the right way – we can’t just use “conda activate” or we’ll get interpreter crashes – see below for more. More generally, it might break when ArcGIS Pro upgrades (but that’s what requirements.txt, etc is for), so you’d need to recreate it – we’ll find out. And that’s how it’s worked with virtualenv and previous upgrades of ArcGIS Pro anyway – the virtualenv would need to be recreated, so we’re no worse off.
Working with your conda env
ArcGIS Pro’s python install needs a few registry keys set correctly for the conda environment – I learned this the hard way. If they aren’t, and you use standard conda methods to switch environments, like “conda activate newenv”, then you’ll get a full interpreter crash. Fortunately Esri prepared for this! Instead of conda activate, you’ll need to use proswap.bat located in C:\Program Files\ArcGIS\Pro\bin\Python\Scripts\. As a matter of practice, I find that I often want to call activate.bat newenv (located in the Scripts directory as well) any time I’ll be working on the command line for the feature of having it call the python.exe in the environment – proswap seems to set the right settings, but on the command line won’t necessarily always make “python” reference the correct executable. I’m still looking into this.
Once it’s complete, you can use your new conda environment by running proswap.bat newenv. Usually, I’ll make a batch file in my project called activate.bat that calls “C:\Program Files\ArcGIS\Pro\bin\Python\Scripts\proswap.bat” newenv – the quotes matter so that Windows doesn’t think C:\Program is a command. Now, I can just call activate.bat in my project directory to get access to the project interpreter.
Once activated, to install new package, you can run “conda install package-name” and have it installed just to that environment. That said, in practice, it tries to update the python version in the environment as well. I haven’t tried it yet, but my recollection is that you might run into troubles with arcpy if you update the Python version – it could be fine, but Esri previously recommended not upgrading to newer Python releases, even within the same major version. What you can do is run conda install package-name and have it list out the set of dependencies, including the python update, then enter “n” when prompted to proceed to stop the installation. You can then manually install the pacakge with conda install package-name –no-deps where it won’t process dependencies. But you then need to manually run the same command for each of the required packages so that actual dependencies are processed.
An alternative for packages like that is to use pip – pip and conda can manage packages side by side, and I’m successfully using packages installed by pip that had many dependencies alongside packages like scikit-learn which I installed with conda. YMMV.
OK, that’s it – I hope that helps you get started with conda in ArcGIS Pro.
Edit 2/2017: Esri has a document with more information on using Conda environments in Pro that is a very helpful comapnion to this.