I'm not a python expert, but the way I was able to import python modules so they work in keyboard maestro's shell environment is by adding the following lines:
If there is still interest... The subroutine shown will take a Python Script as an argument, run it, then return the results. As noted in the comment, putting this in a subroutine means that it doesn't have access to local variables in the calling routine. But import os in the Python code gets you access to KMVAR_everything_in_scope.