<?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 Re: Relative import of Python packages in ArcMap Python Toolbox in ArcMap Questions</title>
    <link>https://community.esri.com/t5/arcmap-questions/relative-import-of-python-packages-in-arcmap/m-p/1268457#M4344</link>
    <description>&lt;P&gt;I managed to import by copying the package folder next to the .pyt file.&lt;/P&gt;&lt;P&gt;Then I added&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import os, sys
main_dir = os.path.dirname(os.path.abspath(__file__))
sys.path.append(main_dir)&lt;/LI-CODE&gt;&lt;P&gt;which apparently solved the issue.&lt;/P&gt;</description>
    <pubDate>Thu, 16 Mar 2023 12:38:02 GMT</pubDate>
    <dc:creator>Manu</dc:creator>
    <dc:date>2023-03-16T12:38:02Z</dc:date>
    <item>
      <title>Relative import of Python packages in ArcMap Python Toolbox</title>
      <link>https://community.esri.com/t5/arcmap-questions/relative-import-of-python-packages-in-arcmap/m-p/1268031#M4342</link>
      <description>&lt;P&gt;I want to share an ArcMap Python Toolbox which requires some additional Python package. Since the package is 100 % Python, I was able to use it by copying the Github repository to my computer. Within the Toolbox script, I added&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import sys
sys.path.append("/absolute/path/to/parent/folder")&lt;/LI-CODE&gt;&lt;P&gt;which added the folder containing the package to the Python path. This works when using the Toolbox locally, with an absolute path to the package location.&lt;/P&gt;&lt;P&gt;However, when I share the Toolbox, I have to copy the package together with the other toolbox files and to use relative paths.&lt;/P&gt;&lt;P&gt;I found multiple approaches to this: &lt;A href="https://gis.stackexchange.com/questions/19860/finding-script-location-dynamically-when-using-arcpy" target="_self"&gt;here&lt;/A&gt;, &lt;A href="https://joelmccune.com/relative-module-imports-in-an-arcgis-python-toolbox/" target="_self"&gt;here&lt;/A&gt;, and &lt;A href="https://community.esri.com/t5/python-questions/get-script-folder-from-a-script-tool-in-a-toolbox/td-p/444383" target="_self"&gt;here&lt;/A&gt;. None of them worked for me.&lt;/P&gt;&lt;P&gt;These are examples of what I tried:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import sys
# one of
# 1
this_dir = os.path.dirname(os.path.abspath("../"))

# 2
this_dir = os.path.dirname(os.path.abspath(__file__))

# 3
this_dir = os.path.dirname(os.path.abspath(sys.argv[0]))

# 4
import inspect
this_dir = inspect.getfile(inspect.currentframe())

# followed by
main_dir = os.path.dirname(this_dir)
sys.path.append(main_dir)

import spectral # = name of the package&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In any of these cases, the Toolbox did not find the package. So, how can I get the Toolbox script directory in an ArcMap Python Toolbox?&lt;/P&gt;</description>
      <pubDate>Wed, 15 Mar 2023 14:22:15 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcmap-questions/relative-import-of-python-packages-in-arcmap/m-p/1268031#M4342</guid>
      <dc:creator>Manu</dc:creator>
      <dc:date>2023-03-15T14:22:15Z</dc:date>
    </item>
    <item>
      <title>Re: Relative import of Python packages in ArcMap Python Toolbox</title>
      <link>https://community.esri.com/t5/arcmap-questions/relative-import-of-python-packages-in-arcmap/m-p/1268457#M4344</link>
      <description>&lt;P&gt;I managed to import by copying the package folder next to the .pyt file.&lt;/P&gt;&lt;P&gt;Then I added&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import os, sys
main_dir = os.path.dirname(os.path.abspath(__file__))
sys.path.append(main_dir)&lt;/LI-CODE&gt;&lt;P&gt;which apparently solved the issue.&lt;/P&gt;</description>
      <pubDate>Thu, 16 Mar 2023 12:38:02 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcmap-questions/relative-import-of-python-packages-in-arcmap/m-p/1268457#M4344</guid>
      <dc:creator>Manu</dc:creator>
      <dc:date>2023-03-16T12:38:02Z</dc:date>
    </item>
  </channel>
</rss>

