Select to view content in your preferred language

Add support for pathlib.Path objects to arcpy

12187
31
04-09-2020 09:20 AM
Status: Implemented
ScottDavis
Frequent Contributor

pathlib seems to be the preferred method for working with system paths in Python 3. We've started using it in place of `os.path` in our office recently and really like it. However, we've run into problems with using it with arcpy. For example:

workspace = pathlib.Path('C:\some path') \ 'anotherfolder' \ 'connection.sde'
with arcpy.EnvManager(workspace=workspace):
    pass‍‍‍‍‍‍

Throws this error: "RuntimeError: Object: Error in accessing environment <workspace>"

To work around this, we end up wrapping all of our Path object with str(). For example:

workspace = str(pathlib.Path('C:\some path') \ 'anotherfolder' \ 'connection.sde')
with arcpy.EnvManager(workspace=workspace):
    pass‍‍‍‍‍‍‍‍‍

It would be great if arcpy (Pro version) handled these Path objects natively!

Tags (2)
31 Comments
MErikReedAugusta

Great news!  There actually already exists an Idea for this here, and it was also implemented finally in ArcGIS Pro 3.7, which released last over two weeks ago.

You can find more information about pathlib in 3.7 at the two links below:

 

For those like me terminally stuck on older versions (3.1.3, here), I just cast all my pathlib objects to string at the very last second when using them as inputs, so I get nearly all of the benefits of pathlib.  Still, it'll be nice A) when I can finally just pass them to arcpy functions and B) can use a python version high enough that it has pathlib's implementation of Walk available.  (Both of which will be true whenever I can finally upgrade to 3.7)