arcpy.mp.LayerFile(layer_filename) hangs

741
2
09-30-2022 11:19 AM
banders
New Contributor

I am trying to upgrade an arcpy script written for ArcGIS Desktop to support ArcGIS Pro (2.9.4). 

The script needs to open an old-style .lyr file (not .lyrx), and then perform various operations on layers in that file.

For ArcGIS Desktop I did this:

  lyr_file = arcpy.mapping.Layer(lyr_filename)

The above command worked as expected in arcpy for ArcGIS Desktop.

With ArcGIS Pro I am doing this:

  lyr_file = arcpy.mp.LayerFile(lyr_filename)

The call to arcpy.mp.LayerFile(..) hangs and seems to never return.  It's unclear to me why the function hangs.  One guess is that it's related to the fact that the .lyr file I am trying to access does not have data source connection info embedded.  (One of my goals is to set connection information after I open the file.)  Missing connection info wasn't a problem in ArcGIS Desktop, but perhaps it's a problem ArcGIS Pro??  

Sample script:

 

 

 

 

import arcpy

layer_filename = r"..." #path to .lyr file without connection info embedded
print("the following function call never returns")
lyr_file = arcpy.mp.LayerFile(layer_filename)
print("this line is never reached")

 

 

 

 

 

Any suggestions would be appreciated!

Tags (3)
0 Kudos
2 Replies
banders
New Contributor

A follow-up.  I have determined why calls to the following function sometimes hang indefinitely

arcpy.mp.LayerFile(layer_filename)

When the layers inside the given .lyr file have embedded database connection (Oracle in my case), but without embedded credentials, the above function call will hang.  A partial workaround is to set the following 

arcpy.env.workspace = path_to_sde_file

where the given .sde file path refers to a .sde file that has credentials embedded and which has an "instance" property that exactly matches what's in the .lyr file.  If the "instance" string is formatted even slightly differently, arcpy.mp.LayerFile(..) will hang.

Is anyone aware of a workaround so I can programmatically open .lyr files without being asked for db credentials?  (This was possible in arcpy for ArcGIS Desktop, but seems not currently possible in arcpy for ArcGIS Pro.)

0 Kudos
andy_gio
New Contributor

I want to use a view from an Enterprise Postgres db.
From ArcGis Pro 3.1 it'a all ok:
- the ".sde" is ok.
- I can import the "Database Feature Class" (that is my view in Postgres) to a new Map
with "Make Query Layer" tool.

Now I want to reproduce the same with arcpy :

arcpy.env.workspace = <sde_connection>

arcpy.MakeQueryLayer_management(
	input_database = <sde_connection>,
	out_layer_name = <out_lyr>,
	query = "select * from " + <pg_view_name>,
	oid_fields = [<oidfield_name>] ,
	shape_type = "POLYGON",
	srid = <srid_view>)

arcpy.SaveToLayerFile_management(
	in_layer = <out_lyr>,
	out_layer = <folder_proj> + <out_lyr> + ".lirx")

newLyr = arcpy.mp.LayerFile(
    layer_file_path = <folder_proj> + <out_lyr> + ".lirx")

<map_obj>.addLayer(
    add_layer_or_layerfile = newLyr)

This is the Python error:

Spoiler
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\_mp.py", line 1304, in __init__
self._arc_object = arcgisscripting._mapping.LayerFile(*gp_fixargs((layer_file_path,), True))
ValueError: <absolute_lirx_path>

In ArcGis Pro 3.1 I can add the .lirx file (SaveToLayerFile result)
to a new map (so the .lirx file seems to be correct).

What wrong with the code?

0 Kudos