Error code 246 when attempting to import ArcGIS API for Python through CMD prompt

5280
18
Jump to solution
02-25-2020 10:35 AM
JohnHuillery1
Occasional Contributor

My scripts fail about half the time with error code 246 on importing the ArcGIS API for Python when executing the script from a batch file.

My batch file:

@echo off
"C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\python.exe" "C:\Users\jhuillery\Desktop\import_arcgis.py"
if errorlevel 1 (echo Failure Reason Given is %errorlevel%)
pause

My script (import_arcgis.py):

print('importing sys...')
import sys
print(sys.executable)
print('importing arcgis...')
try:
    from arcgis.gis import GIS
except ImportError:
    print(sys.exc_info())

CMD output:

importing sys...
C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\python.exe
importing arcgis...
Failure Reason Given is 246

I'm using:

  • ArcGIS Pro 2.5.0
  • ArcGIS API for Python 1.7.0
  • Windows 10 Enterprise
0 Kudos
18 Replies
JohnHuillery1
Occasional Contributor

When it fails, it gives me result code = 2147942646 in the event logs

0 Kudos
JohnHuillery1
Occasional Contributor

No clue what's going on here but my temporary hack solution was to modify my .bat files to keep repeating the commands that run my scripts until the exit code = 0.

:StartLoop
"C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\python.exe" "C:\Users\jhuillery\Desktop\import_arcgis.py"

IF %ERRORLEVEL% NEQ 0 GOTO StartLoop
pause

JeffHaupt
New Contributor II

I know this post was a bit older, but we are trying to solve the same type of issue and I was trying to replicate your batch file code here.  Were you running this through Windows Task Scheduler?

0 Kudos
DanMcCoy1
New Contributor III

My batch file:

@echo off
"C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\python.exe" "C:\Users\jhuillery\Desktop\import_arcgis.py"
if errorlevel 1 (echo Failure Reason Given is %errorlevel%)
pause

@JohnHuillery1 
Have you tried pythonw.exe?

https://community.esri.com/t5/python-questions/last-run-result-message-0xf6-when-trying-to-run-a-pyt...

BillMacPherson
New Contributor III

Here's a more complete DOS batch file that uses a loop to check return codes from the python.exe. It will repeat the loop until it gets a return code == 0 or until the maximum number of tries is reached. So far, I haven't seen it go over 7 tries. Note: This is only checking the problem where Windows Task Scheduler is not executing python.exe. It is not checking to see if there was an error within the python script.

--------------------------------

SET log_file=c:\temp\Log.txt

ECHO ######################################################################### >> %log_file%
ECHO Batch start %date% Time: %time% >> %log_file%

SET /A _counter=1
SET /A _max_tries=21
SET /A _wait_seconds=2

:LOOP

IF %_counter%==%_max_tries% (
ECHO GOTO END - max tries of [%_max_tries%] reached. >> %log_file%
GOTO :END
)

ECHO Attempt: [%_counter%] >> %log_file%

ECHO Error level before python: [%errorlevel%] >> %log_file%

:: Use the python exe to run our batch script.
:: Use full path to both python exe and python script.
"c:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\pythonw.exe" c:\temp\process.py

ECHO Error level after python: [%errorlevel%] >> %log_file%

:: Check the python return code. If success then exit Else pause and try again.
IF %errorlevel%==0 (
ECHO GOTO END - success >> %log_file%
GOTO :END
)
ELSE (
ECHO Wait for some time. >> %log_file%
TIMEOUT %_wait_seconds% > NUL
)

ECHO After pause %date% Time: %time% >> %log_file%

:: Increment the counter.
SET /A _counter=%_counter%+1

GOTO :LOOP

:END
ECHO Batch end %date% Time: %time% >> %log_file%

 

MichaelKohler
Occasional Contributor II

Not sure when it happened, but upgrading to different versions of ArcGIS Pro 2.x caused python 3 scripts to fail in the task manager. I couldn't figure out why my Python 3 scripts weren't working anymore while my Python 2 scripts were working fine. The task manager was not helpful in providing clues so I resorted to running them manually.

When I found this post the other day, I was hoping this would be a fix. I tried scheduling a batch file in the past and that was working intermittently as well. But today I confirmed I had a python 3 script take 3 tries before it was successful by looking at the log.

Thank you very much!

 

0 Kudos
JeffHaupt
New Contributor II

Thanks so much for providing that!

0 Kudos
BillMacPherson
New Contributor III

Here is a similar solution using a PowerShell script loop.

-------------------

$py_path="c:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\python.exe"
$py_script="c:\temp\ps_Process.py"
$log_file="c:\temp\ps_log.txt"
$RandID = Get-Random -Minimum 1000 -Maximum 9999

$EMAIL_TO = "xyz@name.com"
$EMAIL_SUBJECT = "PS Loop Test"
$EMAIL_FROM = "abc@name.com"
$EMAIL_SERVER = "server_ip"

# Logging function to write to a text file.
Function Log {
param(
[Parameter(Mandatory=$true)][String]$msg
)
$FormattedDate = Get-Date -Format "MM-dd-yyyy hh:mm:ss tt"
Add-Content $log_file "$("PS")-$($RandID) $(" ") $($FormattedDate) $(" ") $($msg)"
}

Log "##########################################################"
Log "Start PowerShell"

$SUCCESS=1 # init to failed.
$py_return=""

for ($i = 0; $i -lt 10; $i++){
& $py_path $py_script
if($LASTEXITCODE -eq 0){
# success
Log "$i python.exe attempt success."
$SUCCESS=0
break
}
else {
Log "$i python.exe attempt failed (LASTEXITCODE = $LASTEXITCODE)"
}
Start-Sleep -Seconds 5.0
}

Log "LASTEXITCODE= $LASTEXITCODE"
Log "ERROR= $ERROR"
Log "?= $?"

if ($SUCCESS -eq 0) {
Log "Python.exe ran successfully."
$py_return="SUCCESS"
}
else {
Log "Python.exe failed."
$py_return="FAILED"
}

$SUBJECT="$EMAIL_SUBJECT - $py_return"
Send-MailMessage -From $EMAIL_FROM -To $EMAIL_TO -Subject $SUBJECT -Body "($py_return) Result from python." -SmtpServer $EMAIL_SERVER

Log "End PowerShell"

 

0 Kudos
JakeKnowlesDMM
New Contributor

We were also having these sporadic failed arcpy imports resulting in a 246 error code using a stock arcgispro-py3 environment. Though the pythonw.exe and import looping workarounds did work, we found a permanent solution in adding an exception policy for the environment's python.exe in our server's antivirus software. This resolved all problems with importing arcpy. This might also explain why pythonw.exe worked - since it doesn't spawn a shell, it didn't trigger the same behaviour with our antivirus.

0 Kudos