Pycharm and ArcGIS Pro compatibility: scripts work in Python window but not in Pycharm

876
10
Jump to solution
01-14-2020 10:14 AM
QuangTruong
New Contributor II

Hi there, I've installed PyCharm as per the the ESRI course "Python for Everyone" and was able to get through the entire course without problem. 

However, when I try to run a script I am starting to write on Pycharm, though it runs with no errors, I am getting odd results. I test the script statements in Python window and am able to get the results I am expecting, but when I run them through Pycharm I get a completely different result. 

Can somebody help me with this? 

This is the simple piece of code I am running: 

import arcpy
import os

workspace = r'C:\Users\paperspace\Google Drive\QTA\19010_PDX Zoning\PDXZoning\PDXZoning.gdb'
outWorkspace = r'C:\Users\paperspace\Google Drive\QTA\19010_PDX Zoning\PDXZoning\PDXZoned.gdb'
arcpy.env.workspace = workspace

# set the target address taxlot
# consider capitalizing whatever user inputs
address = "1519 SE MORRISON ST"
field = arcpy.AddFieldDelimiters('taxlots', 'SITEADDR')
query = field + " LIKE '%" + address + "%'"
print(query)

# select the target taxlot
arcpy.SelectLayerByAttribute_management('taxlots', 'NEW_SELECTION', query)

# test output--you can remove this once done testing
outFC_temp = os.path.join(outWorkspace, 'tempARE09')
if arcpy.Exists(outFC_temp):
    arcpy.Delete_management(outFC_temp)
arcpy.CopyFeatures_management('taxlots', outFC_temp)

 When I copy and paste into Python Window, I get the result I am expecting, which is the selection of a specific taxlot in a cadastral map with the SQL query. 

When I run this same script in Pycharm, the script runs fine, but gives me a completely different result (a different taxlot, strangely). Other geoprocessing commands don't seem to do anything, either. 

I can't tell if I am passing the SQL query incorrectly, writing the output file incorrectly, or if I have incorrectly pointed to the correct Python interpreter. However, the script works fine in Python Window and 

This is my Pycharm interpreter, which I believe is pointing to the right spot: 

However, when I double click on the executable from explorer I get this: 

Which seems weird to me but everything else seems to run fine. 

Any help would be appreciated. 

Thanks,

0 Kudos
1 Solution

Accepted Solutions
JoshuaBixby
MVP Esteemed Contributor

Change the following two lines:

res = arcpy.SelectLayerByAttribute_management('taxlots', 'NEW_SELECTION', query)

and

arcpy.CopyFeatures_management(res, outFC_temp)

View solution in original post

10 Replies
DanPatterson_Retired
MVP Esteemed Contributor

There are many unclosed links relating to pycharm....

promising

Can ArcGIS Pro integrate with JetBrains Pycharm 

others

https://community.esri.com/search.jspa?q=pycharm&after=year

If you aren't stuck on pycharm

/blogs/dan_patterson/2018/12/13/spyder 

and when Pro 2.5 comes out, I have been using

/blogs/dan_patterson/2019/12/12/spyder-4-the-python-ide-for-science 

QuangTruong
New Contributor II

I'm not stuck on Pycharm, I just used it because it was recommended by the ESRI "Python for Everyone" course, and it had a well detailed installation setup to integrate with ArcGIS. I've followed the instructions on the link provided in your link, and it seems to be set up correctly, but the problem persists. 

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Regarding:

when I double click on the executable from explorer ....

Don't.  The ArcGIS Pro installer creates a shortcut for properly activating the conda environment.  Running the Python executable the way you describe will eventually run into issues.

QuangTruong
New Contributor II

I only did so at the recommendation of another discussion thread related to the same topic to troubleshoot. Thanks for the advice. 

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

When you run code in PyCharm, it runs it in a separate process so it is not aware of any layers or table views you have created in the application if it is open.  In the code snippet you provided, you are referencing a layer that you never create ('taxlots').  If the workspace is set to a folder or geodatabase that has a data set named 'taxlots', the Select Layer tool will work because it will take the data set instead of a layer, and it will create a new layer.  The newly created layer has a different name than the data set.  Given your code, I suspect it is simply copying the entire 'taxlots' data set when run via PyCharm.

0 Kudos
QuangTruong
New Contributor II

Strangely, what it's doing is selecting one (1) taxlot in the 'taxlots' feature class in the geodatabase, just not the one that I was hoping for with my SQL query! When I copy paste the exact same code in the Python window, it works exactly as expected. So why it would be different is confounding me. 

0 Kudos
QuangTruong
New Contributor II

Ok, Joshua, you were right. I found out that my featureclass was corrupted, so what I was trying to select for wasn't possible. However, it is now doing what Joshua suspected, which is just copying the entire 'taxlots' dataset. How do I get it to only copy a selection? 

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Change the following two lines:

res = arcpy.SelectLayerByAttribute_management('taxlots', 'NEW_SELECTION', query)

and

arcpy.CopyFeatures_management(res, outFC_temp)
QuangTruong
New Contributor II

Ah, yes, that makes sense. I knew it was something simple. Thank you. 

0 Kudos