title: How do I execute a python script that is stored on the internet?
body: <p>I am using python 2.4 for a program which imports scripts from the internet and executes them so a script could be changed by the author and the user wouldn't have to re-download the script.</p>
  
  <p>This is the part of the program that downloads the script:</p>
  
  <pre><code>def downloadScript(self,script):
      myfile=open('#A file path/'+script['name']+'.txt','w')
      try:
          downloadedScript=urllib.urlopen(script['location']).read()
      except:
          #raise error
          return
      myfile.write(downloadedScript)
      myfile.close()
  
  def loadScript(self):
      if not self.scriptCurrentlyLoaded:
          script=self.scripts[self.scroller.listPos]
          if script['location']=='None':
              #raise error
              return
          self.downloadScript(script)
          myfile=open('#A file path/'+script['name']+'.txt','r')
          for line in myfile:
              if line.startswith('from') or line.startswith('import'):
                  exec(line.strip()) #This was added because of the name errors
                                     #being produced but to no affect
          myfile.close()
          execfile('#A file path/'+script['name']+'.txt')
          self.scriptCurrentlyLoaded=True
          self.scriptLoaded=script
      else:
          #raise error
  </code></pre>
  
  <p>The very odd thing is that when I run</p>
  
  <pre><code>execfile(script path)
  </code></pre>
  
  <p>outside of the function, after the script has been downloaded, the script is executed correctly. But trying to run the loadScript function raises name errors in the script even though the names have been imported in the script and before the execfile which I find very odd.</p>
  
  <p>So my question is: Am I using a very bad method to download and execute these scripts?</p>
  
  <p>Sorry if this question was answered before but I can't seem to find anyone else who is trying to run python scripts by downloading them from the internet.</p>
  
  <p><strong>Edit:</strong> adding <code>globals</code> as another argument to the <code>execfile</code> has seemed to fix the problem for now. I don't know if any other problems will occur later though.</p>