Standard python module ImportError in Geoprocessing Service

4858
7
Jump to solution
01-12-2015 03:30 PM
ClintDow
Occasional Contributor

Hello,

I have recently created a geoprocessing service on ArcGIS server (10.3). When attempting to run this service, it fails almost immediately. Upon checking in the log files, I am getting an ImportError, specifically:

Traceback (most recent call last): File "B:\arcgisserver\directories\arcgissystem\arcgisinput\ConfirmationSampling.GPServer\extracted\v101\webapp\confirmation_sampling.py", line 63, in import json ImportError: No module named json Failed to execute (ConfirmationSampling). Failed to execute (ConfirmationSampling).

However, json is a standard python module and should be included in python without having to install a module, to my knowledge. I have tried to import it in the interpreters installed on the system and they have no problem doing so. Is there some python installation that ArcGIS server uses that I have to explicitly install this module for? If so can someone please tell me where it is, or link me to relevant documentation on this topic? I have been unsuccessful finding anything so far.

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
ClintDow
Occasional Contributor

Well, I managed to track down a little documentation on this. ArcGIS Help 10.1 "Importing other python modules"

Unfortunately the methods suggested in the documentation didn't work. Long story short I had zero success with the recommended methods of using sys.path.append, or the consolidation method. I eventually had to copy my own modules into the site-packages folder for the ArcGISx6410.3 folder within Python27. Hopefully if anyone else has this issue this might help them.

View solution in original post

0 Kudos
7 Replies
DanPatterson_Retired
MVP Emeritus

you still have to import it within your script, just like numpy, or string or os or sys

>>> import json
>>> dir(json)
['JSONDecoder', 'JSONEncoder', '__all__', '__author__', '__builtins__', '__doc__', '__file__', 
'__name__', '__package__', '__path__', '__version__', '_default_decoder', '_default_encoder', 'decoder',
 'dump', 'dumps', 'encoder', 'load', 'loads', 'scanner']
0 Kudos
ClintDow
Occasional Contributor

Hi Dan,

I am doing that. In fact the traceback I posted contains 'import json' as the offending line of code, and ImportError itself is only thrown when an import or from ... import statement fails. If I didn't import the module and tried to use it, then it would raise a NameError.

The script runs successfully in ArcMap, and satisfies multiple unit and acceptance tests run via the command line, so I am sure the code itself is not problematic. Its just this one import statement while run as a geoprocessing service that is the issue.

0 Kudos
DanPatterson_Retired
MVP Emeritus

strange then...does it do this with other python modules or just json?

ClintDow
Occasional Contributor

Other modules import successfully, such as math, datetime and so forth. It seems just to be json that is problematic. I have found that when checking sys.executable from within a geoprocessing service, it returns the path to ArcSOC.exe, so they have baked an interpreter via COM into that executable I believe. As such I'm not sure where I would even install a module via pip, or if its possible.

0 Kudos
DanPatterson_Retired
MVP Emeritus

That is not good.  Keep us posted

0 Kudos
ClintDow
Occasional Contributor

Well, I managed to track down a little documentation on this. ArcGIS Help 10.1 "Importing other python modules"

Unfortunately the methods suggested in the documentation didn't work. Long story short I had zero success with the recommended methods of using sys.path.append, or the consolidation method. I eventually had to copy my own modules into the site-packages folder for the ArcGISx6410.3 folder within Python27. Hopefully if anyone else has this issue this might help them.

0 Kudos
JonMorris2
Occasional Contributor II

I've just run into this problem, so it's good to know (not really, but you know what I mean) that someone else has come across it. Using sys.path.append did work for me, but it meant that all dependencies (including lots of python builtins) and files in the script folder were copied to the package folder on the server.

This was even the case when putting the imported module in a subfolder on its own and appending the path with

sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'utils'))

Looks like this needs to be raised as a bug, because the 10.3 help still says that the script should be copied if it is in the same folder.

0 Kudos