Select to view content in your preferred language

Command work in Python Window but not in Python Toolbox

339
2
12-12-2022 07:53 PM
Leooo
by
New Contributor

Just a simple command:

arcpy.analysis.Buffer('Target_Layer', 'C:/Buffer/Buffer.gdb/buffer', '100 Feet', 'FULL', 'ROUND', 'NONE')

It works in the Python Window, created a new layer in the gdb file.  However, when I change this to Python Tools:

def execute(self, params, messages):
arcpy.analysis.Buffer('Target_Layer', 'C:/Buffer/Buffer.gdb/buffer', '100 Feet', 'FULL', 'ROUND', 'NONE')

It shows 'Tool completed', but nothing happen.  However, when I change the output to 'C:/Buffer/buffer' only, it can create a .shp file named 'buffer'.  What is the different between the script in Python Window and Python Toolbox.

Also, when I want to add the output layer in the gdb file to Contents, I type in the Python Window:

arcpy.management.MakeFeatureLayer('buffer', 'buffer_lyr')

It works.  However, when I put it into Python Toolbox:

def execute(self, params, messages):
arcpy.management.MakeFeatureLayer('buffer', 'buffer_lyr')

It shows:

Traceback (most recent call last):
File "<string>", line 80, in execute

I found some solution said that the layer in gdb need to change to .lyr file and then use the .addLayer() function.  Can I have a way need not to create a .lyr file just like I did in the Python Window.

 

 

Tags (2)
0 Kudos
2 Replies
DanPatterson
MVP Esteemed Contributor

'C:/Buffer/buffer'  

will assume you want to create the output in a folder since the workspace isn't a geodatabase like

'C:/Buffer/buffer.gdb'


... sort of retired...
0 Kudos
Leooo
by
New Contributor

I find the solution:

The new feature class is created and saved to the gdb file, but it is hidden in catalog and a refresh is needed to show it.

I can directly add the feature class to map, even it is hidden in the catalog.:

Buffer_lyr = 'C:/Buffer/Buffer.gdb/buffer"'
aprx = arcpy.mp.ArcGISProject('CURRENT')
aprxMap = aprx.listMaps()[0] 
aprxMap.addDataFromPath(Buffer_lyr)

 

0 Kudos