I want to block users for using the command prompt, but I also want to allow them to use Python. However, Python has the os module which users can use to execute commands, like:

>>> import os
>>> os.popen("command to be run eg start "C:/")

How can I stop Python from allowing os.popen (above) to run?


You should be looking for a full sandbox, and not just preventing os from being imported.

For instance, you can use RestrictedPython for blocking imports.

Or you can set sys.modules['os']=None. Look at this related post for more options.


Instead of blocking, you could allow users to run python in a "jail". They would be able to run commands, but in a safe sandbox.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.