Select to view content in your preferred language

Running Python Code in VS Code

300
1
3 weeks ago
Liana_Roberson
New Contributor

Hi community, 

We are trying to run a python 3.1 application from VS Code to use general geoprocessing tools within ArcGIS Server to update some derived GIS layers that are shared as REST services to our users. The application has 3 modules: AutoLayerMaster.py (the main or control module), AutoLayerModules.py (the classes that handle the arcpy work), and CrabPyUtil.py (that handles the logging and the emailing).
The app uses the c:\....\ArcPro….\Python\envs\arcgispro-py3 environment. The general arcpy tools are copy, calculate, make route event layer, export as feature layer, delete, and append.


The script runs fine in the following circumstances:
1. Run from VS Code
2. Run from a user invoking a ps1 script
3. Run directly from task scheduler as a user
4. Run from task scheduler as a user via a ps1 script


The script fails any time it is run from task scheduler where task scheduler is running as System.


Some troubleshooting notes:
We CAN RUN a more basic script that imports arcpy that imports the custom modules: AutoLayers.py and CrabPyUtil.py and also uses CrabPyUtil to write a log file. This WORKS FINE when run from task schedular as System.


This means it’s not the custom module imports, it’s not the pathing used in the logging. It is most likely in the arcpy functions being run in the AutoLayerModules.py script. Here is a detailed list of the ESRI libraries being run in that script:

Milepoints Layers (3 layers)
# Steps
# Iterable Loop Steps Per Network Route (by StateLRS)
# Step 1 - Select Network
# Step 2 - Generate Points Along Lines
# Step 3 - Locate Features Along Routes
# Step 4 - Add & Calculate Fields
# Step 5 - Make Route Event Layer
# Step 6 - Export As Feature Class
# Step 7 - Append To Master Feature Class

# Non-iterable Loop Steps
# Step 8 - Delete Old Records
# Step 9 - Append New Records
# Step 10 - Delete Temp Files

Pavement Management Layer (1 layer)
# Steps
# Step 1 - Select Network
# Step 2 - Execute Stored Procedure Part A
# Step 3 - Make Route Event Layer
# Step 4 - Export As Feature Class
# Step 5 - Generate Points Along Lines (1-mile increments)
# Step 6 - Split Line At Point (1 cm radius)
# Step 7 - Calculate Field (ObjectID into AssetID)
# Step 8 - Locate Features Along Routes
# Step 9 - Join Table To Features (By AssetID)
# Step 10 - Calculate Fields
# Step 11 - Append Split Into Final
# Step 12 - Execute Stored Procedure Part B
# Step 13 - Delete Old Records
# Step 14 - Calculate Date
# Step 15 - Append New Records
# Step 16 - Delete Temp Files

Pavement Surface Condition Layers (3 layers)
# Steps
# Step 1 - Execute Stored Procedure
# Step 2 - Copy Records To New Table
# Step 3 - Add & Calculate LastUpdated Field
# Step 4 - Select Network
# Step 5 - Make Route Event Layer
# Step 6 - Export As Feature Class
# Step 7 - Delete Old Records
# Step 8 - Append New Records
# Step 9 - Delete Temp Files

Road Log Layer (1 layer)
# Steps
# Step 1 - Execute Stored Procedure
# Step 2 - Copy Records to New Table
# Step 3 - Calculate LastUpdated Field
# Step 4 - Select Network
# Step 5 - Make Route Event Layer
# Step 6 - Export As Feature Class
# Step 7 - Delete Old Records
# Step 8 - Append New Records
# Step 9 - Delete Temp Files

 

We would love to hear any feedback to help troubleshoot this issue we're having. 

0 Kudos
1 Reply
TonyAlmeida
MVP Regular Contributor

My initial first thought is that SYSTEM (Local System) has no user profile in the normal sense, no interactive desktop session, and no pre-checked-out ArcGIS Pro license, so SYSTEM can't "sign in."

Try putting it in a .bat file, something like, 

@echo off
setlocal

:: Path to your Pro conda env python
set PYTHON="C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\python.exe"

:: Initialize Pro environment
call "C:\Program Files\ArcGIS\Pro\bin\Python\Scripts\proenv.bat"

:: Run your script
%PYTHON% "C:\path\to\AutoLayerMaster.py"

endlocal