arcpy.Exists working in interactive window but not stand-alone script

1772
14
Jump to solution
09-01-2017 11:25 AM
AllisonBailey
New Contributor III

I am having trouble with the arcpy.Exists() function in a script to check the existence of a table in a personal geodatabase.   When I test in the ArcToolbox Python window, I get the correct result, True, for a table that exists.   When I use the same commands in a stand-alone script, regardless of how I construct the path string, it always returns False for the table.    I also tested just the geodatabase, and it returns True, so it is not having trouble finding the geodatabase itself.  

Below is the code from the script.   When I run from the interactive window, I just remove the print statement.

import arcpy


print arcpy.Exists("Y:/projects/dnr_svmp2016/db/SVMP_2000_2015_DB.v52_20170803/SVMP_DB_v5.2_20170803_AB.mdb/site_samples")
print arcpy.Exists("Y:\projects\dnr_svmp2016\db\SVMP_2000_2015_DB.v52_20170803\SVMP_DB_v5.2_20170803_AB.mdb\site_samples")
print arcpy.Exists("Y:\\projects\\dnr_svmp2016\\db\\SVMP_2000_2015_DB.v52_20170803\\SVMP_DB_v5.2_20170803_AB.mdb\\site_samples")
print arcpy.Exists("Y:/projects/dnr_svmp2016/db/SVMP_2000_2015_DB.v52_20170803/SVMP_DB_v5.2_20170803_AB.mdb")

This script returns:

False

False

False

True

Here is  screenshot of the interactive window:

Screen shot of same commmands in ArcToolbox Python window

I'm hoping that the solution is something very obvious that I am just missing right now!   Thanks for any help.

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
JoshuaBixby
MVP Esteemed Contributor

The path to your IDE answers the question.  Personal geodatabases are not supported with 64-bit Background Geoprocessing, which is the Python distribution your IDE is using.  When running from within ArcMap, it is likely executing in-process, which means it is using 32-bit ArcPy and hence works still with personal geodatabases.

Trying changing your IDE to point to the 32-bit Python distribution that comes with ArcMap.

View solution in original post

14 Replies
DanPatterson_Retired
MVP Emeritus

Just a hunch... try a file geodatabase rather than a personal geodatabase... and one without periods in it and other extraneous characters

0 Kudos
AllisonBailey
New Contributor III

Thanks Dan,

I also had concerns about the funky path, but as I mentioned, the arcpy.Exists() work for the geodatabase itself, which implies to me that the path is not the problem.

Also, I am writing code for others to use, so I don't have control over their file-naming convention or the type of geodatabase they are using.    If that is the problem, I will need to find a different way to code this check, but it would be nice to know why the Exists function is so inconsistent. 

0 Kudos
DanPatterson_Retired
MVP Emeritus

If funky is the default... try 'raw encoding (little r'

r'Y:\projects\dnr_svmp2016\db\SVMP_2000_2015_DB.v52_20170803\SVMP_DB_v5.2_20170803_AB.mdb\site_samples'

0 Kudos
AllisonBailey
New Contributor III

Sadly, same result -- False when run in script, True when run from interactive window.

0 Kudos
DanPatterson_Retired
MVP Emeritus

Then try a shorter, cleaner path with the .mdb to rule out a path issue

Then lastly is the use of *.gdb

Then try a different file...

Beyond that you have ruled everything else out

0 Kudos
AllisonBailey
New Contributor III

Looks like a couple similar (irreproducible) bugs in the past:  Esri Support Search-Results 

0 Kudos
JamesCrandall
MVP Frequent Contributor

I tend to agree that there is a need to move into FileGeodatabases rather than those personal GDB's.  Other than that, you can try to join the path and feature class name together instead of raw strings.

Instead of

r'Y:\projects\dnr_svmp2016\db\SVMP_2000_2015_DB.v52_20170803\SVMP_DB_v5.2_20170803_AB.mdb\site_samples'

Try

import os

gdb = r'Y:\projects\dnr_svmp2016\db\SVMP_2000_2015_DB.v52_20170803\SVMP_DB_v5.2_20170803_AB.mdb'
featureclass_name = r'site_samples'
featureclass = os.path.join(gdb, featureclass_name)
if arcpy.Exists(featureclass):
   'do stuff
0 Kudos
AllisonBailey
New Contributor III

Thanks James and Dan.  I used os.path.join in my original code, but was just testing with the raw strings to make it more explicit what was happening.

Turns out it does work correctly with tables in a file geodatabase, regardless of the "funkiness" of the path.    I'm still bothered by the fact that the same code works correctly in the interactive window correctly, but not in my script.   

My IDE is using C:\Python27\ArcGISx6410.4\python.exe to run the script, so that looks like the right version to me.

Guess I'll work with ListTables instead and check against the list.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

The path to your IDE answers the question.  Personal geodatabases are not supported with 64-bit Background Geoprocessing, which is the Python distribution your IDE is using.  When running from within ArcMap, it is likely executing in-process, which means it is using 32-bit ArcPy and hence works still with personal geodatabases.

Trying changing your IDE to point to the 32-bit Python distribution that comes with ArcMap.