Setting up a Python Development Environment with Anaconda and ArcPy

These instructions show how to set up a Python development environment that can use ArcPy functions and methods. They are written primarily for students in the Professional Science Master’s in GIS using Temple-owned and managed laptops.

ArcGIS Version

These instructions are written for ArcGIS 10.5. They should work with any ArcGIS 10.x version. Students who joined the PSM this term have been issued laptops with ArcGIS 10.6. The paths the instructions refer to will be different in obvious ways (e.g., the ArcPy installation will be in a folder called ArcGIS10.6 instead of ArcGIS10.5). You will want to name your conda environment something appropriate like arcpy10-6 instead of arcpy10-5.

You can also choose to have CLA IT upgrade your computer to the latest version of ArcGIS. This will probably take a few hours or overnight, so plan accordingly.

If you are registered for GUS 8066 and using a personally owned laptop, email me for a license key to install ArcGIS.

Install Miniconda Python

  1. Download the 32-bit Python 3.7 (or latest) Windows installer: https://conda.io/miniconda.html
    NOTE: You must download the 32-bit version. It does not matter whether you download Python 2.x or 3.x, since Anaconda allows you to create environments with different Python versions. ArcPy requires Python 2.7, but most developers have switched to 3.x, so we are downloading 3.7 as our base.
    NOTE: Do not download Python directly from Python.org

  2. Run the installer and choose whether to install for “All Users” or “Just Me”. For students in GUS 8066, I recommend choosing “All Users”.
  3. Install location is a matter of preference, but many Python developers prefer to keep Anaconda in a root folder, such as C:\Miniconda3:

  4. Make sure to not add Anaconda to the PATH, or make it your system Python (i.e., leave both boxes unchecked). If you have multiple Python installations, as we will, it is better to keep each one sandboxed:

  5. Click Install.

Configure Your Python Virtual Environment

You may be prompted during this process to create an Anaconda Cloud account. This is not necessary, and may be skipped. We will not be making use of an Anaconda Cloud account in GUS 8066.

  1. Launch the Anaconda Prompt from the Windows Start Menu.
  2. Create a new environment (with the Spyder IDE) for use with ArcPy with the following command:
    conda create --name arcpy10-5 python=2.7 spyder
    

    Hit Enter to proceed.

  3. In Windows Explorer, navigate to C:\Python27\ArcGIS10.5\Lib\site-packages. Find the file Desktop10.5.pth and copy it to C:\Miniconda3\envs\arcpy10-5\Lib\site-packages. (Note: This assumes you did not change the install path in the previous step. If you created the virtual environment with a different name or at a different path, copy Desktop10.5.pth to that folder.)
  4. Open a text editor (such as Notepad++). Create a new file and add the following line:

    C:\Python27\ArcGIS10.5\Lib\site-packages
    

    Save the file as: C:\Miniconda3\envs\arcpy10-5\Lib\site-packages\arcgis-python-site-packages.pth

  5. Navigate to C:\Miniconda3\envs\arcpy10-5 and double-click python.exe. A Python console will open in a terminal window. At the Python prompt (>>>) type import arcpy and hit Enter. If everything is set up correctly, there should be no error messages. Your Window should look like this:

    • If you get the message ImportError: No module named arcpy, make sure that you did step 3 correctly.
    • If you get the message ImportError: No module named numpy, you have successfully completed step 3, but make sure that you did step 4 correctly.
  6. Close the terminal.

Testing the Spyder IDE

ArcGIS does not come with a Python editor. If you are going to be doing any significant development, you will want to use a full-fledged IDE. There are a large number of Python IDEs. The one we will use for this course is Spyder, the Scientific PYthon Development EnviRonment.

The instructions for creating the conda environment included Spyder. If you forgot, or want to add Spyder to a different environment:

  1. From the start menu, open “Anaconda Prompt (arcpy10-5)”. Or, if this is missing, open the Anaconda Prompt without any environment specified, then type conda activate arcpy10-5.
  2. Install Spyder with:
    conda install spyder
    

    Hit Enter to proceed.

To open Spyder in the future, you can launch it from the Windows Start Menu. Spyder is installed to a specific virtual environment, in the Anaconda2 program folder, you will see an entry with the name of the virtual environment, such as “Spyder (arcpy10-5)”. If you create additional virtual environments, Spyder will not be available unless installed in each environment, after which a new Start Menu entry will be created that is specific to that virtual enviroment.

Once you have set it up correctly, you might want to copy the “Spyder (arcpy10-5)” to the Desktop, or pin it to the Taskbar or the Start Menu.

If you try to import ArcPy in Spyder and it fails, you may have the wrong version of Spyder open (if, for example, you installed Spyder to the base environment or a different virtual environment). If you imported ArcPy successfully in the console (previous section), your virtual environment is set up correctly, so the problem is Spyder. Check your path:

import sys
print(sys.path)

You should see C:\Python27\ArcGIS10.5\Lib\site-packages at the end of the list of paths. If you do not see it, you have the wrong version of Spyder open. Close Spyder and look for “Spyder (arcpy10-5)” in the Start Menu.

Notes

These instructions are based on instructions by Qiusheng Wu. Those instructions are more general, and are a better guide if you do not have the exact same software versions we are using for this class. Some notes:

  • ArcGIS 10.5.1 installs Python 2.7.13. The latest Python release (and the one installed by Anaconda 5.0.1) is 2.7.14. As 2.7.14 is a bug fix release with no new features, I have assumed (and so far can confirm) that ArcPy will function with Python 2.7.14. That being the case, I have demonstrated creating your Python virtual environment with the most recent version of Python. Wu’s instruction create a virtual environment with the Python version matching the version installed by ArcGIS. If I find that we run into compatibility issues, I will update these instructions.
  • Wu’s instructions claim that ArcPy requires specific versions of numpy and matplotlib, and he demonstrates how to determine the correct versions, and install them to the arcpy10-5 virtual environment using conda at the command line.
    • Rather than install numpy or matplotlib, I have added instructions (“Configure Your Python Virtual Environment” – step 4 above) to create a path file so that the arcpy10-5 enviroment can find the packages in the ArcGIS Python site library. This obviates the need to install specific versions, as Python will use the same version available to ArcGIS.
    • If you upgrade numpy or matplotlib manually, or install another package that depends on numpy or matplotlib, the versions will become out of sync. While this is officially unsupported, GIS.SE user James insists that ArcPy works with more recent, unsupported versions of numpy and matplotlib, and that his organization does not bother coordinating numpy and matplotlib versions. Following this advice, the class did not run into compatibility issues in the past, but if we do I will update these instructions.
  • I have only demonstrated how to create an Anaconda Python installation that can import ArcPy. Wu’s instructions also demonstrate how to set up an ArcGIS Python environment to import packages installed by Anaconda. If you set up two-way package imports, you have to keep aware of which environment you are developing in, and realize that if you don’t keep package versions in sync, scripts developed in one environment may not function in the other environment.

Resources