I'm trying to call a function defined in a script contained in Toolbox. I have imported the Toolbox via arcpy.ImportToolbox. The scripts contained are recognized. I cannot call the function defined to each one.
error messages?
is this a python toolbox (pyt)
I guess you are going to have to show what you have, since you appear to be doing something different than normal imports support
try the toolbox alias approach
ImportToolbox—ArcPy Functions | ArcGIS Desktop
The problem arises when I import the scripts in the Toolbox.
script1.py
<SPAN class="comment token"># -*- coding: utf-8 -*-</SPAN> <SPAN class="string token">""" script1.py """</SPAN> <SPAN class="keyword token">from</SPAN> script2 <SPAN class="keyword token">import</SPAN> hello name <SPAN class="operator token">=</SPAN> hello<SPAN class="punctuation token">(</SPAN>nme<SPAN class="operator token">=</SPAN>None<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># nme='Bob'</SPAN> <SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN>name<SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
script2.py
<SPAN class="comment token"># -*- coding: utf-8 -*-</SPAN> <SPAN class="string token">""" script2.py """</SPAN> <SPAN class="keyword token">def</SPAN> <SPAN class="token function">hello</SPAN><SPAN class="punctuation token">(</SPAN>nme<SPAN class="operator token">=</SPAN>None<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">if</SPAN> nme <SPAN class="keyword token">is</SPAN> None<SPAN class="punctuation token">:</SPAN> nme <SPAN class="operator token">=</SPAN> <SPAN class="string token">"Hello You"</SPAN> <SPAN class="keyword token">else</SPAN><SPAN class="punctuation token">:</SPAN> nme <SPAN class="operator token">=</SPAN> <SPAN class="string token">"Hello {}! "</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>nme<SPAN class="punctuation token">)</SPAN> nme <SPAN class="operator token">+=</SPAN> <SPAN class="string token">" called from script2"</SPAN> <SPAN class="keyword token">return</SPAN> nme<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
Run script1, you can change line 6 in script 1 to test
runfile<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'C:/Temp/script1.py'</SPAN><SPAN class="punctuation token">,</SPAN> wdir<SPAN class="operator token">=</SPAN><SPAN class="string token">'C:/Temp'</SPAN><SPAN class="punctuation token">)</SPAN> Reloaded modules<SPAN class="punctuation token">:</SPAN> script2 Hello You called <SPAN class="keyword token">from</SPAN> script2 <SPAN class="comment token"># ---- changed input to script 1</SPAN> runfile<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'C:/Temp/script1.py'</SPAN><SPAN class="punctuation token">,</SPAN> wdir<SPAN class="operator token">=</SPAN><SPAN class="string token">'C:/Temp'</SPAN><SPAN class="punctuation token">)</SPAN> Reloaded modules<SPAN class="punctuation token">:</SPAN> script2 Hello Bob! called <SPAN class="keyword token">from</SPAN> script2<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
If the scripts are in the same location as the other scripts, why don't you just import those
ie.
Script1 is your main script (module) and script 2 is in the same location
Add this line
from script2 import PhiLamdah2XYZ, XYZ2PhiLamdah
I have added a Toolbox named "Test", in which 4 password-protected scripts are imported. Script script2 (imported in the Toolbox "Test") with user-defined parameters is used to call the other ones (depending on the defined parameters). In the scripts I have defined functions. e.g. script1.py
def PhiLamdah2XYZ(Phi, Lamda, h, a, f):
...
return(X,Y,Z)
def XYZ2PhiLamdah(X, Y, Z, a, f):
return(GeodCoord_Dall)
Script Script2.py imports the Toolbox "Test":
relPath = os.path.dirname(__file__)
PythonTBX = relPath + r"\Test.tbx"
arcpy.ImportToolbox(PythonTBX)
Is there a way to call the functions PhiLamdah2XYZ and XYZ2PhiLamdah from Script2.py?
Why are you calling a script in your toolbox? and from where?
Are you not using the toolbox inside of arcmap or ArcGIS pro?
Is it a python toolbox or a conventional toolbox?
Adding toolboxes in Python—Geoprocessing and Python | ArcGIS Desktop
Have you imported your toolbox first?
In the following example, the ImportToolbox function is used to allow tools contained in a custom toolbox to be accessed in Python. After importing the toolbox, the custom tools can be accessed as arcpy.<toolname>_<alias>.>>> arcpy.ImportToolbox("c:/mytools/geometrytools.tbx") >>> arcpy.CreateRegularPolygons_geometry(
In the following example, the ImportToolbox function is used to allow tools contained in a custom toolbox to be accessed in Python. After importing the toolbox, the custom tools can be accessed as arcpy.<toolname>_<alias>.
>>> arcpy.ImportToolbox("c:/mytools/geometrytools.tbx") >>> arcpy.CreateRegularPolygons_geometry(
You might want to elaborate on your situation more
Aangemelde leden kunnen berichten plaatsen, updates volgen en meer. Nieuw hier? Registreer een gratis account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.