Making .exe from modelbuilder and python script

1560
6
06-02-2014 05:00 AM
RicoKock
New Contributor
Hi

Hopefully someone with much more experience in Python can help me.
Ive got a model which i created in modelbuilder, and in the model there is a Python script( i did NOT do the coding) found it in my company's "Archive".

What i want to know if there is any way i can make the model a stand alone .exe? at this moment i need to be in ArcMap10 to run my model, reason for this is that i want to implement my tool into a InfoPath form i created.

So is there any way this is possible?
Please tell me if its possible that i can give more info about the tool and what it does

Thank You
Tags (2)
0 Kudos
6 Replies
IanMurray
Frequent Contributor
You should just be able to convert the entire model to a python script which can be run outside of ArcGIS (you would still have to run it on a computer with ArcGIS licensing).

In the model you simply need to go to Model>export>to python script.

From there you probably could integrate it into whatever you need to make it an .exe file.
0 Kudos
RicoKock
New Contributor
You should just be able to convert the entire model to a python script which can be run outside of ArcGIS (you would still have to run it on a computer with ArcGIS licensing).

In the model you simply need to go to Model>export>to python script.

From there you probably could integrate it into whatever you need to make it an .exe file.


Thank you for the reply, that was the first thing did before coming to the Forums, the Script gives me an error, and me totally new to Python(meaning 0 experience) did not know what to do with the error [ATTACH=CONFIG]34254[/ATTACH].

I know its probably a small error, wanting to "impress" my manager by solving this huge problem we have in the company, my tool certainly resolves our issue but without it being an .exe doesnt really do my tool any justice.

Thank you again
0 Kudos
IanMurray
Frequent Contributor
ah, it seems the python script you included in the model is probably from arcgis 9.3 or prior, which used different syntax for certain tools then what is being used for 10.0 to current.  the gp you see is the older version syntax they used. 

If you could post the python script that was included in the model, we could probably help you debug or update it so it can run for 10.0 - 10.2
0 Kudos
RicoKock
New Contributor
ah, it seems the python script you included in the model is probably from arcgis 9.3 or prior, which used different syntax for certain tools then what is being used for 10.0 to current.  the gp you see is the older version syntax they used. 

If you could post the python script that was included in the model, we could probably help you debug or update it so it can run for 10.0 - 10.2


Hi Thank You so much for reply and helping me, just to add the model runs completely smooth and how its intended to do, in ArcMap 10.

But here is the script hopefully u guys can help me.

import os, sys, random, arcgisscripting
try:
    gp = arcgisscripting.create()
    gp.overwriteoutput = 1

    # Specify input featureclass, output *.lyr file and the percentage of
    # random points to return. Set these parameters in ArcToolbox as shown.
    inputFC = sys.argv[1] # Feature Class or Feature Layer
    outputLyr = sys.argv[2] # Layer File
    inpct = sys.argv[3] # Long

    # Ensure that the input percentage is between 1 and 100%
    inpct = min(int(inpct),100)
    inpct = max(int(inpct),1)

    # Work out how many features to select
    inputDirname = os.path.dirname(inputFC)
    inputBasename = os.path.basename(inputFC)

    gp.workspace = inputDirname
    desc = gp.describe(inputFC)
    totpnts = gp.getcount(inputFC)
    numValues = int(round(totpnts * float(inpct) / 100.0))
    gp.addmessage("Selecting " + str(numValues) + " random features")

    # Generate a list of all features, and select randomly from this
    inList = []
    randomList = []
    fldname = desc.OIDFieldName
    rows = gp.SearchCursor(inputFC)
    row = rows.next()
    gp.addmessage ("Loading all IDs into a list")
    while row:
        id = row.GetValue(fldname)
        inList.append(id)
        row = rows.next()

    selpnts = 0
    gp.addmessage("Creating the list of randomly selected features")
    while len(randomList) < numValues:
        selpnts += 1
        selItem = random.choice(inList)
        randomList.append(selItem)
        inList.remove(selItem)

    # Select features whose OID value occurs in the random list, generate
    # a *.lyr file from this selection. (Leading and trailing [ and ] marks
    # need to be removed from the list object)
    theLen = len(str(randomList))
    sqlexp = '"' + fldname + '"' + " in " + "(" + str(randomList)[1:theLen - 1] + ")"
    selectionLyr = inputBasename + " selection"
    gp.MakeFeatureLayer_management(inputFC, selectionLyr, sqlexp)
    gp.SaveToLayerFile_management(selectionLyr, outputLyr)

    gp.addmessage("\nOutput layer " + outputLyr + " contains features randomly selected from " + inputBasename + "\n")
except:
    gp.adderror("Error running script. Try specifying the full path to the input layer")

# END OF FILE
0 Kudos
JamesCrandall
MVP Frequent Contributor
Not sure if this is a good idea, but to possibly fix your immediate issue you can try to change this:

import os, sys, random, arcgisscripting



To this:


import os, sys, random, arcgisscripting as gp
0 Kudos
RicoKock
New Contributor
Hi is there any one that had a look at the code and how to debug it?

Please urgent!!!
0 Kudos