如何实现以下想法,其中不同的作业分开(隔离)而不退出相同的Python主会话。
>>> Session.Start()
>>> from sympy import *
>>> x = Symbol('x')
>>> ... #do the job
>>> Session.End()
和另一个使用其他包的会话:
>>> Session.Start()
>>> from numpy import *
>>> x = array([1,2,3,4])
>>> ... #do the job
>>> Session.End()
答案 0 :(得分:2)
虽然不推荐,但您可以这样做:
sys.modules
,sys.path
和全局变量的状态。你应该注意到这并不完美,不推荐。我真的看不出import sympy
而不是from sympy import *
的错误。
修改强>
似乎访问和修改全局范围并不容易,并且会让您遇到很多麻烦...请参阅此示例:
class Session(object):
def __init__(self, gref):
self.init()
self.gref = gref
def init(self):
self.modules = {}
self.path = None
self.glob = {}
def start(self):
import sys
self.modules = sys.modules.copy()
self.path = sys.path[:]
self.glob = self.gref.copy()
self.gref['test'] = 'abc'
def stop(self):
import sys
sys.modules = self.modules.copy()
sys.path = self.path[:]
for k in self.gref.keys():
del self.gref[k]
self.gref.update(self.glob)
def __repr__(self):
return repr(self.glob)
运行它:
Python 2.7.3 (default, Aug 1 2012, 05:16:07)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from sessionpy import Session
>>> s = Session(globals())
>>> before = 123
>>> s.start()
>>> after = 456
>>> s.stop()
>>> after
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'after' is not defined
Error in sys.excepthook:
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/apport_python_hook.py", line 66, in apport_excepthook
from apport.fileutils import likely_packaged, get_recent_crashes
File "/usr/lib/python2.7/dist-packages/apport/__init__.py", line 1, in <module>
from apport.report import Report
File "/usr/lib/python2.7/dist-packages/apport/report.py", line 18, in <module>
import problem_report
File "/usr/lib/python2.7/dist-packages/problem_report.py", line 15, in <module>
from email.encoders import encode_base64
File "/usr/lib/python2.7/email/__init__.py", line 115, in <module>
setattr(sys.modules['email'], _name, importer)
KeyError: 'email'
Original exception was:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'after' is not defined
>>> before
123
>>>
您可以看到它有效(因为after
会引发NameError
),但sys.excepthook
中的某些模块会出现另一个问题。我只能想象您将使用sympy
和numpy
两个巨大的库存在的问题。
注意:强> 如果你只想要一个简单的东西运行,你可以搞乱全局变量,你会很好。