title: Python import as global name not defined
body: <p>I have an application that runs on Postgres &amp; Mysql.  Each program checks to determine the database and then imports either postgres_db as db_util or mysql_dt as db_util.  All works well when code in main references db_util, but if a class is imported, the reference to db_util is not defined.</p>
  
  <p>I created the following to classes and main to test the problem and found another interesting side effect. Classes B &amp; C reference ClassA under different import cases.  B &amp; C are identical except B is in main and C is imported.</p>
  
  <p>ClassX.py</p>
  
  <pre><code>class ClassA(object):
      def print_a(self):
          print "this is class a"
  
  class ClassC(object):
      def ref_a(self):
          print 'from C ref a  ==&gt;',
          xa=ClassA()
          xa.print_a()
      def ref_ca(self):
          print 'from C ref ca ==&gt;',
          xa=ca()
          xa.print_a()
  </code></pre>
  
  <p>test_scope.py</p>
  
  <pre><code>from classes.ClassX import ClassA
  from classes.ClassX import ClassA as ca
  from classes.ClassX import ClassC as cb
  
  
  class ClassB(object):
      def ref_a(self):
          print 'from B ref a  ==&gt;',
          xa=ClassA()
          xa.print_a()
      def ref_ca(self):
          print 'from B ref ca ==&gt;',
          xa=ca()
          xa.print_a()
  
  print 'globals:',dir()
  print 'modules','ca:',ca,'cb:',cb,'CA:',ClassA
  print ''
  print 'from main'
  xb=ClassB()
  xb.ref_a()
  xb.ref_ca()
  
  print ''
  print 'from imports'
  xbs=cb()
  xbs.ref_a()
  xbs.ref_ca()
  </code></pre>
  
  <p>And the results:</p>
  
  <pre><code>globals: ['ClassA', 'ClassB', '__builtins__', '__doc__', '__file__', '__name__', '__package__', 'ca', 'cb']
  modules ca: &lt;class 'classes.ClassX.ClassA'&gt; cb: &lt;class 'classes.ClassX.ClassC'&gt; CA: &lt;class 'classes.ClassX.ClassA'&gt;
  
  from main
  from B ref a  ==&gt; this is class a
  from B ref ca ==&gt; this is class a
  
  from imports
  from C ref a  ==&gt; this is class a
  from C ref ca ==&gt;
  Traceback (most recent call last):
    File "test_scope.py", line 32, in &lt;module&gt;
      xbs.ref_ca()
    File "R:\python\test_scripts\scope\classes\ClassX.py", line 13, in ref_ca
      xa=ca()
  NameError: global name 'ca' is not defined
  Press any key to continue . . .
  </code></pre>
  
  <p>From my test, I see that the object ca (imported as) is not available to the ClassC, however, the module ClassA is available (imported without as).</p>
  
  <ol>
  <li>Why the difference between import and import as behavior?  I am unclear why mains globals are not available to classes main imports.</li>
  <li>What is a good approach to dynamically determine appropriate db_util module to import and have it accessible to other imported classes? </li>
  </ol>
  
  <p><strong>Update:</strong>
  After reading yet another post on Namespaces:  "<em><a href="https://stackoverflow.com/questions/15959534/python-visibility-of-global-variables-from-imported-modules?rq=1">Visibility of global variables from imported modules</a></em>", I understand that in my example above the reason classA is available to ClassC is that A &amp; C are in the same imported file, thus the same namespace.</p>
  
  <p>So the remaining question is a design question:</p>
  
  <p>if I have code like this:</p>
  
  <pre><code>if db == 'MySQL':
      from mysql_db import db_util
  elif db == 'Postgres'
      from postgres_db import db_util
  </code></pre>
  
  <p>What is a good approach to make db_util available to all imported modules?</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>from the reponse by Blckknght, I added the code</p>
  
  <pre><code>cb.ca =ca
  </code></pre>
  
  <p>to the scope_test script.  This requires the class call to <strong>xa=ca()</strong> be changed to <strong>xa=self.ca()</strong>.  I also think that adding objects to a class from outside the class, though Python allows it, is not a good design methodology and will make debugging a nightmare.</p>
  
  <p>However, since I think modules and classes should be standalone or specifically declare their dependencies, I am going to implement the class like this, using the code sample from above.</p>
  
  <p>break out ClassA and ClassC to separate modules and at the top of ClassC, before the class definition, do the imports</p>
  
  <pre><code>from ClassA import ClassA
  from ClassA import ClassA as ca
  
  class ClassB(object):
  </code></pre>
  
  <p>and in my real situation, where I need to import the db_util module into several modules</p>
  
  <p>ci.py           #new module to select class for appropriate db</p>
  
  <pre><code>if db == 'MySQL':
      from mysql_db import db_util
  elif db == 'Postgres'
      from postgres_db import db_util
  </code></pre>
  
  <p>in each module needing the db_util class</p>
  
  <pre><code>import ci
  db_util=ci.db_util         #add db_util to module globals
  
  class Module(object):
  </code></pre>
  
  <p>One problem with this is it requires each module using the db_util to import it, but it does make the dependencies known.</p>
  
  <p>I will close this question and want to thank Blckknght and Armin Rigo for their responses which help clarify this issue for me.  I would appreciate any design related feedback.</p>