0

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'

dhiraj
  • 115
  • 9
  • This works for me with pysandbox version 1.5.1. Which version are you using? – nofinator Mar 19 '19 at 20:07
  • I am using the same version 1.5.1. Did you do any additional configuration apart from adding 'datetime' to the config? In my case, I had to modify the SandboxConfig to enable 'traceback' for pysandbox to get the path of 'datetime' module. It still doesn't work for me. – dhiraj Mar 19 '19 at 20:11
  • All I did was create a new virtualenv, run `pip install pysandbox`, and then do `from sandbox import SandboxConfig; sandbox = SandboxConfig('datetime', cpython_restricted=False)`. I'm using Python 2.7. Does that work for you? – nofinator Mar 19 '19 at 20:12
  • Thanks! I did a deep dive into this issue and figured out that my global imports are not available inside any function. `import datetime def get_today(): print(datetime.date.today())` gives **global name datetime is not defined** error. But if I pass datetime as an argument to the function only then it works. – dhiraj Mar 20 '19 at 14:54

1 Answers1

1

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.

vardos
  • 334
  • 3
  • 13