[英]How do I execute a python script that is stored on the internet?
我使用python 2.4作为一个程序,它从互联网上导入脚本并执行它们,以便作者可以更改脚本,用户不必重新下载脚本。
这是下载脚本的程序的一部分:
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
非常奇怪的是,当我跑步时
execfile(script path)
在函数外部,下载脚本后,脚本正确执行。 但是,尝试运行loadScript函数会引发脚本中的名称错误,即使这些名称已经在脚本中导入,并且在我发现非常奇怪的exec文件之前。
所以我的问题是:我是否使用非常糟糕的方法来下载和执行这些脚本?
很抱歉,如果之前已经回答了这个问题,但我似乎找不到其他人试图通过从互联网上下载来运行python脚本。
编辑:添加globals
作为execfile
另一个参数似乎现在解决了问题。 我不知道以后是否会出现任何其他问题。
在R中你可以简单地'来源(url)'。 这是我到目前为止在python中找到的最接近的:
import urllib
(fn,hd) = urllib.urlretrieve('http://host.com/file.py')
execfile(fn)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.