[GIS] Using arcpy from the command line on ArcGIS Linux Server 10.1

arcgis-10.1arcgis-serverarcpyerror-000732linux

We installed ArcGIS SERVER (not Desktop!) 10.1 on a RHEL (Red Hat) machine. ArcGIS Sever is supported for Linux.

However, it appears they don't make it easy to script in python, from the command line, making use of the arcpy module.

There are many things awkward about this. The arcpy library that they use is actually for Windows, with some external patching. In fact, it comes with its own implementation of python. So pathnames look a little weird. You have to be the arcgis user to even get access to python or arcpy. So here is how my code looks:

import arcpy
"""                                                                                                                                                            
From http://resources.arcgis.com/en/help/main/10.1/index.html#//0155000005mm000000

The Python libraries installed by an ArcGIS Linux Server installation is Windows 64-bit Python. Therefor, stand-alone Python scripts should always use the Win\
dows path separator (\) when accessing data and other Python modules. Also, be aware that there is a Z:\ which is mapped to the Linux systems' /. Within a Pyt\
hon script you can access data and other Python modules using Z:\ mapping. For example, data in /myuser/myproject/data can be accessed as z:\myuser\myproject\\
data.                                                                                                                                                          
This does not apply to Python scripts you publish from ArcGIS for Desktop because the publishing process converts all paths to the correct format.             

"""
"""                                                                                                                                                            

I'm using:                                                                                                                                                     

sudo su arcgis                                                                                                                                                 

/opt/arcgis/server/tools/python ./arcpytest.py                                                                                                                 


"""


dataDir=r"z:home\arcpy-tmp"+"\\"
print dataDir

shp='tgr39105lkA.shp'
arcpy.FeatureToLine_management(dataDir+shp, 'temp_links')

What I get back is:

$ /opt/arcgis/server/tools/python ./arcpytest-1.py
z:home\arcpy-tmp\
Traceback (most recent call last):
  File "./arcpytest-1.py", line 28, in <module>
    arcpy.FeatureToLine_management(dataDir+shp, 'temp_links')
  File "C:\Program Files\ArcGIS\Server\arcpy\arcpy\management.py", line 2343, in FeatureToLine
    raise e
arcgisscripting.ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000732: Input Features: Dataset z:home\arcpy-tmp\tgr39105lkA.shp does not exist or is not supported
Failed to execute (FeatureToLine).

Has anyone tried to use arcpy in this direct way? It's a REALLY powerful server, so we put ArcGIS server on it, but want to use ArcPy for programming.

Best Answer

I haven't had too much trouble using arcpy on linux so far.

I installed arcgis into the arc users home (this was the default location, and it was recommended one install as the arc user). As mentioned above in .../arcgis/server/tools you will find a 'python' executable. If you look at that file, you'll see how to run arc's concept of python on linux.

To run arcpy scripts on my RHEL 6 servers this is what I do:

In ~/bin:

ln -s /path/to/myarcpyexecutable arcpython

I found I didn't need much of what was in the original bash script, mine looks like this:

    #!/bin/bash                                                                          
    installDir=/home/arc/arcgis/server                                                   
    arcenv=$installDir/framework/etc/arcenv                                              
    if [ -f $arcenv ]; then                                                              
       . $arcenv                                                                          
       if [ "x$DISPLAY" = "x" ]; then                                                     
         . $installDir/framework/runtime/xvfb/init_Xvfb.sh                                
         StartXvfb > /dev/null 2>&1                                                       
       fi                                                                                 
       wine "C:/Python27/ArcGISx6410.1/python.exe" $*                                     
     else                                                                     
   echo "Unable to set up environment.  Cannot find $arcenv"                          
 fi       

Any python script I want to run I put

#!/home/arc/bin/arcpython

at the top and then can run it like so:

./sync_replicas.py

/home/arc/bin is first on my path, so I just use 'arcpython script.py' to run anything else. You could link 'python' to the arc version of python if you wanted, but I have that running python 2.7, and generally use virtualenvs for all my non-arcpy python stuff.

This is working quite well so far for me. I don't need to use sudo, and most things work as you'd expect (linux paths for example), the main annoyance is that I could never get it to work with ipython.

It is a bit awkward, but nothing compared to having to work on windows!