I have a very basic module I used to try and simply import it into a python tool box. The point would be run a useful application like this.
I tried from the same folder together
toolbox.pyt
# this is same folder as the toolbox
same_folder = os.path.dirname(os.path.realpath(__file__))
if same_folder not in sys.path:
sys.path.append(same_folder)
And from an different folder, just in case it cannot be in the same folder (or vv).
toolbox.pyt
# this is diff folder than toolbox
same_folder = "C:\some\folder\"
sys.path.append(same_folder)
The import resolves, but calling the func always breaks tool. These are the things I tried (with only one func at a time named test_method):
test_class.puy
class TestClass:
@staticmethod
# 1 - return this to getParameterInfo and return as [TestClass.test_method(arcpy)]
def test_method(arcpy_instance):
param0 = arcpy_instance.Parameter(
displayName="Input Features",
name="in_features",
datatype="string",
parameterType="Required",
direction="Input")
return param0
# 2 - add a message - I put this on getParameterInfo, updateParameters, and execute and it never adds any msgs
def test_method(arcpy_instance):
def test_method(arcpy_instance):
arcpy.AddMessage("Test method executed successfully.")
arcpy.AddError("xyzxyzxyz.")
# 3 - write to a file - this doesn't work - or breaks the tool
def test_method():
def test_method():
def test_method():
with open(r"some_dir\output.txt", "w") as f:
f.write("test_method was called")
When called from execute, #3 builds but gives this when executed, even though #3 does not even have a "self" arg. These are all static methods. #1and #2 just do nothing and fail silently - the worst kind of failure.
TestClass.test_method() missing 1 required positional argument: 'self'
All of them break getParameterInfo if put there.

If I move this inside the toolbox things now work.
Do you really need to put all the code inside one giant file?? B/c importing outside modules as shown is a total failure.
What does it have to be so nasty to work with this....
# this causes PermissionError: [Errno 13] Permission denied: - no matter what and I'm an admin.
def test_method_write():
with open(r"some_path/file.txt", "w") as f:
f.write("test_method was called!!!!!")
# this works as exepected
def test_method_param(mod):
param0 = mod.Parameter(
displayName="Input Features",
name="in_features",
datatype="string",
parameterType="Required",
direction="Input")
return param0
# this works as exepected
def test_method_msg(mod):
mod.AddMessage("Test message from test_method_msg")
mod.AddWarning("Test warning from test_method_msg")
mod.AddError("Test error from test_method_msg")
class Tool:
def getParameterInfo(self):
return [test_method_param(arcpy)]
def execute(self, parameters, messages):
test_method_msg(arcpy)