Select to view content in your preferred language

arcpy.env.workspace should accept a workspace object

223
1
03-16-2026 08:55 AM
Status: Open
AlfredBaldenweck
MVP Frequent Contributor

I was working on a tool the other day. It was something along the lines of :

arcpy.env.workspace = arcpy.management.CreateFileGDB(r"C:\\Documents", "Example")

 

I got an error

arcpy.env.workspace = arcpy.management.CreateFileGDB(fol, "Example")
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\geoprocessing\_base.py", line 564, in set_
self[env] = val
~~~~^^^^^
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\geoprocessing\_base.py", line 640, in __setitem__
ret_ = setattr(self._gp, item, value)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: Object: Error in accessing environment <workspace>

However,

arcpy.env.workspace = str(arcpy.management.CreateFileGDB(fol, "Example"))

works just fine.

CreateFileGDB returns a workspace object. It stands to reason arcpy.env.workspace should be able to consume it.

Please let arcpy.env.workspace take workspace objects.

1 Comment
HaydenWelch

Workspace objects should also just kinda behave like Path objects. Keeping track of which functions require a concrete class input and which ones need some form of string is a pain. If the concretes could behave like a string when used as an input (e.g. implement __str__ and __fspath__), the consuming function would just need to call arg = str(arg) and the conversion is made.