I'm having (what I assume is) a simple problem regarding the way import and execfile interact.  I apologize in advance for my naivete.<br><br>Lets say I have the function:<br><br>   def Func1():<br>       print dir()<br>
       execfile('testfile')<br>       print dir()<br>       X<br><br>and the file<br><br>   #file: testfile<br>      X = 3<br><br><br>Then,  when I run Func1 , I get the output:<br><br>   >>> Func1()<br>   []<br>
   ['X']<br>   Traceback (most recent call last):<br>     File "<stdin>", line 1, in <module><br>     File "<stdin>", line 5, in Func1<br>   NameError: global name 'X' is not defined<br>
<br><br>SO, I have three questions:<br>    1) Naively, I would think that the call to "execfile" in Func1 would act as if the line was replaced with the lines of 'testfile'.  But obviously not, in some way that has to do with the subtlety of the python compilation process.   Can someone explain exactly what the problem is?<br>
    2) Why would something show up in the dir() call, but not be defined (e.g. like 'X' did in the second dir() call in Func1()) ?<br>    3) Is there any way to fix this that does not involved (essentially in one way or another) importing "testfile" as a py module?   I would like to avoid that scenario for a variety of reasons ... <br>
<br>Thanks!<br><br>Dan<br><br>