如何阻止导入Python stdlib模块? [英] How can I block a Python stdlib module from being imported?

查看:91
本文介绍了如何阻止导入Python stdlib模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Python脚本中,我想阻止某些stdlib模块,例如 os sys ,进口。我将如何实现这一目标?

In my Python script, I want to prevent certain stdlib modules, such as os and sys, from being imported. How would I accomplish this?

推荐答案

非常字面意思,如果你的意思是将它们剔除以便赢得胜利 通过直接导入加载,通过不受信任的代码使其无法加载,然后:

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

当然,没有模块系统所以你可能意味着 sys ,在这种情况下你遇到了麻烦。

Of course, there is no module system so you might have meant sys, in which case you're in trouble.

如果你试图让不受信任的代码无法做坏事,那么看看< a href =http://wiki.python.org/moin/SandboxedPython =nofollow> http://wiki.python.org/moin/SandboxedPython 并意识到你在追求的不是马上可行。

If you're trying to keep untrusted code from being able to do Bad Things, then take a look at http://wiki.python.org/moin/SandboxedPython and realise that you're after something not immediately feasible.

这篇关于如何阻止导入Python stdlib模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆