如何在不重新启动程序并手动执行的情况下保留导入?
我试过这个:
class runProgram:
def cmdEval(self,data):
try:
return str(repr(eval(data)))
except Exception as e:
return e
def cmdImport(self,data):
try:
__import__(data)
return "Imported."
except:
return "Error to import"
def run(self):
while True:
command = input("Command: ")
command,data = command.split(" ",1)
if command == "ev": print(self.cmdEval(data))
elif command == "imp": print(self.cmdImport(data))
然后我做了以下事情:
>>> runProgram().run()
Command: imp time
Imported.
Command: ev time.time()
name 'time' is not defined
结果没有用,正如我预期的那样,但他们是否可以在不使用保存数据的情况下动态导入?我的意思是我希望能够使用它,但我不想在我重新启动后使用它我想,
imp time
ev time.time()
>1383535034.20894
>>> ================================ RESTART ================================
>>> time.time()
Traceback (most recent call last):
File "<pyshell#238>", line 1, in <module>
time.time()
NameError: name 'time' is not defined
这可能吗?