PyCharm Setup for ArcGIS Desktop

9936
11
12-10-2021 04:58 PM

PyCharm Setup for ArcGIS Desktop

PyCharm is a very useful Integrated Development Environment maintained by JetBrains that has become very popular for users wanting to develop Python for use with ArcGIS Pro. This document is an attempt to guide you to a trouble free installation set up for use with ArcGIS Pro (and ArcMap if you need to work in that environment as well).

PyCharm download and install

PyCharm Community Edition is free to download from www.jetbrains.com. The Community Edition has pretty much all you will need, the professional ($) edition is worth looking into if you need to code in JavaScript or SQL, it has many useful features for that kind of development. But for coding Python scripts, the free community edition is fine.

When you run the installer be sure not to click any of the boxes on the setup dialog, it's best to keep your Windows paths and settings "clean", to avoid messing with Windows settings on which other applications may be dependent.  We will be setting preferences within PyCharm to make it play well with ArcGIS.

pycharm_install.png

Configure Python interpreter for ArcGIS Pro

After PyCharm launches, click the gear at the upper right corner of the screen (or press Ctrl-Alt-S) to open the settings dialog. You can set up PyCharm with multiple Python interpreters, and we want to make the default setup for ArcGIS Pro (you can choose others though, which is handy!)

  1. Select Python Interpreter at left
  2. Click the gear icon at upper right and select Add
  3. Select the Existing environment radio button
  4. Click (set) Make available to all projects
  5. For Interpreter:  click the ... icon at right and navigate to and select the python.exe for Pro:
    C:\Program Files\ArcGIS\Pro\bin\Python\envs\argispro-py3\python.exe
  6. Click OK.

pycharm_arcgispro_setup.jpg

Configure Python interpreters for ArcMap (ArcGIS 10.x). 

Note this step is not needed if you aren't using ArcMap.

Repeat the steps from the last section to add the pythons for ArcMap:

  • ArcMap 32-bit: C:\Python27\ArcGIS10.8\python.exe
  • ArcMap 64-bit geoprocessing (if installed): C:\Python27\ArcGISx6410.8\python.exe

Now, when setting up a new PyCharm project, you can choose from the configured interpreters to get the environment you need for your project.

Configure PyCharm Terminal for ArcGIS Pro

PyCharm supports a terminal window for use in manipulating the conda environment, running shell tools, editors, or whatever else. The standard shell environment for ArcGIS Pro is the one you launch by with the Windows shortcut Python Command Prompt - which launches a shell with the proenv.bat script.

Bring up to the same settings dialog used above to set the Python interpreters

At left, find Tools > Terminal

For Shell Path: click the ... icon at right and navigate to and select 
C:\Program Files\ArcGIS\Pro\bin\Python\Scripts\proenv.bat

Configuring PyCharm Terminal for ArcMap

(Thank you @AzinSharaf (see comments below). An Anaconda solution may be possible as well.)

1. Create a clone of the environment Python 2.7 x64 environment

C:\Python27\ArcGISx6410.8\python.exe -m virtualenv py27-clone-108

2. Copy ArcMap .pth files from C:\Python27\ArcGISx6410.8\ folder to the py27-clone-108 venv folder

3. For Terminal use this command to start a cmd.exe with the paths in place to run and manage python modules

C:\Windows\System32\cmd.exe /k "C:\Python27\envs\py27-clone-1081\Scripts\activate.bat"

 

Comments

@DanPatterson@DrewFlater - open to suggestions, I haven't seen a config guide and my students had a heck of a time getting this set up for the Python For Everyone course. So I made this attempt.

Sorry Curtis, I haven't used pycharm in ages. 

I have been using spyder installed in base using conda, but they now have installers which enable one to chose the python interpreter if they have other clones etc.  

Installation Guide — Spyder 5 documentation (spyder-ide.org)

mac, windows, linux

I don't see a major benefit of having python 2.x in pycharm terminal but you can do it with the following workflow.

1- clone python 2.7 environment

2- in the Pycharm terminal path enter:

C:\Windows\System32\cmd.exe /k "C:\Python27\envs\py27-clone-1081\Scripts\activate.bat"

then your Terminal would be like this:

AzinSharaf_0-1640135290129.png

 

There is no "clone" for ArcGIS 10x desktop python, it uses a basic standalone C Python system install, not a conda env.

You just need to point pycharm at the correct interpreter C:\Python27\ArcGIS10.[X]\python.exe

by "clone 2.7" I mean install virtualenv on default python2.x and create a new venv:

C:\Python27\ArcGISx6410.8\python.exe -m virtualenv py27-clone-108

and then copying PTH files to the new venv folder and then activate it. It works on my machine. 

 

@AzinSharaf  the benefit of using pycharm in 2.7 is so you can use PyCharm to write and debug scripts for ArcMap, although we try to write code that will work in both, you do sometimes need to test to make sure!

Personally when writing scripts for ArcMap  or Pro I try really hard (if possible) to use and test against the existing (extensive) Esri-released python stack so you aren't asking users to mess with the Python environment so they can run my tool or script.

This PyCharm Setup for ArcGIS Desktop is outdated.  This procedure doesn't seem to have the same info for setting PyCharm up for use in ArcGIS Pro 3.0.  Can someone point me to a more applicable source?  Thanks a Bunch

@Shauna-RaeBrown, I think you just need to point the PyCharm interpreter to the ArcGIS Pro Python install.

Configure a Python interpreter | PyCharm Documentation (jetbrains.com)

C:\YourInstallDir\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\python.exe

Thanks, @BlakeTerhune .  That worked, but PyCharm says "No module named arcpy" on my first line "import arcpy".  PyCharm interpreter is set for 2.7 

import arcpy

# Set the workspaces
aprx = r"P:\GIS_Workspace\AZ811andPython\AZ811_Task.aprx"
# aprx = arcpy.mp.ArcGISProject("CURRENT")
arcpy.env.overwriteOutput = True

# It would be better if I could just have the email copied into an input field in a task or toolbox.
tbl = r"P:\GIS_Workspace\AZ811andPython\AZ811_Python.gdb\tempEmailText"
field0 = ['Ticket']
field1 = ['Email_txt']
with arcpy.da.SearchCursor(tbl, field0) as tCursor:
    for row in tCursor:
        myTicket = u'{0}'.format(row[0])
        az811txt = "AZ811Ticket" + myTicket + ".txt"
        tktPath = "P:\\GIS_Workspace\\AZ811andPython\\AZ811_emails\\"
        az811File = tktPath + az811txt
del tCursor
with arcpy.da.SearchCursor(tbl, field1) as eCursor:
    for row in eCursor:
        email = u'{0}'.format(row[0])
        # Write email to a text file
        with open(az811File, 'w') as f:
            f.writelines(email.replace('\n', ''))
del eCursor

@Shauna-RaeBrown wrote:

PyCharm interpreter is set for 2.7


ArcGIS Pro doesn't use Python 2.7.

As @BlakeTerhune states, you need to point PyCharm at ArcGIS Pro's Python, either C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\python.exe or if you've cloned the default arcgispro-py3 environment, the python.exe in your clone.

 

Hi, @Luke_Pinner.  I tried steered to C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\python.exe and did the rest of the settings as noted above.  However, PyCharm pointed at whatever ArcGIS Pro uses (3.7 I think).  I still get errors with PyCharm.  I have not problems using the python window in Pro.  So I'm frustrated.    I don't use or have a cloned environment, but I'll look into.  If I get any success with PyCharm, after the eclipse, I'll leave a comment.

Version history
Last update:
‎01-02-2022 11:18 AM
Updated by:
Contributors