Python import prevention

I have python built into the application as a scripting platform, so users can write python scripts. I am trying to prevent imports, so they cannot be harmful in any way and must adhere to the API provided.

I came up with the following Python code:

__builtins__ .__import__= None reload = None 

This prevents imports and prevents reloading of modules. Reboot prevention is required so that they cannot reload the built-in functions, giving them back working imports.

However, I am not a Python expert. Is there anything else that I am missing that the user can still import modules?

thanks

+8
python scripting import
source share
1 answer

What you probably want is to run Python in a sandbox. There are several ways to do this, for example PyPy has sandbox support .

You can also try isolating the Python process itself using external tools, but I suggest that it depends on the operating system.

+2
source share

All Articles