I created two script files one script file will take user input as a number and i have another script which has to be run those many times the number given by user.Please help me with this?I have tried to call that scrip by using subprocess.Popen method but i am not getting any output.It is getting terminated.
The scripts would help to narrow down issues.
Why the need to run that way, when you can simply run a function (imported from another script/module) any number of times you want.
Can u please elloborate or provide any example.Actually these scripts are the toolbox scripts for the creation of tools in arcmap
I am using PRO as the demo, but the principle is the same... in pictoral form
The tool in action... allow the user to input many names into the toolbos
increment a counter, calling a function that is stored in the 'helper' script. It worked
What you have to do.....
Assign a script to the tool
Set up the parameters... we have a single parameter that allows for multiple names to be input
Now everything... scripts and toolbox are stored in the same folder
Here is the code
Notice in line 16, I import a function from dummy_helper and use it to print some names with a counter
<SPAN class="comment token"># -*- coding: UTF-8 -*-</SPAN> <SPAN class="string token">""" :Script: dummy_main.py <SPAN>:Author: </SPAN><A class="jive-link-email-small" href="mailto:Dan.Patterson@carleton.ca" rel="nofollow noopener noreferrer" target="_blank">Dan.Patterson@carleton.ca</A> :Modified: 2018-xyz-xyz :Purpose: tools for working with numpy arrays :Useage: : :References: : :---------------------------------------------------------------------: """</SPAN> <SPAN class="comment token"># ---- imports, formats, constants ----</SPAN> <SPAN class="keyword token">import</SPAN> sys <SPAN class="keyword token">import</SPAN> arcpy <SPAN class="keyword token">from</SPAN> dummy_helper <SPAN class="keyword token">import</SPAN> dumb_demo <SPAN class="keyword token">def</SPAN> <SPAN class="token function">tweet</SPAN><SPAN class="punctuation token">(</SPAN>msg<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"""Print a message for both arcpy and python. : msg - a text message """</SPAN> m <SPAN class="operator token">=</SPAN> <SPAN class="string token">"\n{}\n"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>msg<SPAN class="punctuation token">)</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>AddMessage<SPAN class="punctuation token">(</SPAN>m<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN>m<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">if</SPAN> len<SPAN class="punctuation token">(</SPAN>sys<SPAN class="punctuation token">.</SPAN>argv<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">==</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">:</SPAN> testing <SPAN class="operator token">=</SPAN> <SPAN class="token boolean">True</SPAN> names <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="string token">'Hello There'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'How are you'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'Goodbye'</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="keyword token">else</SPAN><SPAN class="punctuation token">:</SPAN> names <SPAN class="operator token">=</SPAN> sys<SPAN class="punctuation token">.</SPAN>argv<SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">.</SPAN>split<SPAN class="punctuation token">(</SPAN><SPAN class="string token">";"</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">if</SPAN> names <SPAN class="keyword token">is</SPAN> None<SPAN class="punctuation token">:</SPAN> tweet<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"no names"</SPAN><SPAN class="punctuation token">)</SPAN> cnt <SPAN class="operator token">=</SPAN> <SPAN class="number token">0</SPAN> <SPAN class="keyword token">for</SPAN> i <SPAN class="keyword token">in</SPAN> names<SPAN class="punctuation token">:</SPAN> cnt <SPAN class="operator token">+=</SPAN> <SPAN class="number token">1</SPAN> tweet<SPAN class="punctuation token">(</SPAN>dumb_demo<SPAN class="punctuation token">(</SPAN>cnt<SPAN class="punctuation token">,</SPAN> i<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># ----------------------------------------------------------------------</SPAN> <SPAN class="comment token"># __main__ .... code section</SPAN> <SPAN class="keyword token">if</SPAN> __name__ <SPAN class="operator token">==</SPAN> <SPAN class="string token">"__main__"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"""Optionally... : - print the script source name. : - run the _demo """</SPAN> <SPAN class="comment token"># print("Script... {}".format(script))</SPAN><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></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><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></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
Here is the helper script.... now!!! I tend to use a helper script that contains functions that I use all the time, so you don't have to replicate them in every toolbox that you use.
<SPAN class="comment token"># -*- coding: UTF-8 -*-</SPAN> <SPAN class="string token">""" :Script: dummy_helper.py <SPAN>:Author: </SPAN><A class="jive-link-email-small" href="mailto:Dan.Patterson@carleton.ca" rel="nofollow noopener noreferrer" target="_blank">Dan.Patterson@carleton.ca</A> :Modified: 2018-xyz-xyz :Purpose: tools for working with numpy arrays :Useage: : :References: : :---------------------------------------------------------------------: """</SPAN> <SPAN class="comment token"># ---- imports, formats, constants ----</SPAN> <SPAN class="keyword token">import</SPAN> sys <SPAN class="keyword token">def</SPAN> <SPAN class="token function">dumb_demo</SPAN><SPAN class="punctuation token">(</SPAN>cnt<SPAN class="punctuation token">,</SPAN> name<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">""" : docs """</SPAN> <SPAN class="keyword token">return</SPAN> <SPAN class="string token">"({}) {}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>cnt<SPAN class="punctuation token">,</SPAN> name<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># ----------------------------------------------------------------------</SPAN> <SPAN class="comment token"># __main__ .... code section</SPAN> <SPAN class="keyword token">if</SPAN> __name__ <SPAN class="operator token">==</SPAN> <SPAN class="string token">"__main__"</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">"""Optionally... : - print the script source name. : - run the _demo """</SPAN> <SPAN class="comment token"># print("Script... {}".format(script))</SPAN> <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></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><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
Hope you get the drift.
Thank you so much for your help sir.I have a Graphical User Interface in the second script also.Here in your script it is executing a function,but i also need to a call the GUI in the second Script.Please help us.
Code needed... and if you aren't using a GUI that plays nice with arcmap or pro, then you will have some problems.
I would advise against it and either use the standard toolbox approach or use python toolboxes.
the subprocess module would be an option in pro, but I don't know what you would gain.
Perhaps a full description of your desired workflow with the functional code would help
Actually we have designed the user Interface using script in arcmap toolbox(.tbx).So I am sending the screen shots and the code which has to be called many times.
I
Input 1:It will take the number of parameters needed and this will be the count that the second image need to be called.
Input 2:It has to be executed, as per the input given in first UI.
Code for Second UI(ie second image)
# Import arcpy moduleimport arcpy
# Local variables:filename = arcpy.GetParameterAsText(0)Field = arcpy.GetParameterAsText(1)Select = arcpy.GetParameterAsText(2)max = arcpy.GetParameterAsText(3)min = arcpy.GetParameterAsText(4)expression = "getClass(!{0}!,{1},{2})".format(Select,min,max)codeblock = """def getClass(i, min, max): a = 123 if (i>min and i<max): return 1 return 0"""
# Process: Add Fieldarcpy.AddField_management(filename, Field, "LONG", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")arcpy.CalculateField_management(filename, Field, expression, "PYTHON_9.3", codeblock)
Note that this is a cross-post of three additional questions on GIS StackExchange, all of which were closed for lack of code. The best way to get help with code is to provide code.
- V
Thanks Vince... could you kill this thread then, I have offered suggestions but no one else seems to have any
Thank you so much for your valuable time and suggestions sir.Bye.
Les membres connectés peuvent publier, suivre les mises à jour, et plus encore. Nouveau ici ? Inscrivez-vous gratuitement.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.