This is just the which command, isn't it?  You could replace this whole function with something a lot simpler:

```python
for pathdir in os.getenv('PATH').split(':'):
    if os.path.isfile(os.path.join(pathdir, command)):
        return
raise Exception("command '{0}' not found".format(command))
```