In my Python script, I want to prevent certain stdlib modules, such as os
and sys
, from being imported. How would I accomplish this?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
Don't import them. More generally, don't execute untrusted code inside your module.
eval()
looks spiffy but it's almost certainly not your friend.If you're intent on sandboxing external code, look at the SandboxedPython article on the Python wiki. Until you've read (and understood) everything there, please don't try it.
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:
Of course, there is no module
system
so you might have meantsys
, in which case you're in trouble.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.