Select to view content in your preferred language

Getting started with Python/ArcPy on ArcGIS Server

1038
3
Jump to solution
05-02-2023 09:12 AM
DonMorrison1
Occasional Contributor III

I have a Python3/ArcPy script that must be moved from Windows to be run on a Linux system. From what I understand the only option is to run it on ArcGIS Server.  So I had a system set with with Ubuntu and Arcgis Server 11 (putty/ssh access only). Now I'm trying to figure out how to run my script.  Reading the documentation a number of questions came up.

  1. I thought a good first step would be to start an interactive Python session from the command line, but I can't find where the interpreter is installed. How can I do that?
  2. Must I install conda and create and activate a new environment, or is there already a base Python environment created in the installation? 
  3. Must I install arcgis-server-py3?
  4. Does all of this really have to be done as the user that installed the ArcGIS Server? I tried to run <arcgis_server_installation_directory>/arcgis/server/tools/python3 and it failed for this reason
  5. Would it be better to use a python 3.8 environment which is already installed on this system instead of the one installed with ArcGIS Server?
Tags (3)
0 Kudos
1 Solution

Accepted Solutions
DonMorrison1
Occasional Contributor III

Thanks @Brian_Wilson.  I only have a single script that I have to run and I don't foresee having to install additional packages so I was hoping to avoid everything related to conda (not sure if that is even possible).  

That said, I was able to get my script running. Here is how I did it.

  1. I had the administrator (the guy that did the install) give up full rwx access to all subdirectories of /opt/arcgis/server
  2. Made a copy of /opt/arcgis/server/tools/python (called it python_dam)
    • Removed the code that terminates the script if not being called by the user that did the install
    • Edited the line where wine64 invokes python to call my module
  3. cd to /opt/arcgis/server/tools
  4. ./python_dam

This is what the modified file looks like

#!/bin/bash

installDir=$(cd $(dirname $0)/.. && pwd);export installDir
installer=$(stat -c %U $installDir)
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
    # don't preload libjsig.so for standalone app
    unset LD_PRELOAD
    export CONDA_PREFIX="C:\Program Files\ArcGIS\Server\framework\runtime\ArcGIS\bin\Python\envs\arcgispro-py3"
    export CONDA_DEFAULT_ENV="$CONDA_PREFIX"
    wine64 "$CONDA_PREFIX\python.exe" "/home/dmorrison/cw/test_linkage_mapper.py"
else
    echo "Unable to set up environment.  Cannot find $arcenv"
fi

 

 

View solution in original post

0 Kudos
3 Replies
Brian_Wilson
Regular Contributor II

I have not used the Linux version of Server in a few years now, they might have a group (ie in /etc/group) so if you don't want to run as "root" or user "arcgis" you might get better access if you add your normal user to that group. I think some Esri Windows programmers read a book and then declare themselves Linux programmers but they might have learned more since then. (I did the opposite, I read a Windows book once... 🙂

I am stuck running Esri software on Windows only but rely extensively on Conda in Debian for related work.

Running as myself I would use the Esri copy of Conda to clone the Python that Esri installed and then use the clone. It might endlessly battle you over authorization when you "import arcpy" and you will give up and run as "arcgis" and say "this is really not so bad."

I like to have a separate Conda environment so I can add packages to it and have a "fall back" position instead of contaminating the Esri install with packages that might trigger dependencies and break their version.

I also tend to set up special environments for different projects, like, one for Flask apps and one for GDAL and one just for ArcGIS API and one for automated COVID19 data collection (oh halcyon days of yore).

When I want to run a script standalone (for example from crontab) I put a shebang line in it instead of messing with "conda activate" in wrapper scripts. For example, this runs once a day to check on links and files for me

#!/usr/bin/env -S conda run -n arcgis_tools --no-capture-output python
# Remember to set credentials etc in the .conda/envs/arcgis_tools/etc/conda/activate.d/ files.
"""
    Check for missing taxmap PDF files.
    Use the -v option to get output even when nothing is missing.
    Default is to be quiet for running from crontab.

    This script is part of arcgis_tools.
"""

It's using the 'env' tool to find my copy of conda and then using it to run the python in the file using the arcgis_tools conda environment. I can move Conda or put it on a different machine and as long as I have it in the PATH it will still run the correct python.

This depends on you setting your PATH to include "conda". I do that manually by adding CONDA_PATH="something" and PATH="$PATH:$CONDA_PATH" in my .bashrc file. Conda can help you by making you run "conda init" which will edit your .bashrc. You can try that. After you figure out where your "conda" lives so you can run it. 🙂 I prefer to simply edit the .bashrc file myself.

 

0 Kudos
DonMorrison1
Occasional Contributor III

Thanks @Brian_Wilson.  I only have a single script that I have to run and I don't foresee having to install additional packages so I was hoping to avoid everything related to conda (not sure if that is even possible).  

That said, I was able to get my script running. Here is how I did it.

  1. I had the administrator (the guy that did the install) give up full rwx access to all subdirectories of /opt/arcgis/server
  2. Made a copy of /opt/arcgis/server/tools/python (called it python_dam)
    • Removed the code that terminates the script if not being called by the user that did the install
    • Edited the line where wine64 invokes python to call my module
  3. cd to /opt/arcgis/server/tools
  4. ./python_dam

This is what the modified file looks like

#!/bin/bash

installDir=$(cd $(dirname $0)/.. && pwd);export installDir
installer=$(stat -c %U $installDir)
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
    # don't preload libjsig.so for standalone app
    unset LD_PRELOAD
    export CONDA_PREFIX="C:\Program Files\ArcGIS\Server\framework\runtime\ArcGIS\bin\Python\envs\arcgispro-py3"
    export CONDA_DEFAULT_ENV="$CONDA_PREFIX"
    wine64 "$CONDA_PREFIX\python.exe" "/home/dmorrison/cw/test_linkage_mapper.py"
else
    echo "Unable to set up environment.  Cannot find $arcenv"
fi

 

 

0 Kudos
puzzlrdev3
New Contributor

Hello, 

What's the purpose of this part of the code?

/home/dmorrison/cw/test_linkage_mapper.py

 

0 Kudos