<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic How to set relative path in notebook in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/how-to-set-relative-path-in-notebook/m-p/1569694#M73378</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;P&gt;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.&lt;/P&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;P&gt;Here's a simplified example where I attempt to insert a feature from a geodatabase:&lt;/P&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/DIV&gt;&lt;PRE&gt;&lt;SPAN class=""&gt;# Path to your feature class in the geodatabase&lt;/SPAN&gt;
feature_class_path = feature_path
&lt;SPAN class=""&gt;# Get the current project&lt;/SPAN&gt;
project = arcpy.mp.ArcGISProject(&lt;SPAN class=""&gt;"CURRENT"&lt;/SPAN&gt;)

&lt;SPAN class=""&gt;# Get the active map (or specify which map to use)&lt;/SPAN&gt;
map_object = project.listMaps()[&lt;SPAN class=""&gt;0&lt;/SPAN&gt;]  &lt;SPAN class=""&gt;# Assuming you want the first map&lt;/SPAN&gt;

&lt;SPAN class=""&gt;# Add the feature class to the map&lt;/SPAN&gt;
map_object.addDataFromPath(feature_class_path)

&lt;SPAN class=""&gt;print&lt;/SPAN&gt;(&lt;SPAN class=""&gt;f"Feature class '&lt;SPAN class=""&gt;{feature_class_path}&lt;/SPAN&gt;' added to the map."&lt;/SPAN&gt;)&lt;/PRE&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/DIV&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;P&gt;When I set feature_class_path using a relative path like this:&lt;/P&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/DIV&gt;&lt;PRE&gt;feature_class_path = &lt;SPAN class=""&gt;r".\UpdatePoints\UpdatePoints.gdb\Earthquakes_points"&lt;/SPAN&gt;&lt;/PRE&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/DIV&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;P&gt;it doesn't work. If I use:&lt;/P&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/DIV&gt;&lt;PRE&gt;feature_class_path = os.path.join(&lt;SPAN class=""&gt;"."&lt;/SPAN&gt;, &lt;SPAN class=""&gt;"UpdatePoints"&lt;/SPAN&gt;, &lt;SPAN class=""&gt;"UpdatePoints.gdb"&lt;/SPAN&gt;, &lt;SPAN class=""&gt;"Earthquakes_points"&lt;/SPAN&gt;)&lt;/PRE&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/DIV&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;P&gt;it still doesn't work.&lt;/P&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;P&gt;The code only functions correctly when I use the full path:&lt;/P&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/DIV&gt;&lt;PRE&gt;feature_class_path = &lt;SPAN class=""&gt;r"C:\Users\YourUsername\Documents\UpdatePoints\UpdatePoints.gdb\Earthquakes_points"&lt;/SPAN&gt;&lt;/PRE&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/DIV&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;P&gt;Why is that?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 18 Dec 2024 11:45:54 GMT</pubDate>
    <dc:creator>JV_</dc:creator>
    <dc:date>2024-12-18T11:45:54Z</dc:date>
    <item>
      <title>How to set relative path in notebook</title>
      <link>https://community.esri.com/t5/python-questions/how-to-set-relative-path-in-notebook/m-p/1569694#M73378</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;P&gt;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.&lt;/P&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;P&gt;Here's a simplified example where I attempt to insert a feature from a geodatabase:&lt;/P&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/DIV&gt;&lt;PRE&gt;&lt;SPAN class=""&gt;# Path to your feature class in the geodatabase&lt;/SPAN&gt;
feature_class_path = feature_path
&lt;SPAN class=""&gt;# Get the current project&lt;/SPAN&gt;
project = arcpy.mp.ArcGISProject(&lt;SPAN class=""&gt;"CURRENT"&lt;/SPAN&gt;)

&lt;SPAN class=""&gt;# Get the active map (or specify which map to use)&lt;/SPAN&gt;
map_object = project.listMaps()[&lt;SPAN class=""&gt;0&lt;/SPAN&gt;]  &lt;SPAN class=""&gt;# Assuming you want the first map&lt;/SPAN&gt;

&lt;SPAN class=""&gt;# Add the feature class to the map&lt;/SPAN&gt;
map_object.addDataFromPath(feature_class_path)

&lt;SPAN class=""&gt;print&lt;/SPAN&gt;(&lt;SPAN class=""&gt;f"Feature class '&lt;SPAN class=""&gt;{feature_class_path}&lt;/SPAN&gt;' added to the map."&lt;/SPAN&gt;)&lt;/PRE&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/DIV&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;P&gt;When I set feature_class_path using a relative path like this:&lt;/P&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/DIV&gt;&lt;PRE&gt;feature_class_path = &lt;SPAN class=""&gt;r".\UpdatePoints\UpdatePoints.gdb\Earthquakes_points"&lt;/SPAN&gt;&lt;/PRE&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/DIV&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;P&gt;it doesn't work. If I use:&lt;/P&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/DIV&gt;&lt;PRE&gt;feature_class_path = os.path.join(&lt;SPAN class=""&gt;"."&lt;/SPAN&gt;, &lt;SPAN class=""&gt;"UpdatePoints"&lt;/SPAN&gt;, &lt;SPAN class=""&gt;"UpdatePoints.gdb"&lt;/SPAN&gt;, &lt;SPAN class=""&gt;"Earthquakes_points"&lt;/SPAN&gt;)&lt;/PRE&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/DIV&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;P&gt;it still doesn't work.&lt;/P&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;P&gt;The code only functions correctly when I use the full path:&lt;/P&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/DIV&gt;&lt;PRE&gt;feature_class_path = &lt;SPAN class=""&gt;r"C:\Users\YourUsername\Documents\UpdatePoints\UpdatePoints.gdb\Earthquakes_points"&lt;/SPAN&gt;&lt;/PRE&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/DIV&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;P&gt;Why is that?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 18 Dec 2024 11:45:54 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-set-relative-path-in-notebook/m-p/1569694#M73378</guid>
      <dc:creator>JV_</dc:creator>
      <dc:date>2024-12-18T11:45:54Z</dc:date>
    </item>
    <item>
      <title>Re: How to set relative path in notebook</title>
      <link>https://community.esri.com/t5/python-questions/how-to-set-relative-path-in-notebook/m-p/1569696#M73379</link>
      <description>&lt;LI-CODE lang="python"&gt;p = arcpy.mp.ArcGISProject('CURRENT')
#Project information
print(f'Project: {p.filePath}')&lt;/LI-CODE&gt;&lt;P&gt;perhaps, then strip off the filename&lt;/P&gt;</description>
      <pubDate>Wed, 18 Dec 2024 11:59:13 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-set-relative-path-in-notebook/m-p/1569696#M73379</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2024-12-18T11:59:13Z</dc:date>
    </item>
    <item>
      <title>Re: How to set relative path in notebook</title>
      <link>https://community.esri.com/t5/python-questions/how-to-set-relative-path-in-notebook/m-p/1569766#M73381</link>
      <description>&lt;P&gt;hmmm.&lt;/P&gt;&lt;P&gt;Try something like this:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;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.")&lt;/LI-CODE&gt;</description>
      <pubDate>Wed, 18 Dec 2024 14:48:50 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-set-relative-path-in-notebook/m-p/1569766#M73381</guid>
      <dc:creator>AlfredBaldenweck</dc:creator>
      <dc:date>2024-12-18T14:48:50Z</dc:date>
    </item>
    <item>
      <title>Re: How to set relative path in notebook</title>
      <link>https://community.esri.com/t5/python-questions/how-to-set-relative-path-in-notebook/m-p/1570117#M73393</link>
      <description>&lt;P&gt;No, didn't work either&lt;/P&gt;</description>
      <pubDate>Thu, 19 Dec 2024 11:27:34 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-set-relative-path-in-notebook/m-p/1570117#M73393</guid>
      <dc:creator>JV_</dc:creator>
      <dc:date>2024-12-19T11:27:34Z</dc:date>
    </item>
    <item>
      <title>Re: How to set relative path in notebook</title>
      <link>https://community.esri.com/t5/python-questions/how-to-set-relative-path-in-notebook/m-p/1570248#M73397</link>
      <description>&lt;P&gt;So, two things:&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;I messed up and fed os.path.dirname() an APRX project instead of a file path&lt;/LI&gt;&lt;LI&gt;The file has to actually exist.&lt;/LI&gt;&lt;/OL&gt;&lt;LI-CODE lang="python"&gt;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)&lt;/LI-CODE&gt;&lt;P&gt;This worked fine for me with "example", but "example2" failed because it doesn't exist.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="AlfredBaldenweck_1-1734628157217.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/122134iCF2236BE080F32F5/image-size/medium?v=v2&amp;amp;px=400" role="button" title="AlfredBaldenweck_1-1734628157217.png" alt="AlfredBaldenweck_1-1734628157217.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;So you'll want to add an&amp;nbsp;&lt;STRONG&gt;if arcpy.Exists(feature_class_path)&lt;/STRONG&gt; in there somewhere.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 19 Dec 2024 17:09:47 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-set-relative-path-in-notebook/m-p/1570248#M73397</guid>
      <dc:creator>AlfredBaldenweck</dc:creator>
      <dc:date>2024-12-19T17:09:47Z</dc:date>
    </item>
    <item>
      <title>Re: How to set relative path in notebook</title>
      <link>https://community.esri.com/t5/python-questions/how-to-set-relative-path-in-notebook/m-p/1570829#M73419</link>
      <description>&lt;P&gt;I try to avoid using chdir in my scripts usually because it can make debugging a bit difficult.&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;If you need relative pathing, you can accomplish that using pathlib too.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here's a code example:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;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))&lt;/LI-CODE&gt;</description>
      <pubDate>Mon, 23 Dec 2024 16:49:19 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-set-relative-path-in-notebook/m-p/1570829#M73419</guid>
      <dc:creator>HaydenWelch</dc:creator>
      <dc:date>2024-12-23T16:49:19Z</dc:date>
    </item>
  </channel>
</rss>

