<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Can I access global variables in called python? in ArcGIS API for Python Questions</title>
    <link>https://community.esri.com/t5/arcgis-api-for-python-questions/can-i-access-global-variables-in-called-python/m-p/826920#M3049</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Trick one to explain but I will give it a go with simplified code.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I originally had a python script that had some global variables defined at the top, some functions and then the main code in a "try" statement. eg&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;PythonScript.py&lt;/P&gt;&lt;BLOCKQUOTE class="jive_macro_quote jive-quote jive_text_macro"&gt;&lt;P&gt;import arcpy, os, sys&lt;/P&gt;&lt;P&gt;arcpy.env.workspace = arcpy.GetParameterAsText(0)&lt;BR /&gt;LogAllEvents = arcpy.GetParameterAsText(1)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;globalVariable1 = "Global1"&lt;/P&gt;&lt;P&gt;globalVariable2 = "Global2"&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;sdeConn = arcpy.ArcSDESQLExecute(arcpy.env.workspace)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;def somefunc(Global1):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;print Global1&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;def someotherfunc(Global2)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;global Global1&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;print&amp;nbsp;Global2&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;Global1 = Global2&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;try:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;print "Starting Process"&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;somefunc(Global1)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; someotherfunc(Global2)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;#now carry out&amp;nbsp;some&amp;nbsp;logic using sdeconn&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I now have realized I need a method of calling this python script to perform the main functionality, so have altered this as follows:&lt;/P&gt;&lt;P&gt;PythonScript.py&lt;/P&gt;&lt;BLOCKQUOTE class="jive_macro_quote jive-quote jive_text_macro"&gt;&lt;P&gt;import arcpy, os, sys&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;def somefunc(Global1):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;print Global1&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;def someotherfunc(Global2)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;global Global1&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;print&amp;nbsp;Global2&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;Global1 = Global2&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;def main(arcpy.env.workspace, LogAllEvents&amp;nbsp;)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;print "Starting Process"&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;globalVariable1 = "Global1"&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;globalVariable2 = "Global2"&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;sdeConn = arcpy.ArcSDESQLExecute(arcpy.env.workspace)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;somefunc(Global1)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; someotherfunc(Global2)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;#now carry out&amp;nbsp;some&amp;nbsp;logic using sdeconn&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;if __name__ == "__main__":&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;main(dummy,dummy)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I will always call the main() from another python script so please ignore the details of the "if __name__" statement above. My call from the other script will be:&lt;/P&gt;&lt;P&gt;OtherScript.py&lt;/P&gt;&lt;BLOCKQUOTE class="jive_macro_quote jive-quote jive_text_macro"&gt;&lt;P&gt;import arcpy, os, sys, PythonScript&lt;/P&gt;&lt;P&gt;arcpy.env.workspace = arcpy.GetParameterAsText(0)&lt;/P&gt;&lt;P&gt;LogAllEvents = "Y"&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;PythonScript.main(arcpy.env.workspace, LogAllEvents)&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;As you can see, in PythonScript, I moved all global variables into the function main().&lt;/P&gt;&lt;P&gt;I had to do this as it was a function that I would be calling directly from another script. Could I have left these at the top of the script and accessed in main() by defining them as global? I understand these are accessible when it is the default body of code being called but was not sure calling a function from another script.&lt;/P&gt;&lt;P&gt;I have simplified my scripts above. In real life there are dozens of globals and I need to get my head around this before I start shifting variables wholesale into main().&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 08 Feb 2017 16:13:28 GMT</pubDate>
    <dc:creator>MarkWingfield</dc:creator>
    <dc:date>2017-02-08T16:13:28Z</dc:date>
    <item>
      <title>Can I access global variables in called python?</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/can-i-access-global-variables-in-called-python/m-p/826920#M3049</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Trick one to explain but I will give it a go with simplified code.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I originally had a python script that had some global variables defined at the top, some functions and then the main code in a "try" statement. eg&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;PythonScript.py&lt;/P&gt;&lt;BLOCKQUOTE class="jive_macro_quote jive-quote jive_text_macro"&gt;&lt;P&gt;import arcpy, os, sys&lt;/P&gt;&lt;P&gt;arcpy.env.workspace = arcpy.GetParameterAsText(0)&lt;BR /&gt;LogAllEvents = arcpy.GetParameterAsText(1)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;globalVariable1 = "Global1"&lt;/P&gt;&lt;P&gt;globalVariable2 = "Global2"&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;sdeConn = arcpy.ArcSDESQLExecute(arcpy.env.workspace)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;def somefunc(Global1):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;print Global1&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;def someotherfunc(Global2)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;global Global1&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;print&amp;nbsp;Global2&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;Global1 = Global2&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;try:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;print "Starting Process"&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;somefunc(Global1)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; someotherfunc(Global2)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;#now carry out&amp;nbsp;some&amp;nbsp;logic using sdeconn&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I now have realized I need a method of calling this python script to perform the main functionality, so have altered this as follows:&lt;/P&gt;&lt;P&gt;PythonScript.py&lt;/P&gt;&lt;BLOCKQUOTE class="jive_macro_quote jive-quote jive_text_macro"&gt;&lt;P&gt;import arcpy, os, sys&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;def somefunc(Global1):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;print Global1&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;def someotherfunc(Global2)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;global Global1&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;print&amp;nbsp;Global2&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;Global1 = Global2&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;def main(arcpy.env.workspace, LogAllEvents&amp;nbsp;)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;print "Starting Process"&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;globalVariable1 = "Global1"&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;globalVariable2 = "Global2"&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;sdeConn = arcpy.ArcSDESQLExecute(arcpy.env.workspace)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;somefunc(Global1)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; someotherfunc(Global2)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;#now carry out&amp;nbsp;some&amp;nbsp;logic using sdeconn&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;if __name__ == "__main__":&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;main(dummy,dummy)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I will always call the main() from another python script so please ignore the details of the "if __name__" statement above. My call from the other script will be:&lt;/P&gt;&lt;P&gt;OtherScript.py&lt;/P&gt;&lt;BLOCKQUOTE class="jive_macro_quote jive-quote jive_text_macro"&gt;&lt;P&gt;import arcpy, os, sys, PythonScript&lt;/P&gt;&lt;P&gt;arcpy.env.workspace = arcpy.GetParameterAsText(0)&lt;/P&gt;&lt;P&gt;LogAllEvents = "Y"&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;PythonScript.main(arcpy.env.workspace, LogAllEvents)&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;As you can see, in PythonScript, I moved all global variables into the function main().&lt;/P&gt;&lt;P&gt;I had to do this as it was a function that I would be calling directly from another script. Could I have left these at the top of the script and accessed in main() by defining them as global? I understand these are accessible when it is the default body of code being called but was not sure calling a function from another script.&lt;/P&gt;&lt;P&gt;I have simplified my scripts above. In real life there are dozens of globals and I need to get my head around this before I start shifting variables wholesale into main().&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 08 Feb 2017 16:13:28 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/can-i-access-global-variables-in-called-python/m-p/826920#M3049</guid>
      <dc:creator>MarkWingfield</dc:creator>
      <dc:date>2017-02-08T16:13:28Z</dc:date>
    </item>
  </channel>
</rss>

