[GIS] Running ArcPy script from ArcObjects

arcgis-desktoparcgis-enginearcmaparcobjectsarcpy

the standard method requires to run ArcPy script in command line, i've struggled with it to run but without a result, i can import arcpy, set the workspace to the geodatabase path and describe the env variable using arcpy.Describe(arcpy.env.workspace).name it gives me the geodatabase name, but when i try to get featureclasses using arcpy.ListFeatureClasses() i get [], i have made sure that all the environement variables in PATH and PYTHONPATH are ok .

i would like to know if there's an alternative way to run ArcPy script, like it could be run in ArcMap console, is there a way to send scripts to ArcMap console using ArcObjects, what's the difference between ArcMap console and standard command line console?

so any way to run an ArcPy script using ArcObjects 10 or Arcgis Engine 10 will be very welcome.

Best Answer

There may be several options. If you package your script into a toolbox you could reference your toolbox (and tool) via ArcObjects as such:

 IGeoProcessor2 gp = new GeoProcessorClass();
 gp.AddToolbox(@"C:\YourPath\YourToolbox.tbx");
 parameters.Add(@"C:\YourPath\ParamsIfYouHaveThem.gdb\ParamFC");
 gp.Execute("NameOfYourToolInsideReferencedToolbox", parameters, null);

Read more on this method here: http://help.arcgis.com/en/sdk/10.0/arcobjects_net/conceptualhelp/index.html#executingCustomTool

Or you could try this route:

System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo.FileName = @"C:\path\toYour\script.py";
proc.StartInfo.UseShellExecute = true;
proc.Start()

which is mentioned here: http://forums.esri.com/Thread.asp?c=93&f=993&t=276632

Related Question