Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You're 3 years 8 months wiser since you posted so I'm assuming you'd have figured the first issue out, but given that a solution has not yet been posted (primarily because no one seemed to have a problem with the first issue), the following is my solution.</p> <h2>[UPDATED]</h2> <p>The last solution I provided was incorrect. Below I am providing the correct solution and explaining it in detail using code that I executed.</p> <p><strong>The problem is inherent in Python's <code>execfile()</code> builtin. Its one of the reasons that function has been deprecated in Python 3.x.</strong></p> <p>When you executed <code>execfile()</code> inside <code>runme()</code>, the objects <code>spam()</code> and <code>eggs()</code> were loaded into method <code>runme()</code>'s namespace, and not into the global namespace <em>(as they should ideally be)</em>. Consider the following code:</p> <h1>myscript.py</h1> <pre><code>def spam(): print 'spam' def eggs(): if 'spam' not in globals(): print 'method spam() is not present in global namespace' spam() try: eggs() except Exception as e: print e </code></pre> <h1>mainprogram.py</h1> <pre><code>class mainprogram(): def runme(self): execfile("myscript.py") print 'Objects lying in local namespace of runme() are -' print locals() this = mainprogram() this.runme() </code></pre> <h1>Interpreter Output</h1> <pre><code>&gt;&gt;&gt;import mainprogram method spam() is not present in global namespace name 'spam' is not defined Objects lying in local namespace of runme() are - {'e': NameError("name 'spam' is not defined",), 'spam': &lt;function spam at 0x000000000000002B&gt;, 'eggs': &lt;function eggs at 0x000000000000002C&gt;, 'self': &lt;mainprogram.mainprogram instance at 0x000000000000002D&gt;} </code></pre> <p>From the output you can see that <code>spam()</code> is not in the global namespace, but in method <code>runme()</code>'s namespace. So hypothetically, the correct way to call <code>spam()</code> would have been</p> <pre><code>def eggs(): global this this.runme.spam() </code></pre> <p>However, there is no way to access <code>spam()</code> while it is lying inside <code>runme()</code>'s namespace. The solution, therefore, is to insert <code>spam()</code> in the global namespace as follows:</p> <h1>myscript.py</h1> <pre><code>global spam def spam(): print "spam" def eggs(): spam() eggs() </code></pre> <p>This will ensure that a reference to object <code>spam()</code> is created inside the <code>globals()</code> dictionary (i.e., the global namespace), making it callable from <code>eggs()</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload