Refresh a Python Toolbox in ArcCatalog

1526
5
Jump to solution
09-10-2021 03:44 PM
BerndtNording
New Contributor III

I have incorporated the hack at the bottom of  this help page in order to work with python toolboxes in VS.

The hack works, but if I make changes to the code of any tool therin, save them and then refresh the toolbox in ArcCatalog, the tool runs the old code. It won't run the new code until I restart ArcCatalog, which is anything but fast. Is there anyway to refresh the toolbox without restarting ArcCatalog?

0 Kudos
1 Solution

Accepted Solutions
JohannesLindner
MVP Frequent Contributor

I think (not sure), ArcGIS uses the compiled pyc file unless you explicitly reload the module in your toolbox.

# toolbox.pyt
import importlib

import your_module
importlib.reload(your_module)
from your_module import *


# To see changes made in modules you import in your_module, 
# you have to reload them in your_module, too:

# your_module.py
import importlib

import utility_functions
importlib.reload(utility_functions)
from utility_functions import *

 


Have a great day!
Johannes

View solution in original post

5 Replies
by Anonymous User
Not applicable
Did you try a right click on the updated toolbox or it’s containing folder and selecting refresh?
0 Kudos
BerndtNording
New Contributor III

Yes, I refreshed the Toolbox as well as the folder containing the Toolbox.  This worked when all the code was in the .pyt. It seems that ArcCatalog is storing the code in the .py in memory and not re-reading it when the .pyt that references the .py is refreshed.

0 Kudos
nzjs
by
New Contributor III

I have the same issue when developing scripts for a Python toolbox which import classes or utilities from separate .py files inside the main tool .py file. The only way I can see changes in these files referenced within the tool is by restarting ArcGIS Pro. 

If there a solution I would also love to know about it.

0 Kudos
JohannesLindner
MVP Frequent Contributor

I think (not sure), ArcGIS uses the compiled pyc file unless you explicitly reload the module in your toolbox.

# toolbox.pyt
import importlib

import your_module
importlib.reload(your_module)
from your_module import *


# To see changes made in modules you import in your_module, 
# you have to reload them in your_module, too:

# your_module.py
import importlib

import utility_functions
importlib.reload(utility_functions)
from utility_functions import *

 


Have a great day!
Johannes
BerndtNording
New Contributor III

Brilliant solution.   Almost - but only works for ArcGIS Pro.

ArcCatalog is ArcGIS 10.x and thus uses Python 2.7, where this doesn't work. Some digging came up with just

reload(your-module)

or there's another reload in module imp:

import imp

imp.reload(your-module)

 

which works for python 2 and 3.2-3.3 although I don't think there's Pro version that uses any of these Python versions.

 

Thanks for putting me on the right path!

0 Kudos