sys.path在哪里被替换? [英] Where does sys.path get replaced?
问题描述
某些图书馆以我不想要的方式更改我的 sys.path
。
Some library does change my sys.path
in a way that I don't want it to.
但是我找不到它。受影响的virtualenv安装了很多库。
But I can't find it. The affected virtualenv has a lot of libraries installed.
我用一个改变了修改的类替换了sys.path,但这并没有帮助,因为代码似乎更改 sys.path
如下所示:
I replaced sys.path with a own class which alters the modifications, but this does not help, since the code seems to alter sys.path
like this:
sys.path= [...] + sys.path
我如何找到恶代码行及其堆栈跟踪?
How can I find the "evil" code line and its stack trace?
相关
- 调试sys.path 的修改
- Debugging modifications of sys.path
推荐答案
我发现这样的恶意代码行。
I found the evil code line like this.
我改变了sys.globals ['sys' ]在sitecustomize.py:
I alter sys.globals['sys'] in sitecustomize.py:
# sitecustomize.py
import sys
class AttributeIsReadonly(ValueError):
pass
class MakeModuleAttributesReadonly(object):
def __init__(self, module, readonly_attributes):
self.module=module
self.readonly_attributes=readonly_attributes
def __setattr__(self, item, value):
if item in ['module', 'readonly_attributes']:
return super(MakeModuleAttributesReadonly, self).__setattr__(item, value)
if item in self.readonly_attributes:
raise AttributeIsReadonly('Access on attribute %r is readonly' % item)
return setattr(self.module, item, value)
def __getattr__(self, item):
return getattr(self.module, item)
sys.modules['sys']=MakeModuleAttributesReadonly(sys, ['path'])
#import sys
#sys.path=sys.path # this will raise the above AttributeIsReadonly
它引发 AttributeIsReadonly
,我看到代码行堆栈跟踪。
It raises AttributeIsReadonly
and I see the code line and the stack trace.
这篇关于sys.path在哪里被替换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!