Search code examples
pythonpysandbox

Adding new feature not working in pysandbox


I am using the pysandbox to run the Python code in sandbox environment. I got it up and running, but I want it to support python datetime module. As per the documentation, I added 'datetime' in the config as follows,

sandbox = SandboxConfig('datetime', cpython_restricted=False)

but if I run the code I still get the error,

'global name datetime is not defined'


Solution

  • There is some issue with the pysandbox. Looks like you are passing some parameter to the sandboxed function through the locals variable. In this case, the import is not added to the global namespace.

    Simply write global datetime right after you import it and it will work. Otherwise, you may also pass it as a function parameter.