Skip to content
Advertisement

How can i fix this error that occurs when I use arcpy in Pycharm?

first off let me state I am not a programmer. My knowledge of programming is occasionally using R to help with data at my job. Recently I have been attempting to use python in order to automate several ArcGIS functions.

import arcpy

arcpy.env.workspace = 
r"C:UserslscarneyDesktop2020data_ArcScripting2020_HessianFly_ShapeFileCreation.gdb"

print(arcpy.env.workspace) 

I have been introducing myself to python and arcpy through different tutorials and I see this bit of code repeated often throughout those tutorials. However when I run this instead of “Finished with exit code 0” I have this mess.

Traceback (most recent call last):
  File "C:UserslscarneyDesktopArcGIS ScriptsArcGIS_ScriptsvenvRepairDataSource.py", line 1, in <module>
    import arcpy
  File "C:Program Files (x86)ArcGISDesktop10.6ArcPyarcpy__init__.py", line 22, in <module>
    from arcpy.geoprocessing import gp
  File "C:Program Files (x86)ArcGISDesktop10.6ArcPyarcpygeoprocessing__init__.py", line 14, in <module>
    from _base import *
  File "C:Program Files (x86)ArcGISDesktop10.6ArcPyarcpygeoprocessing_base.py", line 612, in <module>
    env = GPEnvironments(gp)
  File "C:Program Files (x86)ArcGISDesktop10.6ArcPyarcpygeoprocessing_base.py", line 609, in GPEnvironments
    return GPEnvironment(geoprocessor)
  File "C:Program Files (x86)ArcGISDesktop10.6ArcPyarcpygeoprocessing_base.py", line 565, in __init__
    self._refresh()
  File "C:Program Files (x86)ArcGISDesktop10.6ArcPyarcpygeoprocessing_base.py", line 567, in _refresh
    envset = (set(env for env in self._gp.listEnvironments()))
RuntimeError: NotInitialized

Process finished with exit code 1

Things I have done:

I have tried running a few simple lines of code (adding variables, printing variables ect) outside of arcpy and everything worked fine. I have checked the python interpreter and it is one located in arcgis so I’m fairly certain that is not the issue, especially since arcpy is not available using the other python interpreter path.

If anyone could help point me in the right direction It would be greatly appreciated. I just dont have enough experience with python and Pycharm to understand what is going wrong. Thanks.

Advertisement

Answer

I figured out how to make it work, but I am not entirely sure why…. If anyone could provide some clarification it would be appreciated. Until then I will explain to the best of my ability.

The Python interpreter I was using was C:Python27ArcGIS10.6 I thought that would work because after switching to it from the default, pycharm began to recognize ArcPy functions. Obviously that didn’t work.

While reading this Noticed that path they used and tried it on my computer. — https://community.esri.com/t5/python-documents/pycharm-setup-for-arcgis-desktop/ta-p/1125129

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!)

Select Python Interpreter at left
Click the gear icon at upper right and select Add
Select the Existing environment radio button
Click (set) Make available to all projects
For Interpreter:  click the ... icon at right
 and navigate to and select the python.exe for Pro:
C:Program FilesArcGISProbinPythonenvsargispro-py3python.exe
Click OK.

What works –> C:Program FilesArcGISProbinPythonenvsarcgispro-py3

So it seems that in order for it to work the interpreter has to be from the arcgispro-py3 folder located in your ArcGIS install. I don`t understand how this differs from the path I used first because both recognized ArcPy functions. Regardless I can confirm that the new path can execute the code with no exceptions.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement