How can I block a Python stdlib module from being imported?
问题 In my Python script, I want to prevent certain stdlib modules, such as os and sys , from being imported. How would I accomplish this? 回答1: Taking you very literally, and if you just mean "to stub them out so that they won't be loaded by a straight import", not "make them unloadable by untrusted code", then: import sys sys.modules['os'] = None sys.modules['system'] = None Of course, there is no module system so you might have meant sys , in which case you're in trouble. If you're trying to