I have the DVCS Mercurial on my PATH in a Windows system respective in a GNU/Linux system. If I open a Bash shell in GNU/Linux I can execute hg (the Mercurial command), the same goes if I open a Command Prompt in Windows.
I would like to execute hg and check exit status and capture the output. I know about the subprocess module and check_output. But I can´t get it to use my PATH. I would prefer not to hard code the path as I want the script to work as long as you have Mercurial on your path.
An example would be greatly appreciated.
Happy holidays!
If you already have
hgin your path you shouldn’t need anything special in the call tosubprocesssince you should get the same environment.Anyway, if you want to explicitly add a PATH environment variable to your subprocess, you can do this:
This code copies the environment you’re getting from your shell and overwrites the
PATHvariable. You might want to append toPATHinstead if you’re going to use the sameenvdictionary to run other commands.There’s no need to copy the whole environment, but this will prevent other problems you might have from happening because other environment variables are missing.