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

5284
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
1 Solution

Accepted Solutions
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...

View solution in original post

18 Replies
JoshuaBixby
MVP Esteemed Contributor

How long has this been going on?  How is Pro licensed on your machine?  Can you remove the try/except block and share the full error and traceback?

0 Kudos
JohnHuillery1
Occasional Contributor

I'm not sure how long it has been going on. I've mostly noticed it after I upgraded to Pro 2.5.

Licensing info:

CMD output with try/except removed:

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

I'm not getting Python errors because it seems like Python is crashing on the import statement.

This only happens when I run it through a cmd prompt. It only happens about half the time.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Using the The Microsoft Error Lookup Tool - Win32 apps | Microsoft Docs, code 246 matches two different results depending on whether it is hex or dec.

# for decimal 246 / hex 0xf6
  PCI_VERIFIER_DETECTED_VIOLATION                                bugcodes.h
# for hex 0x246 / decimal 582
  ERROR_ILLEGAL_CHARACTER                                        winerror.h
# An illegal character was encountered. For a multi-byte
# character set this includes a lead byte without a
# succeeding trail byte. For the Unicode character set this
# includes the characters 0xFFFF and 0xFFFE.
# as an HRESULT: Severity: SUCCESS (0), FACILITY_NULL (0x0), Code 0x246
# for hex 0x246 / decimal 582
  ERROR_ILLEGAL_CHARACTER                                        winerror.h
# An illegal character was encountered. For a multi-byte
# character set this includes a lead byte without a
# succeeding trail byte. For the Unicode character set this
# includes the characters 0xFFFF and 0xFFFE.
# 3 matches found for "246"

If you were having a PCI_VERIFIER_DETECTED_VIOLATION issue, I am guessing you would be seeing a whole lot more problems, maybe even a BSOD.  So, that leaves the illegal character and the likely culprit.

What program did you create your Python script in?  Did you copy and paste the code from somewhere?

Or, it could just be something got corrupted with Python when you installed/upgraded to Pro 2.5.  You could always try uninstalling and reinstalling Pro.

0 Kudos
JohnHuillery1
Occasional Contributor

I used Spyder to write the code originally.

I rewrote the code in plain old notepad and I'm getting the same issue.

The same issue is happening when I get my coworkers to run it on their machines so I don't think it's a corrupted install.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

I ran your code on my Pro 2.5 Beta (I haven't upgraded to Pro 2.5 final yet) without any issues.

0 Kudos
JohnHuillery1
Occasional Contributor

How are you running it? It only happens when I run it from the cmd line.

Also it doesn't happen every time. It is very random.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

I opened up a new command prompt, and then typed in the path to the batch file containing the code.

0 Kudos
JohnHuillery1
Occasional Contributor

That would cause the issue (sometimes) on the machines I've tested on.  How can I get this resolved so my processes that run on the Task Scheduler don't fail randomly?

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Any intermittent problem is challenging to solve, not just those with ArcGIS API for Python.  At this point, I don't have a whole lot of suggestions.  Do you see anything in the Windows event logs at the same time you are seeing the code fail to run?

0 Kudos