4

I've got a few on demand scripts via the web that run ArcGIS in the background via Arcpy. Now, every one of these scripts takes a solid 15-30 seconds to run, which I find okay. After doing some trial and debugging on it, I find that 90% of the time spend running the script is taken importing Arcpy.

I've broken down a script I wrote here

  1. Import standard python modules like: os, cgi, time (less than 1 second)
  2. Grab web query and disseminate into a dictionary (less than 1 second)
  3. Import ArcPy (15 seconds)
  4. Import utm (less than 1 second)
  5. Convert lat long to utm (less than 1 second)
  6. Arcpy insert cursor, insert a new point into a feature class (less than 1 second)
  7. Arcpy update cursor, add attributes to a the new point (less than 1 second)
  8. Print web page based on outcome of query and Arcpy cursors (less than 1 second)

This whole script take about 16-20 seconds to run and most of that is spent importing Arcpy. The PC I am using is good, so what is the issue?

Is Arcpy so big that it actually takes this much time to import? Is there any other reason why the import takes so long? Also is there any work around to speed this up?

5
  • 4
    Related: gis.stackexchange.com/a/12458/65 May 19, 2016 at 17:23
  • 4
    Another Related: gis.stackexchange.com/questions/68025/…
    – Goldring
    May 19, 2016 at 18:33
  • 2
    What version of arcgis/arcpy are you running? I have 8-9 s import time on a very old laptop running 10.3.
    – Aaron
    May 19, 2016 at 21:59
  • 3
    Short answer: Yes it really takes that long the first time, and there isn't anything you can do about it.
    – Vince
    May 19, 2016 at 22:22
  • 2
    Set up a server+client via socket -- the server handles all the imports and heavy code while the client merely parses (+ pickles if need be) the web query before passing to the server. See SO.
    – Paul
    May 20, 2016 at 0:36

1 Answer 1

5

After a long time of wondering this myself, I looked into it:
When first imported 'arcpy' needs to do some black-box validation stuff that checks if your license is active among other things (like loading DLLs). This process takes the longest (I timed ~3.4 seconds using instructions from this blog).

This long process can be emulated by the following code:
>>> import arcgisscripting as arc # assuming ESRI's /bin/ folder is on your %PATH%
>>> gpo = arc.create(10.0) # creates the black-box validation object that is used by everything in arcpy.

This process is required for arcpy to function and sadly can't go any faster.

2
  • 2
    Is there a way to have it pre validated? Like have arcmap running or something like that
    – Mojimi
    Oct 25, 2016 at 18:14
  • 2
    ArcMap/Catalog's Python Window (in the Geoprocessing menu) will pre-load arcpy, but I find it unstable and don't use it often. I've learned to expect the lag, so I'll sometimes I'll import arcpy later in the script. Nov 14, 2016 at 15:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.