How to open external Python script from ArcMap PyWindow

1904
5
01-04-2017 08:19 AM
Lake_Worth_BeachAdmin
Regular Contributor

   Hello all,

I have this python script that I execute inside the ArcMap Python Window and it does an array of spatial analysis processes to get me to my desired final mxd content. I then have another script that overrides my existing feature classes on my AGOL account with the current layers in the open mxd. : Click Here (it is necessary that this script is ran outside of ArcMap via the Python shell. 

The only issue is that after I run my first script in ArcMap I then have to open my python shell outside of ArcMap in order to run the second script. I am trying to find a way to execute the 2nd script from the first script without manually leaving arcmap to open my python shell and load the script. But keep in mind my first script is being run from the ArcMap Pywindow. SO I cannot figure out a way to escape that... hope this question makes sense and is this even possible. I am basically trying to eliminate all user-interaction to completely automate this task once I load the first script into ArcMap. 

Please feel free to ask if this does not make sense. 

0 Kudos
5 Replies
JakeSkinner
Esri Esteemed Contributor

Hi Joseph,

You could add the AGOL update script to your current script that you run in ArcMap.  All you will need to do is update the if __name__ == "__main__" section.  Ex:

if __name__ == "__main__":
    #
    # start

    print("Starting Feature Service publish process")

    # Find and gather settings from the ini file
    localPath = sys.path[0]

    # AGOL Credentials
    inputUsername = 'agolUser'
    inputPswd = 'gis12345'

    # FS values
    MXD = r"C:\temp\Airports.mxd"
    serviceName = "Airports"
    folderName = ''
    tags = 'Airports'
    summary = 'Airports Summary'
    maxRecords = '1000'

    # Share FS to: everyone, org, groups
    shared = 'true'
    everyone = 'false'
    orgs = 'false'
    groups = ''  # Groups are by ID. Multiple groups comma separated


    # create a temp directory under the script
    tempDir = os.path.join(localPath, "tempDir")
    if not os.path.isdir(tempDir):
        os.mkdir(tempDir)
    finalSD = os.path.join(tempDir, serviceName + ".sd")

    # initialize AGOLHandler class
    agol = AGOLHandler(inputUsername, inputPswd, serviceName)

    # Turn map document into .SD file for uploading
    makeSD(MXD, serviceName, tempDir, finalSD, maxRecords, tags, summary)

    # overwrite the existing .SD on arcgis.com
    if agol.upload(finalSD, tags, summary):

        # publish the sd which was just uploaded
        fsID = agol.publish()

        # share the item
        if ast.literal_eval(shared):
            agol.enableSharing(fsID, everyone, orgs, groups)

        print("\nfinished.")‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
Lake_Worth_BeachAdmin
Regular Contributor

I am not really sure where to edit my code, your example looks way different than the original script I found on github. 

0 Kudos
Lake_Worth_BeachAdmin
Regular Contributor

the issue is the hosted feature service script will not work when its ran inside arcmap it must be ran outside (via python shell). This is what the ESRI support told me. So I need a way to call the script into shell from inside arcmap... so inside arc map write code that will open python shell and find my script then run. 

I hope this makes sense. 

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

Not sure if I follow that ... You aren't supposed to have arcmap open at all, just open your python IDE an launch the script from there... assuming you installed a second party IDE like pycharm, spyder, pythonwin etc or IDLE. You cant use the python IDE built into arcmap to do this.  It isn't a separate IDE but is tied to arcmap

0 Kudos
Lake_Worth_BeachAdmin
Regular Contributor

No I can have arcmap open but when I execute the script from arcmap it does not work. When I execute the script from either cmd.  or python IDLE it works.

When I spoke to the ESRI tech who supported this script he informed me the script was design to be ran outside of ArcMap. 

0 Kudos