<?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 problem using python modules in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/problem-using-python-modules/m-p/434010#M34113</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I have a rather basic python question re: modules&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I have a python script that's essentially structured as follows:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;======================================&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;## aaa.py&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;func1():&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp; ...some code...&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;...arcpy.AddMessage("message")&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;...return&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;func2():&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ...some code...&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;...arcpy.AddMessage("message")&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;...return&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;If __name__ = "main":&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;...import sys, arcpy, argparse, os, time&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ...some code...&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;...func1()&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;...func2()&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;...return&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;======================================&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I want to write another script that uses the functions within aaa.py.&amp;nbsp; So I import aaa.py as a module into this other script (see bleow).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;======================================&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;## bbb.py&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;import sys, arcpy, argparse, os, time, aaa&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;some code...&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;aaa.func1()&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;=======================================&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;When I execute aaa all is fine.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;When I execute bbb, it returns an error "NameError: global name 'arcpy' is not defined".&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Appreciate any insight on how to avoid this.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;b&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;PS - used ... in post above to show indent since spaces and tabs removed&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 06 Mar 2013 23:16:11 GMT</pubDate>
    <dc:creator>BKS</dc:creator>
    <dc:date>2013-03-06T23:16:11Z</dc:date>
    <item>
      <title>problem using python modules</title>
      <link>https://community.esri.com/t5/python-questions/problem-using-python-modules/m-p/434010#M34113</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I have a rather basic python question re: modules&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I have a python script that's essentially structured as follows:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;======================================&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;## aaa.py&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;func1():&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp; ...some code...&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;...arcpy.AddMessage("message")&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;...return&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;func2():&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ...some code...&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;...arcpy.AddMessage("message")&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;...return&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;If __name__ = "main":&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;...import sys, arcpy, argparse, os, time&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ...some code...&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;...func1()&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;...func2()&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;...return&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;======================================&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I want to write another script that uses the functions within aaa.py.&amp;nbsp; So I import aaa.py as a module into this other script (see bleow).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;======================================&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;## bbb.py&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;import sys, arcpy, argparse, os, time, aaa&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;some code...&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;aaa.func1()&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;=======================================&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;When I execute aaa all is fine.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;When I execute bbb, it returns an error "NameError: global name 'arcpy' is not defined".&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Appreciate any insight on how to avoid this.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;b&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;PS - used ... in post above to show indent since spaces and tabs removed&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 06 Mar 2013 23:16:11 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/problem-using-python-modules/m-p/434010#M34113</guid>
      <dc:creator>BKS</dc:creator>
      <dc:date>2013-03-06T23:16:11Z</dc:date>
    </item>
    <item>
      <title>Re: problem using python modules</title>
      <link>https://community.esri.com/t5/python-questions/problem-using-python-modules/m-p/434011#M34114</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;This was difficult for me to understand at first as well.&amp;nbsp; The top level program is the module that has all of your functions living inside of it (aaa.py in your case).&amp;nbsp; It does not appear that you have imported the arcpy module into your "aaa.py" script.&amp;nbsp; You need to do this at the top of this module.&amp;nbsp; Even though you have imported arcpy in bbb.py, this is outside the &lt;/SPAN&gt;&lt;STRONG&gt;scope&lt;/STRONG&gt;&lt;SPAN&gt; of the functions you are importing from aaa.py.&amp;nbsp; Anything outside of a function is a global variable, anything inside of a function is a local variable.&amp;nbsp; If you have functions without any input parameters, they simply cannot "see" any of the global variables that are outside of its scope, i.e. outside of the function.&amp;nbsp; You can see the name of the module that any function when you import it into another program with this simple test: &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
# aaa.py

import arcpy

def func1():
&amp;nbsp;&amp;nbsp;&amp;nbsp; # some code
&amp;nbsp;&amp;nbsp;&amp;nbsp; print 'Name =&amp;nbsp; %s' %__name__
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage('Name =&amp;nbsp; %s' %__name__)
&amp;nbsp;&amp;nbsp;&amp;nbsp; return


func1()
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This will return:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;

&amp;gt;&amp;gt;&amp;gt; Name =&amp;nbsp; '__main__'

&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Whenever you execute this function in top level module the __name__ will always be '__main__'&amp;nbsp; whenever you run the aaa.py. However, if you import this function into bbb.py and run it you will get a different result:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
# bbb.py

import aaa

aaa.func1()
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This will return:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;

&amp;gt;&amp;gt;&amp;gt; Name =&amp;nbsp; 'aaa'

&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This is true because the __name__ from where func1() lives is always 'aaa', UNLESS it is ran inside "aaa.py", then the name will be '__main__'.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The only ways (that I know of, there are probably more ways) that you can pass global variables into functions is to pass them in as parameters or declare them as a global variable.&amp;nbsp; If you import arcpy into your aaa.py file, you should be able to call it inside of another module without having the error on importing arcpy (for running func1() that uses arcpy) in bbb.py.&amp;nbsp; Anything outside of the functions from aaa.py will be able to access the import arcpy at the top of bbb.py&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Conversely, the same can be true in the opposite case.&amp;nbsp; I imported arcpy into my 'aaa.py' script.&amp;nbsp; When I import aaa and call func1() arcpy is only available inside func1() because arcpy is not imported in bbb.py, even though arcpy is associated with aaa.func1().&amp;nbsp; If you want to use arcpy in bbb.py, it must be imported into this as well.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Whenever I write modules that contain functions I will use the "if __name__ ==&amp;nbsp; '__main__' " at the bottom so I can test the functions to make sure they work with some sample input variables.&amp;nbsp; That way whenever they are imported into another module the sample variables under the if name = main part of the top level module will not be used because the functions __name__ will never be __main__ when it is ran in another module.&amp;nbsp; I hope this makes sense.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 19:27:49 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/problem-using-python-modules/m-p/434011#M34114</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2021-12-11T19:27:49Z</dc:date>
    </item>
    <item>
      <title>Re: problem using python modules</title>
      <link>https://community.esri.com/t5/python-questions/problem-using-python-modules/m-p/434012#M34115</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hello Caleb1987,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks for all of that info.&amp;nbsp; I did know some of that but it's good to get more info and is appreciated.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Although I DO import arcpy into both aaa.py and bbb.py, you've made me realize why I'm getting the error I think.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;That is, when I run bbb.py which imports both arcpy and aaa.py, and attempt to execute aaa.func1(), the reason there is a problem with aaa.func1() not recognizing 'arcpy' is that it never gets imported into aaa.func1() since it's only imported in aaa.py when name == __main__ (i.e. when executing aaa.py directly).&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;So I do get this.&amp;nbsp; But what is the solution then?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Should I add "import arcpy" to each function within aaa.py ?&amp;nbsp; This does solve the problem but it doesn't seem like the right thing to do.&amp;nbsp; Not sure if this will create any issues (performance or otherwise).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Any thoughts?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Cheers, b&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 07 Mar 2013 01:55:09 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/problem-using-python-modules/m-p/434012#M34115</guid>
      <dc:creator>BKS</dc:creator>
      <dc:date>2013-03-07T01:55:09Z</dc:date>
    </item>
    <item>
      <title>Re: problem using python modules</title>
      <link>https://community.esri.com/t5/python-questions/problem-using-python-modules/m-p/434013#M34116</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Hello Caleb1987,&lt;BR /&gt;Although I DO import arcpy into both aaa.py and bbb.py, you've made me realize why I'm getting the error I think.&lt;BR /&gt;&lt;BR /&gt;That is, when I run bbb.py which imports both arcpy and aaa.py, and attempt to execute aaa.func1(), the reason there is a problem with aaa.func1() not recognizing 'arcpy' is that it never gets imported into aaa.func1() since it's only imported in aaa.py when name == __main__ (i.e. when executing aaa.py directly).&amp;nbsp; &lt;BR /&gt;&lt;BR /&gt;So I do get this.&amp;nbsp; But what is the solution then?&lt;BR /&gt;&lt;BR /&gt;Should I add "import arcpy" to each function within aaa.py ?&amp;nbsp; This does solve the problem but it doesn't seem like the right thing to do.&amp;nbsp; Not sure if this will create any issues (performance or otherwise).&lt;BR /&gt;&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Interesting you mention this....True, you do import arcpy in aaa.py, BUT you only import it IF the name is main.&amp;nbsp; So rather than doing it that way, you need to put the import arcpy at the very top above your def statements even (or at least above the name == main part).&amp;nbsp; Otherwise arcpy only gets imported if the name is main.&amp;nbsp; Do you see what I mean?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;So instead of &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt; def func1(): &amp;nbsp;&amp;nbsp;&amp;nbsp; # do something&amp;nbsp; if __name__ == '__main__':&amp;nbsp; &amp;nbsp;&amp;nbsp; import arcpy&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # this only gets imported when name is __main__&amp;nbsp; &amp;nbsp;&amp;nbsp; func1()&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Do THIS:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;import arcpy&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # this way arcpy is ALWAYS imported&amp;nbsp; def func1(): &amp;nbsp;&amp;nbsp;&amp;nbsp; # do something&amp;nbsp; if __name__ == '__main__':&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; func1() &lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This will be what makes the difference&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 07 Mar 2013 02:03:17 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/problem-using-python-modules/m-p/434013#M34116</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2013-03-07T02:03:17Z</dc:date>
    </item>
    <item>
      <title>Re: problem using python modules</title>
      <link>https://community.esri.com/t5/python-questions/problem-using-python-modules/m-p/434014#M34117</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks - that's so obvious I didn't see it.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Appreciate your patience.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;b&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 07 Mar 2013 02:11:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/problem-using-python-modules/m-p/434014#M34117</guid>
      <dc:creator>BKS</dc:creator>
      <dc:date>2013-03-07T02:11:22Z</dc:date>
    </item>
  </channel>
</rss>

