Where does sys.path get replaced?

我们两清 提交于 2019-12-14 03:14:13

问题


Some library does change my sys.path in a way that I don't want it to.

But I can't find it. The affected virtualenv has a lot of libraries installed.

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?

Related

  • Debugging modifications of sys.path

回答1:


I found the evil code line like this.

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

It raises AttributeIsReadonly and I see the code line and the stack trace.



来源:https://stackoverflow.com/questions/33212466/where-does-sys-path-get-replaced

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!