arcpy.AddJoin Not Working In ArcGIS Pro Script

3160
10
10-25-2019 06:53 AM
KennethGibson
New Contributor II

I am trying to join a layer in an ArcGIS Pro map to a table using a scripting tool. The layer is called 'Heights', and is being joined to a table called 'Names'. The Python code below is working when run from the Python window within ArcGIS Pro. However, when I try to run it as a script from the Catalog Pane then the join does not happen, even though the geoprocessing detail does not throw up any error messages. Am I missing a step or does this just not work in ArcGIS Pro?

import arcpy
#Reference map document from within ArcGIS Pro
aprx = arcpy.mp.ArcGISProject('CURRENT')
map = aprx.activeMap
# Local variables:
Heights = "Heights"
toTable = r"C:\#workspace\Names"
# Process: Add Join
arcpy.AddJoin_management(Heights, 'LINK_ID', toTable, 'AI_LINK_ID', 'KEEP_ALL')‍‍‍‍‍‍‍‍‍‍‍
10 Replies
DanPatterson_Retired
MVP Emeritus

If you are running as a separate script the concept of "Current" isn't known, it is only valid in Pro's python window.

Have you tried replacing Current with the path to the aprx and setting arcpy's env to it so that layers can be referenced without full paths (eg Heights)

Also   /blogs/dan_patterson/2016/08/14/script-formatting 

would help people reference line numbers, if the above is indeed a script

0 Kudos
KennethGibson
New Contributor II

HI Dan

Thanks for the advice about the code rendering. I have now fixed this is the post.

I am bit confused by the 'Current' not being known as I am able to switch the Heights layer off in the map simply by adding the following lines to the script:

lyrs = map.listLayers(World5m_Heights)
lyrs[0].visible = False‍‍‍‍

The full script then becomes:

import arcpy
#Reference map document from within ArcGIS Pro
aprx = arcpy.mp.ArcGISProject('Current')
map = aprx.activeMap
# Local variables:
Heights = "Heights"
toTable = r"C:\#workspace\Names"
# Toggle Heights layer off
lyrs = map.listLayers(Heights)
lyrs[0].visible = False
# Process: Add Join
arcpy.AddJoin_management(Heights, 'LINK_ID', toTable, 'AI_LINK_ID', 'KEEP_ALL')‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

This would indicate that the layer is being referenced and manipulated OK using the script. However, the join does not work. If I change the parameters in the join to...

arcpy.AddJoin_management(lyrs[0], 'LINK_ID', toTable, 'AI_LINK_ID', 'KEEP_ALL')‍‍

...it still doesn't work.

0 Kudos
DanPatterson_Retired
MVP Emeritus

Kenneth, Are you running it in the python window within Pro? or from a separate python IDE?

Add Join—Data Management toolbox | ArcGIS Desktop 

on a lark, pass the full path to both to test whether something is wrong with one of the two tables

If the input is a feature class or dataset path, this tool will automatically create and return a new layer with the result of the tool applied.
0 Kudos
KennethGibson
New Contributor II

I'm running the script from a toolbox within the Catalog Pane of an ArcGIS Pro project. This has to run within the project and on layers in the map because the full script will actually loop through all the layers in the map in order to join each onel to the Names table so that the map labels can reference one of the columns in the joined table. This is a script that runs perfectly well in ArcMap using the same method (i.e. a toolbox script in ArcCatalog from within ArcMap) and all I am trying to do is update the script so that it works within ArcGIS Pro.

If I do as you suggest to the actual feature classes rather than the map layers then the join will likely work OK but there will be no way to actually view if it is successful or not because the join will be in a temporary state in order to allow processes like Calculate Fields to run. Instead, I need this to work on the layers within the ArcGIS Pro map. As mentioned it seems that I can manipulate the layers OK (tested using the lyr.visible = False code) but I can't get the join to work.

Since the code works within the Python window in ArcGIS Pro then I'm confident there is nothing wrong with the actual tables.

0 Kudos
DanPatterson_Retired
MVP Emeritus

The last thing I can think of is that if the table is open when the join is being performed, it doesn't show, but needs to be closed and reopened.  I am also a bit confused about the reference to the totable which appears to be on disk and not a reference to a table view.

Maybe someone else can see what I am overlooking

0 Kudos
KennethGibson
New Contributor II

Yes, I had thought to reload the table but to no avail I'm afraid. I've also tried to create a view of the Names table and then joining to that. The following code is added with some fieldinfo:

arcpy.MakeTableView_management(toTable, 'Table_View', '', '', fieldinfo)

That then alters the AddJoin command to:

arcpy.AddJoin_management(Heights, 'LINK_ID', 'Table_View', 'AI_LINK_ID', 'KEEP_ALL')‍‍

It sounds like we may not be able to figure this out. If anyone is able to confirm that this indeed does not work in ArcGIS Pro then I will raise it as a bug or future enhancement. Thanks for your help.

DanPatterson_Retired
MVP Emeritus

Esri Support Search-Results 

nothing obvious... except the warning that the inputs must be layers or table views

0 Kudos
LouisHill1
Occasional Contributor II

I'm having the same issue.  I'm trying to join a table (that exists inside a FGDB) to a feature class (also in the same FGDB) from IDLE.  The Python join command  works in the ArcPro Python window, but does not execute correctly when run as a standalone script from the IDLE.

0 Kudos
AndyWells
Occasional Contributor II

I am having the same problem. Script works in the ArcGIS Pro python command window but will not run as a standalone script in a toolbox. Data and table both in a local FGDB. Would be nice to find a way around this. Creating in-memory layers has not worked so far. 

0 Kudos