Select to view content in your preferred language

How to set relative path in notebook

262
5
Jump to solution
4 weeks ago
JV_
by
Occasional Contributor

Hi,

 

I'm working on a code that I would like to share and, ideally, I want to use relative paths. However, I've tried several approaches and found that it only works when I use the complete path.

 

Here's a simplified example where I attempt to insert a feature from a geodatabase:

 
 
# Path to your feature class in the geodatabase
feature_class_path = feature_path
# Get the current project
project = arcpy.mp.ArcGISProject("CURRENT")

# Get the active map (or specify which map to use)
map_object = project.listMaps()[0]  # Assuming you want the first map

# Add the feature class to the map
map_object.addDataFromPath(feature_class_path)

print(f"Feature class '{feature_class_path}' added to the map.")
 
 

When I set feature_class_path using a relative path like this:

 
 
feature_class_path = r".\UpdatePoints\UpdatePoints.gdb\Earthquakes_points"
 
 

it doesn't work. If I use:

 
 
feature_class_path = os.path.join(".", "UpdatePoints", "UpdatePoints.gdb", "Earthquakes_points")
 
 

it still doesn't work.

 

The code only functions correctly when I use the full path:

 
 
feature_class_path = r"C:\Users\YourUsername\Documents\UpdatePoints\UpdatePoints.gdb\Earthquakes_points"
 
 

Why is that?

 

 

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
AlfredBaldenweck
MVP Regular Contributor

So, two things:

  1. I messed up and fed os.path.dirname() an APRX project instead of a file path
  2. The file has to actually exist.
import os
# Get the current project
project = arcpy.mp.ArcGISProject("CURRENT")

# Change working directory 
os.chdir(os.path.dirname(project.filePath))

# Path to your feature class in the geodatabase
# Hard-code in the relative path, then resolve it to absolute 
# for use in geoprocessing
feature_class_path = r".\Default.gdb\example"
feature_class_path = os.path.abspath(feature_class_path)
print(feature_class_path)

# Get the active map (or specify which map to use)
map_object = project.listMaps()[0]  # Assuming you want the first map

# Add the feature class to the map
map_object.addDataFromPath(feature_class_path)

This worked fine for me with "example", but "example2" failed because it doesn't exist.

AlfredBaldenweck_1-1734628157217.png

So you'll want to add an if arcpy.Exists(feature_class_path) in there somewhere. 

 

View solution in original post

0 Kudos
5 Replies
DanPatterson
MVP Esteemed Contributor
p = arcpy.mp.ArcGISProject('CURRENT')
#Project information
print(f'Project: {p.filePath}')

perhaps, then strip off the filename


... sort of retired...
0 Kudos
AlfredBaldenweck
MVP Regular Contributor

hmmm.

Try something like this:

import os
# Get the current project
project = arcpy.mp.ArcGISProject("CURRENT")

# Change working directory 
os.chdir(os.path.dirname(project))

# Path to your feature class in the geodatabase
# Hard-code in the relative path, then resolve it to absolute 
# for use in geoprocessing
feature_class_path = r".\ex.gdb\feature_path"
feature_class_path = os.path.abspath(feature_class_path)


# Get the active map (or specify which map to use)
map_object = project.listMaps()[0]  # Assuming you want the first map

# Add the feature class to the map
map_object.addDataFromPath(feature_class_path)

print(f"Feature class '{feature_class_path}' added to the map.")
0 Kudos
JV_
by
Occasional Contributor

No, didn't work either

0 Kudos
AlfredBaldenweck
MVP Regular Contributor

So, two things:

  1. I messed up and fed os.path.dirname() an APRX project instead of a file path
  2. The file has to actually exist.
import os
# Get the current project
project = arcpy.mp.ArcGISProject("CURRENT")

# Change working directory 
os.chdir(os.path.dirname(project.filePath))

# Path to your feature class in the geodatabase
# Hard-code in the relative path, then resolve it to absolute 
# for use in geoprocessing
feature_class_path = r".\Default.gdb\example"
feature_class_path = os.path.abspath(feature_class_path)
print(feature_class_path)

# Get the active map (or specify which map to use)
map_object = project.listMaps()[0]  # Assuming you want the first map

# Add the feature class to the map
map_object.addDataFromPath(feature_class_path)

This worked fine for me with "example", but "example2" failed because it doesn't exist.

AlfredBaldenweck_1-1734628157217.png

So you'll want to add an if arcpy.Exists(feature_class_path) in there somewhere. 

 

0 Kudos
HaydenWelch
Frequent Contributor

I try to avoid using chdir in my scripts usually because it can make debugging a bit difficult.

Just use full paths for everything, but name the segments (e.g. project_directory, working_database, active_dataset, etc.) that way you can see exactly what is being accessed. I usually use pathlib.Path objects so I can use inline operators and avoid messy os.path.join calls.

If you need relative pathing, you can accomplish that using pathlib too.

 

Here's a code example:

from pathlib import Path
import arcpy
from arcpy._mp import Map

# Get Project
prj = arcpy.mp.ArcGISProject("CURRENT")

# Set Paths
home_folder = Path(prj.homeFolder)
database: Path = home_folder / "Data.gdb"
feature_class: Path = database / "FeatureClass"

# Get Map
_map: Map = prj.listMaps("Map Name")[0]

# Add Featureclass
_map.addDataFromPath(str(feature_class))