modifying local environment in python
You're not trying to modify a local environment, you're trying to modify the module's dictionary, which you can access by calling globals().

Note: the dictionary returned by locals() isn't meant to be modified.