[GIS] Custom Python Toolbox not importing when module in other folder

arcgis-10.6arcpyeclipsepython-toolbox

I'm following this guide
http://pro.arcgis.com/en/pro-app/arcpy/geoprocessing_and_python/extending-geoprocessing-through-python-modules.htm

I got it to work on arcmap 10.6 (desktop) when the toolbox is in the same folder as the module (src/foo/foo.py). But when I move the toolbox into src/foo/esri/toolboxes, the toolbox has a red x on it in arccatalog.

Inside the toolbox I've tried:

  • import foo.foo
  • import foo
  • import foo from foo

When i right click on toolbox and "check syntax" I keep getting the error
"ImportError: No module named foo"

Reading up on google it sounds like i may need to do something with sys.path.append… but I've tried tons of copied and pasted solutions to no avail.

Other people have said I have to type in the path manually, but that's a problem since i'm hoping to share this to others.

I tried building it, and installing it, and it doesn't show up in arctoolbox. When I have the toolbox in the foo folder, it does show up in arctoolbox after install.

I'm out of things to try and looking for help!

I am creating the project in PyDev Eclipse, and there are no syntax errors or problems with imports.

Best Answer

As commented by @KHibma:

The core of your problem is one script cannot find another script to import. While sure, you're using ArcGIS and its GIS software indicating the problem. The core part of the problem is specific to importing modules within Python. ArcGIS "runs" a PYT to discover if there are any issues. If you were to run your project in Eclipse it should error the same way as you see in ArcGIS. Luke & Michael have provided some good pointers on the core concepts of modules and importing

@Luke's comments were:

Read up on the module search path and how to set it.

and

I would also think about reorganising your code and having the toolbox at the top level, something like https://gis.stackexchange.com/a/43213/2856 and see also possible duplicate Q. https://gis.stackexchange.com/a/75942/2856

while @MichaelStimson commented that:

This might help you https://stackoverflow.com/questions/50499/how-do-i-get-the-path-and-name-of-the-file-that-is-currently-executing

I note that you have commented that:

Based on the links, [you]'ve solved the problem by putting sys.path.append(path to my src folder).

I should also point out that you seem to be using ArcPy with ArcMap but you are referring to ArcGIS Pro documentation which is not always the same.