Select to view content in your preferred language

Tying to allow the user to set their workspace in a custom scirpt tool

145
3
Jump to solution
2 weeks ago
dcaESRIwvges
New Contributor

I'm using arcgis pro 3.5.2 and python 3.11.11

The following is how i set up the inital code:

# locations of the working stratigraphy data
working_strat_data_location = arcpy.GetParameterAsText(0)
arcpy.env.workspace = os.path.join(working_strat_data_location, "geodatabase_name.gdb")
arcpy.env.overwriteOutput = True

However, when i add a folder paths like: "C:\Users" or "\\NETWORK_1\folder1\folder2\" into a string parameter in the custom script tool, i even tried setting the paramter as a folder. This was not working or it was not setting the workspace to this location. 

The only way i got it to work was using this:

arcpy.env.workspace = r"NETWORK_1\folder1\folder2\geodatabase_name.gdb"

I know the "r" isn't needed as in the past I've been told to never use it, but I want to know what I'm missing in the first block of code. 

I'm replacing an old workflow from desktop 10.0 (no longer supported) by creating this custom tool that I'm trying to get to work in Pro. This first step is essential. 

0 Kudos
1 Solution

Accepted Solutions
VenkataKondepati
Emerging Contributor

Hi,

I’ve run into this exact issue when modernizing older workflows from ArcMap 10.x to ArcGIS Pro.

The core of the problem is that when you pass a folder path as a parameter and then try to build the full GDB path using os.path.join(), ArcPy doesn’t always behave the way you’d expect — especially if the input path has trailing slashes, or if the geodatabase doesn’t exist yet.

Here's what I usually do to bulletproof it:

import os, arcpy

working_folder = arcpy.GetParameterAsText(0)
gdb_name = "geodatabase_name.gdb"
gdb_path = os.path.join(working_folder.strip(), gdb_name)

if arcpy.Exists(gdb_path):
    arcpy.env.workspace = gdb_path
    arcpy.AddMessage(f"Workspace set to: {gdb_path}")
else:
    arcpy.AddError(f"Could not find GDB at: {gdb_path}")
  • Make sure your script tool parameter is set to type = Folder (not generic string).

  • Always validate the final gdb_path before using it.

  • I know people say not to use the r"" prefix — but when hardcoding UNC paths, I still use it out of habit because it avoids weird issues with escape characters.

    The reason your arcpy.env.workspace = r"NETWORK_1\folder1\folder2\geodatabase_name.gdb" worked is likely because you bypassed the join logic entirely — which also means you’re avoiding any string formatting mishaps from the input.

    So, you’re not doing anything wrong — it’s just that ArcGIS Pro and its newer Python runtime (3.11 now) can be a little less forgiving than ArcMap used to be.

    Please let me know if you have further questions. 

    Regards,

    Venkat

 

View solution in original post

3 Replies
VenkataKondepati
Emerging Contributor

Hi,

I’ve run into this exact issue when modernizing older workflows from ArcMap 10.x to ArcGIS Pro.

The core of the problem is that when you pass a folder path as a parameter and then try to build the full GDB path using os.path.join(), ArcPy doesn’t always behave the way you’d expect — especially if the input path has trailing slashes, or if the geodatabase doesn’t exist yet.

Here's what I usually do to bulletproof it:

import os, arcpy

working_folder = arcpy.GetParameterAsText(0)
gdb_name = "geodatabase_name.gdb"
gdb_path = os.path.join(working_folder.strip(), gdb_name)

if arcpy.Exists(gdb_path):
    arcpy.env.workspace = gdb_path
    arcpy.AddMessage(f"Workspace set to: {gdb_path}")
else:
    arcpy.AddError(f"Could not find GDB at: {gdb_path}")
  • Make sure your script tool parameter is set to type = Folder (not generic string).

  • Always validate the final gdb_path before using it.

  • I know people say not to use the r"" prefix — but when hardcoding UNC paths, I still use it out of habit because it avoids weird issues with escape characters.

    The reason your arcpy.env.workspace = r"NETWORK_1\folder1\folder2\geodatabase_name.gdb" worked is likely because you bypassed the join logic entirely — which also means you’re avoiding any string formatting mishaps from the input.

    So, you’re not doing anything wrong — it’s just that ArcGIS Pro and its newer Python runtime (3.11 now) can be a little less forgiving than ArcMap used to be.

    Please let me know if you have further questions. 

    Regards,

    Venkat

 

TonyAlmeida
MVP Regular Contributor

try using  pathlib

from pathlib import Path
working_strat_data_location = Path(arcpy.GetParameterAsText(0))
arcpy.env.workspace = str(working_strat_data_location / "geodatabase_name.gdb")

 

dcaESRIwvges
New Contributor

Thank you both! I will try both solutions and let you know!