<?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 Import module - undefined global variable in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/import-module-undefined-global-variable/m-p/183387#M14090</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I thought I understood and fixed this problem:&amp;nbsp; A variable defined in a module remains undefined after I import the module.&amp;nbsp; Here is the simplified version.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The main script and the module are in the same folder/dir.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This is my module:&amp;nbsp; mysettings.py&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;

def main():

&amp;nbsp;&amp;nbsp;&amp;nbsp; myvar = "This is imported."

if __name__ == '__main__':
&amp;nbsp;&amp;nbsp;&amp;nbsp; main()

&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This is my script:&amp;nbsp; irp.py:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;

import mysettings

def main():

&amp;nbsp;&amp;nbsp;&amp;nbsp; print myvar

if __name__ == '__main__':
&amp;nbsp;&amp;nbsp;&amp;nbsp; main()

&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Interpreter error:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;NameError: global name 'myvar' is not defined&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Can you tell me why?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks,&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Mon, 04 Jun 2012 15:00:30 GMT</pubDate>
    <dc:creator>DonBarker</dc:creator>
    <dc:date>2012-06-04T15:00:30Z</dc:date>
    <item>
      <title>Import module - undefined global variable</title>
      <link>https://community.esri.com/t5/python-questions/import-module-undefined-global-variable/m-p/183387#M14090</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I thought I understood and fixed this problem:&amp;nbsp; A variable defined in a module remains undefined after I import the module.&amp;nbsp; Here is the simplified version.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The main script and the module are in the same folder/dir.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This is my module:&amp;nbsp; mysettings.py&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;

def main():

&amp;nbsp;&amp;nbsp;&amp;nbsp; myvar = "This is imported."

if __name__ == '__main__':
&amp;nbsp;&amp;nbsp;&amp;nbsp; main()

&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This is my script:&amp;nbsp; irp.py:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;

import mysettings

def main():

&amp;nbsp;&amp;nbsp;&amp;nbsp; print myvar

if __name__ == '__main__':
&amp;nbsp;&amp;nbsp;&amp;nbsp; main()

&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Interpreter error:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;NameError: global name 'myvar' is not defined&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Can you tell me why?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks,&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 04 Jun 2012 15:00:30 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/import-module-undefined-global-variable/m-p/183387#M14090</guid>
      <dc:creator>DonBarker</dc:creator>
      <dc:date>2012-06-04T15:00:30Z</dc:date>
    </item>
    <item>
      <title>Re: Import module - undefined global variable</title>
      <link>https://community.esri.com/t5/python-questions/import-module-undefined-global-variable/m-p/183388#M14091</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;You need to do something like this in your irp.py&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;mysettings.main()&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;When you import a module it isn't executed as the __main__ so it needs to be called by function if you have it setup that way.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 04 Jun 2012 15:15:09 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/import-module-undefined-global-variable/m-p/183388#M14091</guid>
      <dc:creator>MathewCoyle</dc:creator>
      <dc:date>2012-06-04T15:15:09Z</dc:date>
    </item>
    <item>
      <title>Re: Import module - undefined global variable</title>
      <link>https://community.esri.com/t5/python-questions/import-module-undefined-global-variable/m-p/183389#M14092</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Matthew,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks for taking time to reply.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I'm a Python novice so don't understand how/where to use this in my script.&amp;nbsp; Can you explain?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 04 Jun 2012 15:20:23 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/import-module-undefined-global-variable/m-p/183389#M14092</guid>
      <dc:creator>DonBarker</dc:creator>
      <dc:date>2012-06-04T15:20:23Z</dc:date>
    </item>
    <item>
      <title>Re: Import module - undefined global variable</title>
      <link>https://community.esri.com/t5/python-questions/import-module-undefined-global-variable/m-p/183390#M14093</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Your mysettings.py should be something along these lines, depending on what it does.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;def main():

&amp;nbsp;&amp;nbsp;&amp;nbsp; myvar = "This is imported."
&amp;nbsp;&amp;nbsp;&amp;nbsp; return myvar

if __name__ == '__main__':
&amp;nbsp;&amp;nbsp;&amp;nbsp; main()&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Your irp.py should be something like this.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import mysettings

def main():

&amp;nbsp;&amp;nbsp;&amp;nbsp; print mysettings.main()

if __name__ == '__main__':
&amp;nbsp;&amp;nbsp;&amp;nbsp; main()&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 09:20:21 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/import-module-undefined-global-variable/m-p/183390#M14093</guid>
      <dc:creator>MathewCoyle</dc:creator>
      <dc:date>2021-12-11T09:20:21Z</dc:date>
    </item>
    <item>
      <title>Re: Import module - undefined global variable</title>
      <link>https://community.esri.com/t5/python-questions/import-module-undefined-global-variable/m-p/183391#M14094</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Got it now.&amp;nbsp; Thanks again for taking time to reply and explain.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 06 Jun 2012 15:54:00 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/import-module-undefined-global-variable/m-p/183391#M14094</guid>
      <dc:creator>DonBarker</dc:creator>
      <dc:date>2012-06-06T15:54:00Z</dc:date>
    </item>
  </channel>
</rss>

